new fields: gallery, time/place for events
[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.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
19
20
21 class Category(models.Model):
22     taxonomy = models.CharField(_('taxonomy'), max_length=32, choices=app_settings.TAXONOMIES)
23
24     class Meta:
25         verbose_name = _('category')
26         verbose_name_plural = _('categories')
27
28     def __unicode__(self):
29         return self.title or u""
30
31     @models.permalink
32     def get_absolute_url(self):
33         return 'migdal_category', [self.slug]
34
35
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),
39 })
40
41
42 class PublishedEntryManager(models.Manager):
43     def get_query_set(self):
44         return super(PublishedEntryManager, self).get_query_set().filter(
45                 tQ(published=True)
46             )
47
48
49 class PhotoGallery(models.Model):
50     key = models.CharField(max_length=64)
51
52     def __unicode__(self):
53         return self.key
54
55
56 class Photo(models.Model):
57     gallery = models.ForeignKey(PhotoGallery)
58     image = models.ImageField(_('image'), upload_to='entry/photo/', null=True, blank=True)
59
60
61 class Entry(models.Model):
62     type = models.CharField(
63         max_length=16,
64         choices=((t.db, t.slug) for t in app_settings.TYPES),
65         db_index=True)
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)
79
80     objects = models.Manager()
81     published_objects = PublishedEntryManager()
82
83     class Meta:
84         verbose_name = _('entry')
85         verbose_name_plural = _('entries')
86         ordering = ['-date']
87
88     def __unicode__(self):
89         return self.title
90
91     def save(self, *args, **kwargs):
92         published_now = False
93         for lc, ln in settings.LANGUAGES:
94             if (getattr(self, "published_%s" % lc)
95                     and getattr(self, "published_at_%s" % lc) is None):
96                 now = datetime.now()
97                 setattr(self, "published_at_%s" % lc, now)
98                 if self.first_published_at is None:
99                     self.first_published_at = now
100                     published_now = True
101         super(Entry, self).save(*args, **kwargs)
102         if published_now and self.pk is not None:
103             self.notify_author_published()
104
105     def clean(self):
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)
111
112     @models.permalink
113     def get_absolute_url(self):
114         return 'migdal_entry_%s' % self.type, [self.slug]
115
116     def get_type(self):
117         return dict(app_settings.TYPES_DICT)[self.type]
118
119     def notify_author_published(self):
120         if not self.author_email:
121             return
122         site = Site.objects.get_current()
123         mail_text = loader.get_template('migdal/mail/published.txt').render(
124             Context({
125                 'entry': self,
126                 'site': site,
127             }))
128         send_mail(
129             ugettext(u'Your story has been published at %s.') % site.domain,
130             mail_text, settings.SERVER_EMAIL, [self.author_email]
131         )
132
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:
137                     yield f.read()
138
139
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'))),
143                 default='n'),
144 })
145
146 TEXTILE_HELP = _('Use <a href="https://txstyle.org/article/44/an-overview-of-the-textile-syntax">Textile</a> syntax.')
147
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),
159 })
160
161
162 class Attachment(models.Model):
163     file = models.FileField(_('file'), upload_to='entry/attach/')
164     entry = models.ForeignKey(Entry)
165
166     def url(self):
167         return self.file.url if self.file else ''
168
169
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(
174             Context({
175                 'comment': instance,
176                 'site': site,
177             }))
178         send_mail(
179             ugettext(u'New comment under your story at %s.') % site.domain,
180             mail_text, settings.SERVER_EMAIL, 
181             [instance.content_object.author_email]
182         )
183 models.signals.post_save.connect(notify_new_comment, sender=XtdComment)
184
185
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):
189         fields = (
190             comment.user, comment.user_name, comment.user_email,
191             comment.user_url, comment.submit_date, comment.ip_address,
192             comment.followup, comment.comment)
193         mail_managers(
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)
198         return False
199     return True
200 # comment_will_be_posted.connect(spamfilter)