c8fdf2dadd412077f15e1ff957ca52ce5663bd15
[wolnelektury.git] / src / annoy / models.py
1 from django.apps import apps
2 from django.conf import settings
3 from django.db import models
4 from django.template import Context, Template
5 from django.utils.translation import gettext_lazy as _
6 from django.utils.timezone import now
7 from .places import PLACES, PLACE_CHOICES, STYLES
8
9
10 class Banner(models.Model):
11     place = models.SlugField(_('place'), choices=PLACE_CHOICES)
12     style = models.CharField(
13         _('style'), max_length=255, blank=True,
14         choices=STYLES,
15         help_text=_('Affects blackout.')
16     )
17     smallfont = models.BooleanField(_('small font'), default=False)
18     text_color = models.CharField(max_length=10, blank=True)
19     background_color = models.CharField(max_length=10, blank=True)
20     action_label = models.CharField(
21         _('action label'),
22         max_length=255, blank=True,
23         help_text=_('If empty, whole banner will serve as a link')
24     )
25     open_label = models.CharField(_('open label'), max_length=255, blank=True)
26     close_label = models.CharField(_('close label'), max_length=255, blank=True)
27     text = models.TextField(_('text'))
28     image = models.FileField(_('image'), upload_to='annoy/banners/', blank=True)
29     url = models.CharField(_('url'), max_length=1024)
30     priority = models.PositiveSmallIntegerField(
31         _('priority'), default=0,
32         help_text=_('Banners with higher priority come first.'))
33     since = models.DateTimeField(_('since'), null=True, blank=True)
34     until = models.DateTimeField(_('until'), null=True, blank=True)
35     show_members = models.BooleanField(_('show members'), default=False)
36     staff_preview = models.BooleanField(_('staff preview'), default=False)
37     only_authenticated = models.BooleanField(_('only for authenticated users'), default=False)
38
39     class Meta:
40         verbose_name = _('banner')
41         verbose_name_plural = _('banners')
42         ordering = ('place', '-priority',)
43
44     def __str__(self):
45         return self.text
46
47     def get_text(self):
48         return Template(self.text).render(Context())
49
50     @classmethod
51     def choice(cls, place, request):
52         Membership = apps.get_model('club', 'Membership')
53
54         if hasattr(request, 'annoy_banner_exempt'):
55             return cls.objects.none()
56
57         if settings.DEBUG:
58             assert place in PLACES, f"Banner place `{place}` must be defined in annoy.places."
59
60         n = now()
61         banners = cls.objects.filter(
62             place=place
63         ).exclude(
64             since__gt=n
65         ).exclude(
66             until__lt=n
67         ).order_by('-priority', '?')
68
69         if not request.user.is_authenticated:
70             banners = banners.filter(only_authenticated=False)
71
72         if not request.user.is_staff:
73             banners = banners.filter(staff_preview=False)
74
75         if Membership.is_active_for(request.user):
76             banners = banners.filter(show_members=True)
77
78         return banners
79
80
81 class DynamicTextInsert(models.Model):
82     paragraphs = models.IntegerField(_('pararaphs'))
83     url = models.CharField(max_length=1024)
84
85     class Meta:
86         verbose_name = _('dynamic insert')
87         verbose_name_plural = _('dynamic inserts')
88         ordering = ('paragraphs', )
89
90     def __str__(self):
91         return str(self.paragraphs)
92
93     @classmethod
94     def get_all(cls, request):
95         Membership = apps.get_model('club', 'Membership')
96         if Membership.is_active_for(request.user) and not request.user.is_staff:
97             return cls.objects.none()
98         return cls.objects.all()
99
100
101     def choose(self):
102         return self.dynamictextinserttext_set.order_by('?').first()
103
104
105 class DynamicTextInsertText(models.Model):
106     insert = models.ForeignKey(DynamicTextInsert, models.CASCADE)
107     own_colors = models.BooleanField(default=False)
108     background_color = models.CharField(max_length=10, blank=True)
109     text_color = models.CharField(max_length=10, blank=True)
110     text = models.TextField(_('text'))
111     image = models.FileField(blank=True, upload_to='annoy/inserts/')