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