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 _, string_concat
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 banner = models.BooleanField(_('banner'), default=False, help_text=string_concat(_('Adjust size to image, ignore the text'), '<br>(Przestarzałe; użyj funkcji "Obraz" w sekcji "Media box")'))
58 background_plain = models.BooleanField(_('plain background'), default=False)
59 background_color = models.CharField(_('background color'), max_length=32, blank=True)
60 image = models.ImageField(
61 _('image'), upload_to='social/cite', null=True, blank=True,
62 help_text=_('Best image is exactly 975px wide and weights under 100kB.'))
63 image_shift = models.IntegerField(
64 _('shift'), null=True, blank=True,
65 help_text=string_concat(_('Vertical shift, in percents. 0 means top, 100 is bottom. Default is 50%.'), '<br>(Przestarzałe; użyj obrazka o właściwych proporcjach;)'))
66 image_title = models.CharField(_('title'), max_length=255, null=True, blank=True)
67 image_author = models.CharField(_('author'), max_length=255, blank=True, null=True)
68 image_link = models.URLField(_('link'), blank=True, null=True)
69 image_license = models.CharField(_('license name'), max_length=255, blank=True, null=True)
70 image_license_link = models.URLField(_('license link'), blank=True, null=True)
72 created_at = models.DateTimeField(_('created at'), auto_now_add=True)
73 group = models.ForeignKey(BannerGroup, verbose_name=_('group'), null=True, blank=True, on_delete=models.SET_NULL)
76 ordering = ('vip', 'text')
77 verbose_name = _('banner')
78 verbose_name_plural = _('banners')
83 t.append(self.text[:60])
85 t.append('[ks.]'[:60])
86 t.append(self.link[:60])
88 t.append('vip: ' + self.vip)
95 def get_absolute_url(self):
96 """This is used for testing."""
97 return "%s?banner=%d" % (reverse('main_page'), self.id)
100 return self.video or self.picture
103 return self.vip or self.text or self.book
107 # TODO: move all banners to pictures.
113 pieces.append('text')
114 return '-'.join(pieces)
117 def save(self, *args, **kwargs):
118 ret = super(Cite, self).save(*args, **kwargs)
122 @cached_render('social/cite_promo.html')
129 def clear_cache(self):
130 clear_cached_renders(self.main_box)
133 class Carousel(models.Model):
134 slug = models.SlugField(_('slug'), unique=True)
138 verbose_name = _('carousel')
139 verbose_name_plural = _('carousels')
144 class CarouselItem(models.Model):
145 order = models.PositiveSmallIntegerField(_('order'), unique=True)
146 carousel = models.ForeignKey(Carousel, models.CASCADE, verbose_name=_('carousel'))
147 banner = models.ForeignKey(Cite, models.CASCADE, null=True, blank=True, verbose_name=_('banner'))
148 banner_group = models.ForeignKey(BannerGroup, models.CASCADE, null=True, blank=True, verbose_name=_('banner group'))
151 ordering = ('order',)
152 unique_together = [('carousel', 'order')]
153 verbose_name = _('carousel item')
154 verbose_name_plural = _('carousel items')
157 return str(self.banner or self.banner_group)
160 if not self.banner and not self.banner_group:
161 raise ValidationError(_('Either banner or banner group is required.'))
162 elif self.banner and self.banner_group:
163 raise ValidationError(_('Either banner or banner group is required.'))
165 def get_banner(self):
166 return self.banner or self.banner_group.get_banner()