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 type = models.CharField(max_length=16,
37 choices=((t.db, t.slug) for t in 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 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]
70 add_translatable(Entry, languages=settings.OBLIGATORY_LANGUAGES, fields={
71 'slug': models.SlugField(unique=True, db_index=True),
72 'title': models.CharField(_('title'), max_length=255),
73 'lead': MarkupField(_('lead'), markup_type='textile_pl'),
76 add_translatable(Entry, languages=settings.OPTIONAL_LANGUAGES, fields={
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 'needed': models.CharField(_('needed'), max_length=1, db_index=True, choices=(
81 ('n', _('Unneeded')), ('w', _('Needed')), ('y', _('Done'))),
85 add_translatable(Entry, {
86 'body': MarkupField(_('body'), markup_type='textile_pl', null=True, blank=True),
87 'published': models.BooleanField(_('published'), default=False),
92 class Attachment(models.Model):
93 file = models.FileField(_('file'), upload_to='entry/attach/')
94 entry = models.ForeignKey(Entry)
97 return self.file.url if self.file else ''