1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from random import randint
5 from django.db import models
6 from django.conf import settings
7 from django.core.exceptions import ValidationError
8 from django.core.urlresolvers import reverse
9 from django.utils.translation import ugettext_lazy as _
10 from ssify import flush_ssi_includes
11 from catalogue.models import Book
14 class BannerGroup(models.Model):
15 name = models.CharField(_('name'), max_length=255, unique=True)
16 created_at = models.DateTimeField(_('created at'), auto_now_add=True)
20 verbose_name = _('banner group')
21 verbose_name_plural = _('banner groups')
26 def get_absolute_url(self):
27 """This is used for testing."""
28 return "%s?banner_group=%d" % (reverse('main_page'), self.id)
31 banners = self.cite_set.all()
32 count = banners.count()
35 return banners[randint(0, count-1)]
38 class Cite(models.Model):
39 book = models.ForeignKey(Book, verbose_name=_('book'), null=True, blank=True)
40 text = models.TextField(_('text'), blank=True)
41 small = models.BooleanField(_('small'), default=False, help_text=_('Make this cite display smaller.'))
42 vip = models.CharField(_('VIP'), max_length=128, null=True, blank=True)
43 link = models.URLField(_('link'))
44 video = models.URLField(_('video'), blank=True)
45 picture = models.ImageField(_('picture'), blank=True)
47 sticky = models.BooleanField(_('sticky'), default=False, db_index=True,
48 help_text=_('Sticky cites will take precedense.'))
49 banner = models.BooleanField(_('banner'), default=False, help_text=_('Adjust size to image, ignore the text'))
51 image = models.ImageField(
52 _('image'), upload_to='social/cite', null=True, blank=True,
53 help_text=_('Best image is exactly 975px wide and weights under 100kB.'))
54 image_shift = models.IntegerField(
55 _('shift'), null=True, blank=True,
56 help_text=_(u'Vertical shift, in percents. 0 means top, 100 is bottom. Default is 50%.'))
57 image_title = models.CharField(_('title'), max_length=255, null=True, blank=True)
58 image_author = models.CharField(_('author'), max_length=255, blank=True, null=True)
59 image_link = models.URLField(_('link'), blank=True, null=True)
60 image_license = models.CharField(_('license name'), max_length=255, blank=True, null=True)
61 image_license_link = models.URLField(_('license link'), blank=True, null=True)
63 created_at = models.DateTimeField(_('created at'), auto_now_add=True)
64 group = models.ForeignKey(BannerGroup, verbose_name=_('group'), null=True, blank=True, on_delete=models.SET_NULL)
67 ordering = ('vip', 'text')
68 verbose_name = _('banner')
69 verbose_name_plural = _('banners')
72 return u"%s: %s…" % (self.vip, self.text[:60])
74 def get_absolute_url(self):
75 """This is used for testing."""
76 return "%s?choose_cite=%d" % (reverse('main_page'), self.id)
79 return self.video or self.picture
82 return self.vip or self.text or self.book
86 # TODO: move all banners to pictures.
94 pieces.append('small')
95 return '-'.join(pieces)
98 def save(self, *args, **kwargs):
99 ret = super(Cite, self).save(*args, **kwargs)
100 self.flush_includes()
103 def flush_includes(self):
105 template % (self.pk, lang)
107 '/ludzie/cite/%s.%s.html',
108 '/ludzie/cite_main/%s.%s.html',
110 for lang in [lc for (lc, _ln) in settings.LANGUAGES]] +
111 ['/ludzie/cite_info/%s.html' % self.pk])
114 class Carousel(models.Model):
115 slug = models.SlugField(_('slug'), unique=True)
119 verbose_name = _('carousel')
120 verbose_name_plural = _('carousels')
125 class CarouselItem(models.Model):
126 order = models.PositiveSmallIntegerField(_('order'), unique=True)
127 carousel = models.ForeignKey(Carousel, models.CASCADE, verbose_name=_('carousel'))
128 banner = models.ForeignKey(Cite, models.CASCADE, null=True, blank=True, verbose_name=_('banner'))
129 banner_group = models.ForeignKey(BannerGroup, models.CASCADE, null=True, blank=True, verbose_name=_('banner group'))
132 ordering = ('order',)
133 unique_together = [('carousel', 'order')]
134 verbose_name = _('carousel item')
135 verbose_name_plural = _('carousel items')
138 return str(self.banner or self.banner_group)
141 if not self.banner and not self.banner_group:
142 raise ValidationError(_('Either banner or banner group is required.'))
143 elif self.banner and self.banner_group:
144 raise ValidationError(_('Either banner or banner group is required.'))
146 def get_banner(self):
147 return self.banner or self.banner_group.get_banner()