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 settings
12 class Category(models.Model):
13 taxonomy = models.CharField(_('taxonomy'), max_length=32,
14 choices=settings.TAXONOMIES)
17 verbose_name = _('category')
18 verbose_name_plural = _('categories')
20 def __unicode__(self):
24 def get_absolute_url(self):
25 return ('migdal_category', [self.slug])
28 add_translatable(Category, {
29 'title': models.CharField(max_length=64, unique=True, db_index=True),
30 'slug': models.SlugField(unique=True, db_index=True),
34 class Entry(models.Model):
35 type = models.CharField(max_length=16,
36 choices=((t.db, t.slug) for t in settings.TYPES),
38 date = models.DateTimeField(auto_now_add=True, db_index=True)
39 author = models.CharField(_('author'), max_length=128)
40 author_email = models.EmailField(_('author email'), max_length=128, null=True, blank=True,
41 help_text=_('Used only to display gravatar and send notifications.'))
42 image = models.ImageField(_('image'), upload_to='entry/image/', null=True, blank=True)
43 promo = models.BooleanField(_('promoted'), default=False)
44 categories = models.ManyToManyField(Category, null=True, blank=True)
47 verbose_name = _('entry')
48 verbose_name_plural = _('entries')
51 def __unicode__(self):
54 def save(self, *args, **kwargs):
55 # convert blank to null for slug uniqueness check to work
56 for lc, ln in settings.OPTIONAL_LANGUAGES:
57 slug_name = "slug_%s" % lc
58 if hasattr(self, slug_name) == u'':
59 setattr(self, slug_name, None)
60 super(Entry, self).save(*args, **kwargs)
63 def get_absolute_url(self):
64 return ('migdal_entry_%s' % self.type, [self.slug])
67 return dict(settings.TYPES_DICT)[self.type]
69 add_translatable(Entry, languages=settings.OPTIONAL_LANGUAGES, fields={
70 'needed': models.CharField(_('needed'), max_length=1, db_index=True, choices=(
71 ('n', _('Unneeded')), ('w', _('Needed')), ('y', _('Done'))),
75 add_translatable(Entry, {
76 'slug': models.SlugField(unique=True, db_index=True, null=True, blank=True),
77 'title': models.CharField(_('title'), max_length=255, null=True, blank=True),
78 'lead': MarkupField(_('lead'), markup_type='textile_pl', null=True, blank=True,
79 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.')),
80 'body': MarkupField(_('body'), markup_type='textile_pl', null=True, blank=True,
81 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.')),
82 'published': models.BooleanField(_('published'), default=False),
86 class Attachment(models.Model):
87 file = models.FileField(_('file'), upload_to='entry/attach/')
88 entry = models.ForeignKey(Entry)
91 return self.file.url if self.file else ''