6ba28ee9f2007d37261d2e2660c11aea154c2423
[prawokultury.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 from django.conf import settings
6 from django.contrib.sites.models import Site
7 from django.core.mail import send_mail
8 from django.db import models
9 from django.template import loader, Context
10 from django.utils.translation import get_language, ugettext_lazy as _, ugettext
11 from django_comments_xtd.models import XtdComment
12 from markupfield.fields import MarkupField
13 from migdal import app_settings
14 from migdal.helpers import add_translatable
15
16 class Category(models.Model):
17     taxonomy = models.CharField(_('taxonomy'), max_length=32,
18                     choices=app_settings.TAXONOMIES)
19
20     class Meta:
21         verbose_name = _('category')
22         verbose_name_plural = _('categories')
23
24     def __unicode__(self):
25         return self.title
26
27     @models.permalink
28     def get_absolute_url(self):
29         return ('migdal_category', [self.slug])
30
31
32 add_translatable(Category, {
33     'title': models.CharField(max_length=64, unique=True, db_index=True),
34     'slug': models.SlugField(unique=True, db_index=True),
35 })
36
37
38 class Entry(models.Model):
39     type = models.CharField(max_length=16,
40             choices=((t.db, t.slug) for t in app_settings.TYPES),
41             db_index=True)
42     date = models.DateTimeField(auto_now_add=True, db_index=True, editable=True)
43     author = models.CharField(_('author'), max_length=128)
44     author_email = models.EmailField(_('author email'), max_length=128, null=True, blank=True,
45             help_text=_('Used only to display gravatar and send notifications.'))
46     image = models.ImageField(_('image'), upload_to='entry/image/', null=True, blank=True)
47     promo = models.BooleanField(_('promoted'), default=False)
48     categories = models.ManyToManyField(Category, null=True, blank=True, verbose_name=_('categories'))
49
50     class Meta:
51         verbose_name = _('entry')
52         verbose_name_plural = _('entries')
53         ordering = ['-date']
54
55     def __unicode__(self):
56         return self.title
57
58     def save(self, *args, **kwargs):
59         if self.pk is not None:
60             orig = type(self).objects.get(pk=self.pk)
61             published_now = False
62             for lc, ln in settings.LANGUAGES:
63                 if (not getattr(orig, "published_%s" % lc) and
64                         getattr(self, "published_%s" % lc)):
65                     published_now = True
66             if published_now:
67                 self.notify_author_published()
68
69         # convert blank to null for slug uniqueness check to work
70         for lc, ln in app_settings.OPTIONAL_LANGUAGES:
71             slug_name = "slug_%s" % lc
72             if hasattr(self, slug_name) == u'':
73                 setattr(self, slug_name, None)
74         super(Entry, self).save(*args, **kwargs)
75
76     @models.permalink
77     def get_absolute_url(self):
78         return ('migdal_entry_%s' % self.type, [self.slug])
79
80     def get_type(self):
81         return dict(app_settings.TYPES_DICT)[self.type]
82
83     def notify_author_published(self):
84         if not self.author_email:
85             return
86         site = Site.objects.get_current()
87         mail_text = loader.get_template('migdal/mail/published.txt').render(
88             Context({
89                 'entry': self,
90                 'site': site,
91             }))
92         send_mail(
93             ugettext(u'Your story has been published at %s.') % site.domain,
94             mail_text, settings.SERVER_EMAIL, [self.author_email]
95         )
96             
97         
98         
99
100 add_translatable(Entry, languages=app_settings.OPTIONAL_LANGUAGES, fields={
101     'needed': models.CharField(_('needed'), max_length=1, db_index=True, choices=(
102                 ('n', _('Unneeded')), ('w', _('Needed')), ('y', _('Done'))),
103                 default='n'),
104 })
105
106 add_translatable(Entry, {
107     'slug': models.SlugField(unique=True, db_index=True, null=True, blank=True),
108     'title': models.CharField(_('title'), max_length=255, null=True, blank=True),
109     'lead': MarkupField(_('lead'), markup_type='textile_pl', null=True, blank=True,
110                 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.')),
111     'body': MarkupField(_('body'), markup_type='textile_pl', null=True, blank=True,
112                 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.')),
113     'published': models.BooleanField(_('published'), default=False),
114 })
115
116
117 class Attachment(models.Model):
118     file = models.FileField(_('file'), upload_to='entry/attach/')
119     entry = models.ForeignKey(Entry)
120
121     def url(self):
122         return self.file.url if self.file else ''
123
124
125
126 def notify_new_comment(sender, instance, created, **kwargs):
127     if (created and isinstance(instance.content_object, Entry) and
128                 instance.content_object.author_email):
129         site = Site.objects.get_current()
130         mail_text = loader.get_template('migdal/mail/new_comment.txt').render(
131             Context({
132                 'comment': instance,
133                 'site': site,
134             }))
135         send_mail(
136             ugettext(u'New comment under your story at %s.') % site.domain,
137             mail_text, settings.SERVER_EMAIL, 
138             [instance.content_object.author_email]
139         )
140 models.signals.post_save.connect(notify_new_comment, sender=XtdComment)