Skip to content

Commit

Permalink
added insta ban
Browse files Browse the repository at this point in the history
  • Loading branch information
ialbert committed Jun 14, 2022
1 parent 72ae37f commit 7a41cb2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
3 changes: 3 additions & 0 deletions biostar/accounts/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ def init_users():
else:

User.objects.filter(pk=user.pk).update(is_superuser=True, is_staff=True)

#Profile.objects.filter(user__pk=user.pk).update(state=Profile.TRUSTED)

# You might want to reapply the default ADMIN password on migration.
# This will log out the user from their current session.
#user.set_password(settings.DEFAULT_ADMIN_PASSWORD)
Expand Down
4 changes: 3 additions & 1 deletion biostar/forum/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ def create_post_from_json(**json_data):

def create_post(author, title, content, request=None, root=None, parent=None, ptype=Post.QUESTION, tag_val="",
nodups=True):


# Check if a post with this exact content already exists.
post = Post.objects.filter(content=content, author=author).order_by('-creation_date').first()

Expand Down Expand Up @@ -583,7 +585,7 @@ def validate_move(user, source, target):
return False


def db_logger(user=None, action=Log.MODERATE, text='', target=None, ipaddr=None, post=None):
def db_logger(user, action=Log.MODERATE, text='', target=None, ipaddr=None, post=None):
"""
Creates a database log.
"""
Expand Down
31 changes: 30 additions & 1 deletion biostar/forum/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.conf import settings
from snowpenguin.django.recaptcha2.fields import ReCaptchaField
from snowpenguin.django.recaptcha2.widgets import ReCaptchaWidget
from biostar.accounts.models import User
from biostar.accounts.models import User, Profile
from biostar.accounts.forms import get_tags_widget
from .models import Post, SharedLink
from biostar.forum import models, auth, util
Expand Down Expand Up @@ -198,6 +198,30 @@ def clean_content(self):
return content


def check_spam(content):

## Certain words lead to insta ban.
insta_ban = settings.INSTA_BAN.splitlines()
insta_ban = map(lambda x: x.strip(), insta_ban)
insta_ban = filter(None, insta_ban)

is_spam = False
for line in insta_ban:
is_spam = is_spam or line in content

return is_spam

def suspend_user(user):

if user.profile.trusted:
return

if user.profile.state == Profile.NEW:
user.profile.state = Profile.SUSPENDED
user.profile.save()
admin = User.objects.filter(is_superuser=True).order_by("pk").first()
auth.db_logger(user=admin, target=user, text=f'insta banned')

class PostShortForm(forms.Form):
MIN_LEN, MAX_LEN = 10, 10000
content = forms.CharField(widget=forms.Textarea, min_length=MIN_LEN, max_length=MAX_LEN, strip=False)
Expand All @@ -211,6 +235,11 @@ def __init__(self, post, user=None, request=None, ptype=Post.COMMENT, *args, **k

def clean_content(self):
content = self.cleaned_data["content"]
is_spam = check_spam(content)

if is_spam:
suspend_user(self.user)
raise forms.ValidationError("Spam words detected in the content")
return content

def clean(self):
Expand Down
4 changes: 4 additions & 0 deletions biostar/forum/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,7 @@ def join(*args):
'debug_toolbar',
])
MIDDLEWARE.append('debug_toolbar.middleware.DebugToolbarMiddleware')


INSTA_BAN = """
"""

0 comments on commit 7a41cb2

Please sign in to comment.