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):
15 verbose_name = _('category')
16 verbose_name_plural = _('categories')
18 def __unicode__(self):
22 def get_absolute_url(self):
23 return ('migdal_category', [self.slug])
26 add_translatable(Category, {
27 'title': models.CharField(max_length=64, unique=True, db_index=True),
28 'slug': models.SlugField(unique=True, db_index=True),
32 class Entry(models.Model):
33 type = models.CharField(max_length=16,
34 choices=((t.db, t.slug) for t in settings.TYPES),
36 date = models.DateTimeField(auto_now_add=True, db_index=True)
37 author = models.CharField(_('author'), max_length=128)
38 author_email = models.EmailField(_('author email'), max_length=128, null=True, blank=True,
39 help_text=_('Used only to display gravatar and send notifications.'))
40 image = models.ImageField(_('image'), upload_to='entry/image/', null=True, blank=True)
41 promo = models.BooleanField(_('promoted'), default=False)
42 categories = models.ManyToManyField(Category, null=True, blank=True)
45 verbose_name = _('entry')
46 verbose_name_plural = _('entries')
49 def __unicode__(self):
52 def save(self, *args, **kwargs):
53 # convert blank to null for slug uniqueness check to work
54 for lc, ln in settings.OPTIONAL_LANGUAGES:
55 slug_name = "slug_%s" % lc
56 if hasattr(self, slug_name) == u'':
57 setattr(self, slug_name, None)
58 super(Entry, self).save(*args, **kwargs)
61 def get_absolute_url(self):
62 return ('migdal_entry_%s' % self.type, [self.slug])
65 return dict(settings.TYPES_DICT)[self.type]
67 add_translatable(Entry, languages=settings.OPTIONAL_LANGUAGES, fields={
68 'needed': models.CharField(_('needed'), max_length=1, db_index=True, choices=(
69 ('n', _('Unneeded')), ('w', _('Needed')), ('y', _('Done'))),
73 add_translatable(Entry, {
74 'slug': models.SlugField(unique=True, db_index=True, null=True, blank=True),
75 'title': models.CharField(_('title'), max_length=255, null=True, blank=True),
76 'lead': MarkupField(_('lead'), markup_type='textile_pl', null=True, blank=True,
77 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.')),
78 'body': MarkupField(_('body'), markup_type='textile_pl', null=True, blank=True,
79 help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.')),
80 'published': models.BooleanField(_('published'), default=False),
84 class Attachment(models.Model):
85 file = models.FileField(_('file'), upload_to='entry/attach/')
86 entry = models.ForeignKey(Entry)
89 return self.file.url if self.file else ''