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
10 class Banner(models.Model):
11 place = models.SlugField(_('place'), choices=PLACE_CHOICES)
12 style = models.CharField(
13 _('style'), max_length=255, blank=True,
15 help_text=_('Affects blackout.')
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(
22 max_length=255, blank=True,
23 help_text=_('If empty, whole banner will serve as a link')
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)
40 verbose_name = _('banner')
41 verbose_name_plural = _('banners')
42 ordering = ('place', '-priority',)
48 return Template(self.text).render(Context())
51 def choice(cls, place, request):
52 Membership = apps.get_model('club', 'Membership')
54 if hasattr(request, 'annoy_banner_exempt'):
55 return cls.objects.none()
58 assert place in PLACES, f"Banner place `{place}` must be defined in annoy.places."
61 banners = cls.objects.filter(
67 ).order_by('-priority', '?')
69 if not request.user.is_authenticated:
70 banners = banners.filter(only_authenticated=False)
72 if not request.user.is_staff:
73 banners = banners.filter(staff_preview=False)
75 if Membership.is_active_for(request.user):
76 banners = banners.filter(show_members=True)
81 class DynamicTextInsert(models.Model):
82 paragraphs = models.IntegerField(_('pararaphs'))
83 url = models.CharField(max_length=1024)
86 verbose_name = _('dynamic insert')
87 verbose_name_plural = _('dynamic inserts')
88 ordering = ('paragraphs', )
91 return str(self.paragraphs)
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()
102 return self.dynamictextinserttext_set.order_by('?').first()
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/')