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.
6 from datetime import datetime
7 from django.conf import settings
8 from django.contrib.sites.models import Site
9 from django.core.exceptions import ValidationError
10 from django.core.mail import mail_managers, send_mail
11 from django.db import models
12 from django.template import loader, Context
13 from django.utils.translation import ugettext_lazy as _, ugettext
14 from django_comments_xtd.models import XtdComment
15 from fnpdjango.utils.fields import TextileField
16 from fnpdjango.utils.models.translation import add_translatable, tQ
17 from migdal import app_settings
18 from migdal.fields import SlugNullField
21 class Category(models.Model):
22 taxonomy = models.CharField(_('taxonomy'), max_length=32, choices=app_settings.TAXONOMIES)
25 verbose_name = _('category')
26 verbose_name_plural = _('categories')
28 def __unicode__(self):
29 return self.title or u""
32 def get_absolute_url(self):
33 return 'migdal_category', [self.slug]
36 add_translatable(Category, {
37 'title': models.CharField(max_length=64, unique=True, db_index=True),
38 'slug': models.SlugField(unique=True, db_index=True),
42 class PublishedEntryManager(models.Manager):
43 def get_query_set(self):
44 return super(PublishedEntryManager, self).get_query_set().filter(
49 class PhotoGallery(models.Model):
50 key = models.CharField(max_length=64)
52 def __unicode__(self):
56 class Photo(models.Model):
57 gallery = models.ForeignKey(PhotoGallery)
58 image = models.ImageField(_('image'), upload_to='entry/photo/', null=True, blank=True)
61 class Entry(models.Model):
62 type = models.CharField(
64 choices=((t.db, t.slug) for t in app_settings.TYPES),
66 date = models.DateTimeField(_('created at'), auto_now_add=True, db_index=True)
67 changed_at = models.DateTimeField(_('changed at'), auto_now=True, db_index=True)
68 author = models.CharField(_('author'), max_length=128)
69 author_email = models.EmailField(
70 _('author email'), max_length=128, null=True, blank=True,
71 help_text=_('Used only to display gravatar and send notifications.'))
72 image = models.ImageField(_('image'), upload_to='entry/image/', null=True, blank=True)
73 promo = models.BooleanField(_('promoted'), default=False)
74 in_stream = models.BooleanField(_('in stream'), default=True)
75 categories = models.ManyToManyField(Category, blank=True, verbose_name=_('categories'))
76 first_published_at = models.DateTimeField(_('published at'), null=True, blank=True)
77 canonical_url = models.URLField(_('canonical link'), null=True, blank=True)
78 gallery = models.ForeignKey(PhotoGallery, null=True, blank=True)
80 objects = models.Manager()
81 published_objects = PublishedEntryManager()
84 verbose_name = _('entry')
85 verbose_name_plural = _('entries')
88 def __unicode__(self):
91 def save(self, *args, **kwargs):
93 for lc, ln in settings.LANGUAGES:
94 if (getattr(self, "published_%s" % lc)
95 and getattr(self, "published_at_%s" % lc) is None):
97 setattr(self, "published_at_%s" % lc, now)
98 if self.first_published_at is None:
99 self.first_published_at = now
101 super(Entry, self).save(*args, **kwargs)
102 if published_now and self.pk is not None:
103 self.notify_author_published()
106 for lc, ln in settings.LANGUAGES:
107 if (getattr(self, "published_%s" % lc) and
108 not getattr(self, "slug_%s" % lc)):
109 raise ValidationError(
110 ugettext("Published entry should have a slug in relevant language (%s).") % lc)
113 def get_absolute_url(self):
114 return 'migdal_entry_%s' % self.type, [self.slug]
117 return dict(app_settings.TYPES_DICT)[self.type]
119 def notify_author_published(self):
120 if not self.author_email:
122 site = Site.objects.get_current()
123 mail_text = loader.get_template('migdal/mail/published.txt').render(
129 ugettext(u'Your story has been published at %s.') % site.domain,
130 mail_text, settings.SERVER_EMAIL, [self.author_email]
133 def inline_html(self):
134 for att in self.attachment_set.all():
135 if att.file.name.endswith(".html"):
136 with open(att.file.path) as f:
140 add_translatable(Entry, languages=app_settings.OPTIONAL_LANGUAGES, fields={
141 'needed': models.CharField(_('needed'), max_length=1, db_index=True, choices=(
142 ('n', _('Unneeded')), ('w', _('Needed')), ('y', _('Done'))),
146 TEXTILE_HELP = _('Use <a href="https://txstyle.org/article/44/an-overview-of-the-textile-syntax">Textile</a> syntax.')
148 add_translatable(Entry, {
149 'slug': SlugNullField(unique=True, db_index=True, null=True, blank=True),
150 'title': models.CharField(_('title'), max_length=255, null=True, blank=True),
151 'lead': TextileField(
152 _('lead'), markup_type='textile_pl', null=True, blank=True, help_text=TEXTILE_HELP),
153 'body': TextileField(
154 _('body'), markup_type='textile_pl', null=True, blank=True, help_text=TEXTILE_HELP),
155 'place': models.CharField(_('place'), null=True, blank=True, max_length=256),
156 'time': models.CharField(_('time'), null=True, blank=True, max_length=256),
157 'published': models.BooleanField(_('published'), default=False),
158 'published_at': models.DateTimeField(_('published at'), null=True, blank=True),
162 class Attachment(models.Model):
163 file = models.FileField(_('file'), upload_to='entry/attach/')
164 entry = models.ForeignKey(Entry)
167 return self.file.url if self.file else ''
170 def notify_new_comment(sender, instance, created, **kwargs):
171 if created and isinstance(instance.content_object, Entry) and instance.content_object.author_email:
172 site = Site.objects.get_current()
173 mail_text = loader.get_template('migdal/mail/new_comment.txt').render(
179 ugettext(u'New comment under your story at %s.') % site.domain,
180 mail_text, settings.SERVER_EMAIL,
181 [instance.content_object.author_email]
183 models.signals.post_save.connect(notify_new_comment, sender=XtdComment)
186 def spamfilter(sender, comment, **kwargs):
187 """Very simple spam filter. Just don't let any HTML links go through."""
188 if re.search(r"<a\s+href=", comment.comment):
190 comment.user, comment.user_name, comment.user_email,
191 comment.user_url, comment.submit_date, comment.ip_address,
192 comment.followup, comment.comment)
194 u"Spam filter report",
195 (u"""This comment was turned down as SPAM: \n""" +
196 """\n%s""" * len(fields) +
197 """\n\nYou don't have to do anything.""") % fields)
200 # comment_will_be_posted.connect(spamfilter)