Add simple spamfilter
[django-migdal.git] / migdal / models.py
1 # -*- coding: utf-8 -*-
2 # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 import re
6 from datetime import datetime
7 from django.conf import settings
8 from django.contrib.comments.signals import comment_will_be_posted
9 from django.contrib.sites.models import Site
10 from django.core.exceptions import ValidationError
11 from django.core.mail import mail_managers, send_mail
12 from django.db import models
13 from django.template import loader, Context
14 from django.utils.translation import get_language, ugettext_lazy as _, ugettext
15 from django_comments_xtd.models import XtdComment
16 from markupfield.fields import MarkupField
17 from fnpdjango.utils.models.translation import add_translatable
18 from migdal import app_settings
19 from migdal.fields import SlugNullField
20
21 class Category(models.Model):
22     taxonomy = models.CharField(_('taxonomy'), max_length=32,
23                     choices=app_settings.TAXONOMIES)
24
25     class Meta:
26         verbose_name = _('category')
27         verbose_name_plural = _('categories')
28
29     def __unicode__(self):
30         return self.title
31
32     @models.permalink
33     def get_absolute_url(self):
34         return ('migdal_category', [self.slug])
35
36
37 add_translatable(Category, {
38     'title': models.CharField(max_length=64, unique=True, db_index=True),
39     'slug': models.SlugField(unique=True, db_index=True),
40 })
41
42
43 class Entry(models.Model):
44     type = models.CharField(max_length=16,
45             choices=((t.db, t.slug) for t in app_settings.TYPES),
46             db_index=True)
47     date = models.DateTimeField(_('created at'), auto_now_add=True, db_index=True)
48     changed_at = models.DateTimeField(_('changed at'), auto_now=True, db_index=True)
49     author = models.CharField(_('author'), max_length=128)
50     author_email = models.EmailField(_('author email'), max_length=128, null=True, blank=True,
51             help_text=_('Used only to display gravatar and send notifications.'))
52     image = models.ImageField(_('image'), upload_to='entry/image/', null=True, blank=True)
53     promo = models.BooleanField(_('promoted'), default=False)
54     categories = models.ManyToManyField(Category, null=True, blank=True, verbose_name=_('categories'))
55
56     class Meta:
57         verbose_name = _('entry')
58         verbose_name_plural = _('entries')
59         ordering = ['-date']
60
61     def __unicode__(self):
62         return self.title
63
64     def save(self, *args, **kwargs):
65         if self.pk is not None:
66             orig = type(self).objects.get(pk=self.pk)
67             published_now = False
68             for lc, ln in settings.LANGUAGES:
69                 if (getattr(self, "published_%s" % lc)
70                         and getattr(self, "published_at_%s" % lc) is None):
71                     setattr(self, "published_at_%s" % lc, datetime.now())
72                     published_now = True
73             if published_now:
74                 self.notify_author_published()
75         super(Entry, self).save(*args, **kwargs)
76
77     def clean(self):
78         for lc, ln in settings.LANGUAGES:
79             if (getattr(self, "published_%s" % lc) and
80                     not getattr(self, "slug_%s" % lc)):
81                 raise ValidationError(
82                     ugettext("Published entry should have a slug in relevant language (%s).") % lc)
83
84     @models.permalink
85     def get_absolute_url(self):
86         return ('migdal_entry_%s' % self.type, [self.slug])
87
88     def get_type(self):
89         return dict(app_settings.TYPES_DICT)[self.type]
90
91     def notify_author_published(self):
92         if not self.author_email:
93             return
94         site = Site.objects.get_current()
95         mail_text = loader.get_template('migdal/mail/published.txt').render(
96             Context({
97                 'entry': self,
98                 'site': site,
99             }))
100         send_mail(
101             ugettext(u'Your story has been published at %s.') % site.domain,
102             mail_text, settings.SERVER_EMAIL, [self.author_email]
103         )
104
105
106 add_translatable(Entry, languages=app_settings.OPTIONAL_LANGUAGES, fields={
107     'needed': models.CharField(_('needed'), max_length=1, db_index=True, choices=(
108                 ('n', _('Unneeded')), ('w', _('Needed')), ('y', _('Done'))),
109                 default='n'),
110 })
111
112 add_translatable(Entry, {
113     'slug': SlugNullField(unique=True, db_index=True, null=True, blank=True),
114     'title': models.CharField(_('title'), max_length=255, null=True, blank=True),
115     'lead': MarkupField(_('lead'), markup_type='textile_pl', null=True, blank=True,
116                 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.')),
117     'body': MarkupField(_('body'), markup_type='textile_pl', null=True, blank=True,
118                 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.')),
119     'published': models.BooleanField(_('published'), default=False),
120     'published_at': models.DateTimeField(_('published at'), null=True, blank=True),
121 })
122
123
124 class Attachment(models.Model):
125     file = models.FileField(_('file'), upload_to='entry/attach/')
126     entry = models.ForeignKey(Entry)
127
128     def url(self):
129         return self.file.url if self.file else ''
130
131
132
133 def notify_new_comment(sender, instance, created, **kwargs):
134     if (created and isinstance(instance.content_object, Entry) and
135                 instance.content_object.author_email):
136         site = Site.objects.get_current()
137         mail_text = loader.get_template('migdal/mail/new_comment.txt').render(
138             Context({
139                 'comment': instance,
140                 'site': site,
141             }))
142         send_mail(
143             ugettext(u'New comment under your story at %s.') % site.domain,
144             mail_text, settings.SERVER_EMAIL, 
145             [instance.content_object.author_email]
146         )
147 models.signals.post_save.connect(notify_new_comment, sender=XtdComment)
148
149
150 def spamfilter(sender, comment, **kwargs):
151     """Very simple spam filter. Just don't let any HTML links go through."""
152     if re.search(r"<a\s+href=", comment.comment):
153         fields = (comment.user, comment.user_name, comment.user_email,
154             comment.user_url, comment.submit_date, comment.ip_address,
155             comment.followup, comment.comment)
156         mail_managers(u"Spam filter report",
157             (u"""This comment was turned down as SPAM: \n""" +
158             """\n%s""" * len(fields) +
159             """\n\nYou don't have to do anything.""") % fields)
160         return False
161     return True
162 comment_will_be_posted.connect(spamfilter)