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.db import models
6 from django.utils.translation import get_language, ugettext_lazy as _, ugettext
7 from markupfield.fields import MarkupField
8 from migdal.helpers import add_translatable
9 from migdal import app_settings
13 class Category(models.Model):
14 taxonomy = models.CharField(_('taxonomy'), max_length=32,
15 choices=app_settings.TAXONOMIES)
18 verbose_name = _('category')
19 verbose_name_plural = _('categories')
21 def __unicode__(self):
25 def get_absolute_url(self):
26 return ('migdal_category', [self.slug])
29 add_translatable(Category, {
30 'title': models.CharField(max_length=64, unique=True, db_index=True),
31 'slug': models.SlugField(unique=True, db_index=True),
35 class Entry(models.Model):
36 type = models.CharField(max_length=16,
37 choices=((t.db, t.slug) for t in app_settings.TYPES),
39 date = models.DateTimeField(auto_now_add=True, db_index=True)
40 author = models.CharField(_('author'), max_length=128)
41 author_email = models.EmailField(_('author email'), max_length=128, null=True, blank=True,
42 help_text=_('Used only to display gravatar and send notifications.'))
43 image = models.ImageField(_('image'), upload_to='entry/image/', null=True, blank=True)
44 promo = models.BooleanField(_('promoted'), default=False)
45 categories = models.ManyToManyField(Category, null=True, blank=True)
48 verbose_name = _('entry')
49 verbose_name_plural = _('entries')
52 def __unicode__(self):
55 def save(self, *args, **kwargs):
56 # convert blank to null for slug uniqueness check to work
57 for lc, ln in app_settings.OPTIONAL_LANGUAGES:
58 slug_name = "slug_%s" % lc
59 if hasattr(self, slug_name) == u'':
60 setattr(self, slug_name, None)
61 super(Entry, self).save(*args, **kwargs)
64 def get_absolute_url(self):
65 return ('migdal_entry_%s' % self.type, [self.slug])
68 return dict(app_settings.TYPES_DICT)[self.type]
70 add_translatable(Entry, languages=app_settings.OPTIONAL_LANGUAGES, fields={
71 'needed': models.CharField(_('needed'), max_length=1, db_index=True, choices=(
72 ('n', _('Unneeded')), ('w', _('Needed')), ('y', _('Done'))),
76 add_translatable(Entry, {
77 'slug': models.SlugField(unique=True, db_index=True, null=True, blank=True),
78 'title': models.CharField(_('title'), max_length=255, null=True, blank=True),
79 'lead': MarkupField(_('lead'), markup_type='textile_pl', null=True, blank=True,
80 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.')),
81 'body': MarkupField(_('body'), markup_type='textile_pl', null=True, blank=True,
82 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.')),
83 'published': models.BooleanField(_('published'), default=False),
87 class Attachment(models.Model):
88 file = models.FileField(_('file'), upload_to='entry/attach/')
89 entry = models.ForeignKey(Entry)
92 return self.file.url if self.file else ''