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.urls import reverse
9 from django.utils.translation import ugettext_lazy as _
10 from catalogue.models import Book
11 from wolnelektury.utils import cached_render, clear_cached_renders
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, models.CASCADE, 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,
46 help_text='Najlepsze wymiary: 975 x 315 z tekstem, 487 x 315 bez tekstu.')
47 picture_alt = models.CharField(_('picture alternative text'), max_length=255, blank=True)
48 picture_title = models.CharField(_('picture title'), max_length=255, null=True, blank=True)
49 picture_author = models.CharField(_('picture author'), max_length=255, blank=True, null=True)
50 picture_link = models.URLField(_('picture link'), blank=True, null=True)
51 picture_license = models.CharField(_('picture license name'), max_length=255, blank=True, null=True)
52 picture_license_link = models.URLField(_('picture license link'), blank=True, null=True)
54 sticky = models.BooleanField(_('sticky'), default=False, db_index=True,
55 help_text=_('Sticky cites will take precedense.'))
56 background_plain = models.BooleanField(_('plain background'), default=False)
57 background_color = models.CharField(_('background color'), max_length=32, blank=True)
58 image = models.ImageField(
59 _('background image'), upload_to='social/cite', null=True, blank=True,
60 help_text=_('Best background is 975 x 315 px and under 100kB.'))
61 image_title = models.CharField(_('background title'), max_length=255, null=True, blank=True)
62 image_author = models.CharField(_('background author'), max_length=255, blank=True, null=True)
63 image_link = models.URLField(_('background link'), blank=True, null=True)
64 image_license = models.CharField(_('background license name'), max_length=255, blank=True, null=True)
65 image_license_link = models.URLField(_('background license link'), blank=True, null=True)
67 created_at = models.DateTimeField(_('created at'), auto_now_add=True)
68 group = models.ForeignKey(BannerGroup, verbose_name=_('group'), null=True, blank=True, on_delete=models.SET_NULL)
71 ordering = ('vip', 'text')
72 verbose_name = _('banner')
73 verbose_name_plural = _('banners')
78 t.append(self.text[:60])
80 t.append('[ks.]'[:60])
81 t.append(self.link[:60])
83 t.append('vip: ' + self.vip)
90 def get_absolute_url(self):
91 """This is used for testing."""
92 return "%s?banner=%d" % (reverse('main_page'), self.id)
95 return self.video or self.picture
98 return self.vip or self.text or self.book
105 pieces.append('text')
106 return '-'.join(pieces)
109 def save(self, *args, **kwargs):
110 ret = super(Cite, self).save(*args, **kwargs)
114 @cached_render('social/cite_promo.html')
121 def clear_cache(self):
122 clear_cached_renders(self.main_box)
125 class Carousel(models.Model):
126 slug = models.SlugField(_('slug'), unique=True)
130 verbose_name = _('carousel')
131 verbose_name_plural = _('carousels')
136 class CarouselItem(models.Model):
137 order = models.PositiveSmallIntegerField(_('order'), unique=True)
138 carousel = models.ForeignKey(Carousel, models.CASCADE, verbose_name=_('carousel'))
139 banner = models.ForeignKey(Cite, models.CASCADE, null=True, blank=True, verbose_name=_('banner'))
140 banner_group = models.ForeignKey(BannerGroup, models.CASCADE, null=True, blank=True, verbose_name=_('banner group'))
143 ordering = ('order',)
144 unique_together = [('carousel', 'order')]
145 verbose_name = _('carousel item')
146 verbose_name_plural = _('carousel items')
149 return str(self.banner or self.banner_group)
152 if not self.banner and not self.banner_group:
153 raise ValidationError(_('Either banner or banner group is required.'))
154 elif self.banner and self.banner_group:
155 raise ValidationError(_('Either banner or banner group is required.'))
157 def get_banner(self):
158 return self.banner or self.banner_group.get_banner()