cite text not required
[wolnelektury.git] / src / social / models.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.db import models
6 from django.utils.translation import ugettext_lazy as _
7 from django.conf import settings
8 from django.core.urlresolvers import reverse
9 from ssify import flush_ssi_includes
10 from catalogue.models import Book
11
12
13 class Cite(models.Model):
14     book = models.ForeignKey(Book, verbose_name=_('book'), null=True, blank=True)
15     text = models.TextField(_('text'), blank=True)
16     small = models.BooleanField(_('small'), default=False, help_text=_('Make this cite display smaller.'))
17     vip = models.CharField(_('VIP'), max_length=128, null=True, blank=True)
18     link = models.URLField(_('link'))
19     sticky = models.BooleanField(_('sticky'), default=False, db_index=True,
20                                  help_text=_('Sticky cites will take precedense.'))
21     banner = models.BooleanField(_('banner'), default=False, help_text=_('Adjust size to image, ignore the text'))
22
23     image = models.ImageField(
24         _('image'), upload_to='social/cite', null=True, blank=True,
25         help_text=_('Best image is exactly 975px wide and weights under 100kB.'))
26     image_shift = models.IntegerField(
27         _('shift'), null=True, blank=True,
28         help_text=_(u'Vertical shift, in percents. 0 means top, 100 is bottom. Default is 50%.'))
29     image_title = models.CharField(_('title'), max_length=255, null=True, blank=True)
30     image_author = models.CharField(_('author'), max_length=255, blank=True, null=True)
31     image_link = models.URLField(_('link'), blank=True, null=True)
32     image_license = models.CharField(_('license name'), max_length=255, blank=True, null=True)
33     image_license_link = models.URLField(_('license link'), blank=True, null=True)
34
35     class Meta:
36         ordering = ('vip', 'text')
37         verbose_name = _('cite')
38         verbose_name_plural = _('cites')
39
40     def __unicode__(self):
41         return u"%s: %s…" % (self.vip, self.text[:60])
42
43     def get_absolute_url(self):
44         """This is used for testing."""
45         return "%s?choose_cite=%d" % (reverse('main_page'), self.id)
46
47     def save(self, *args, **kwargs):
48         ret = super(Cite, self).save(*args, **kwargs)
49         self.flush_includes()
50         return ret
51
52     def flush_includes(self):
53         flush_ssi_includes([
54             template % (self.pk, lang)
55             for template in [
56                 '/ludzie/cite/%s.%s.html',
57                 '/ludzie/cite_main/%s.%s.html',
58             ]
59             for lang in [lc for (lc, _ln) in settings.LANGUAGES]] +
60             ['/ludzie/cite_info/%s.html' % self.pk])