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.
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
16 class Category(models.Model):
17 taxonomy = models.CharField(_('taxonomy'), max_length=32,
18 choices=app_settings.TAXONOMIES)
21 verbose_name = _('category')
22 verbose_name_plural = _('categories')
24 def __unicode__(self):
28 def get_absolute_url(self):
29 return ('migdal_category', [self.slug])
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),
38 class Entry(models.Model):
39 type = models.CharField(max_length=16,
40 choices=((t.db, t.slug) for t in app_settings.TYPES),
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'))
51 verbose_name = _('entry')
52 verbose_name_plural = _('entries')
55 def __unicode__(self):
58 def save(self, *args, **kwargs):
59 if self.pk is not None:
60 orig = type(self).objects.get(pk=self.pk)
62 for lc, ln in settings.LANGUAGES:
63 if (not getattr(orig, "published_%s" % lc) and
64 getattr(self, "published_%s" % lc)):
67 self.notify_author_published()
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)
77 def get_absolute_url(self):
78 return ('migdal_entry_%s' % self.type, [self.slug])
81 return dict(app_settings.TYPES_DICT)[self.type]
83 def notify_author_published(self):
84 if not self.author_email:
86 site = Site.objects.get_current()
87 mail_text = loader.get_template('migdal/mail/published.txt').render(
93 ugettext(u'Your story has been published at %s.') % site.domain,
94 mail_text, settings.SERVER_EMAIL, [self.author_email]
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'))),
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),
117 class Attachment(models.Model):
118 file = models.FileField(_('file'), upload_to='entry/attach/')
119 entry = models.ForeignKey(Entry)
122 return self.file.url if self.file else ''
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(
136 ugettext(u'New comment under your story at %s.') % site.domain,
137 mail_text, settings.SERVER_EMAIL,
138 [instance.content_object.author_email]
140 models.signals.post_save.connect(notify_new_comment, sender=XtdComment)