1
from django.db import models
2
from django.contrib.auth.models import User
3
from django.contrib.contenttypes.models import ContentType
4
from django.contrib.contenttypes.fields import GenericForeignKey
5
from django.conf import settings
9
class SuspiciousInput(models.Model):
10
"""Model for collecting suspicios user input.
12
Call the check_input method with this attributes:
13
content_object = Model instance of a saved(!) object
15
text = text to check for suspicious content
18
is_suspicous = SuspiciousInput.check_input(content_object=post,
19
user=post.user, text=post.body)
23
text = models.CharField(
24
max_length=200, verbose_name='suspicious user input')
25
user = models.ForeignKey(User, verbose_name='related user')
26
content_type = models.ForeignKey(ContentType, verbose_name='related model')
27
object_id = models.PositiveIntegerField()
28
content_object = GenericForeignKey('content_type', 'object_id')
31
ordering = ['content_type_id']
32
default_permissions = ('change', 'delete',)
34
def __unicode__(self):
37
def is_suspicious(self):
38
if any(x in self.text.lower() for x in settings.ANTI_SPAM_KWRDS):
40
if re.search(settings.ANTI_SPAM_PHONE_NR, self.text):
45
def check_input(cls, *args, **kwargs):
46
user_input = cls(*args, **kwargs)
47
is_spam = user_input.is_suspicious()