Added support for spam checks.

This commit is contained in:
Martin Mahner 2011-06-02 21:03:57 +02:00
parent 9248e8edd5
commit 19bfd3f769
3 changed files with 25 additions and 2 deletions

View file

@ -1,4 +1,4 @@
from pastebin.apps.dpaste.models import Snippet
from pastebin.apps.dpaste.models import Snippet, Spamword
from django.contrib import admin
class SnippetAdmin(admin.ModelAdmin):
@ -11,3 +11,4 @@ class SnippetAdmin(admin.ModelAdmin):
)
admin.site.register(Snippet, SnippetAdmin)
admin.site.register(Spamword)

View file

@ -1,7 +1,7 @@
from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from pastebin.apps.dpaste.models import Snippet
from pastebin.apps.dpaste.models import Snippet, Spamword
from pastebin.apps.dpaste.highlight import LEXER_LIST_ALL, LEXER_LIST, LEXER_DEFAULT
import datetime
@ -47,6 +47,14 @@ class SnippetForm(forms.ModelForm):
except KeyError:
pass
def clean_content(self):
content = self.cleaned_data.get('content')
if content:
regex = Spamword.objects.get_regex()
if regex.findall(content):
raise forms.ValidationError('This snippet was identified as SPAM.')
return content
def save(self, parent=None, *args, **kwargs):
# Set parent snippet

View file

@ -2,6 +2,7 @@ import datetime
import difflib
import random
import mptt
import re
from django.db import models
from django.db.models import permalink
from django.utils.translation import ugettext_lazy as _
@ -46,3 +47,16 @@ class Snippet(models.Model):
return '%s' % self.secret_id
mptt.register(Snippet, order_insertion_by=['content'])
class SpamwordManager(models.Manager):
def get_regex(self):
return re.compile(r'|'.join((i[1] for i in self.values_list())),
re.MULTILINE)
class Spamword(models.Model):
word = models.CharField(max_length=100)
objects = SpamwordManager()
def __unicode__(self):
return self.word