3 from django.apps import apps
4 from django.conf import settings
5 from django.db import models
6 from django.template import Context, Template
7 from django.utils.translation import gettext_lazy as _
8 from django.utils.timezone import now
9 from .places import PLACES, PLACE_CHOICES, STYLES
12 class Banner(models.Model):
13 place = models.SlugField(_('place'), choices=PLACE_CHOICES)
14 style = models.CharField(
15 _('style'), max_length=255, blank=True,
17 help_text=_('Affects blackout.')
19 smallfont = models.BooleanField(_('small font'), default=False)
20 text_color = models.CharField(max_length=10, blank=True)
21 background_color = models.CharField(max_length=10, blank=True)
22 action_label = models.CharField(
24 max_length=255, blank=True,
25 help_text=_('If empty, whole banner will serve as a link')
27 open_label = models.CharField(_('open label'), max_length=255, blank=True)
28 close_label = models.CharField(_('close label'), max_length=255, blank=True)
29 text = models.TextField(_('text'))
30 image = models.FileField(_('image'), upload_to='annoy/banners/', blank=True)
31 url = models.CharField(_('url'), max_length=1024)
32 priority = models.PositiveSmallIntegerField(
33 _('priority'), default=0,
34 help_text=_('Banners with higher priority come first.'))
35 since = models.DateTimeField(_('since'), null=True, blank=True)
36 until = models.DateTimeField(_('until'), null=True, blank=True)
37 show_members = models.BooleanField(_('show members'), default=False)
38 staff_preview = models.BooleanField(_('staff preview'), default=False)
39 only_authenticated = models.BooleanField(_('only for authenticated users'), default=False)
42 verbose_name = _('banner')
43 verbose_name_plural = _('banners')
44 ordering = ('place', '-priority',)
50 return Template(self.text).render(Context())
53 def choice(cls, place, request):
54 Membership = apps.get_model('club', 'Membership')
56 if hasattr(request, 'annoy_banner_exempt'):
57 return cls.objects.none()
60 assert place in PLACES, f"Banner place `{place}` must be defined in annoy.places."
63 banners = cls.objects.filter(
69 ).order_by('-priority', '?')
71 if not request.user.is_authenticated:
72 banners = banners.filter(only_authenticated=False)
74 if not request.user.is_staff:
75 banners = banners.filter(staff_preview=False)
77 if Membership.is_active_for(request.user):
78 banners = banners.filter(show_members=True)
83 class DynamicTextInsert(models.Model):
84 paragraphs = models.IntegerField(_('pararaphs'))
85 url = models.CharField(max_length=1024)
88 verbose_name = _('dynamic insert')
89 verbose_name_plural = _('dynamic inserts')
90 ordering = ('paragraphs', )
93 return str(self.paragraphs)
96 def get_all(cls, request):
97 Membership = apps.get_model('club', 'Membership')
98 if Membership.is_active_for(request.user) and not request.user.is_staff:
99 return cls.objects.none()
100 return cls.objects.all()
104 return self.dynamictextinserttext_set.order_by('?').first()
107 class DynamicTextInsertText(models.Model):
108 insert = models.ForeignKey(DynamicTextInsert, models.CASCADE)
109 own_colors = models.BooleanField(default=False)
110 background_color = models.CharField(max_length=10, blank=True)
111 text_color = models.CharField(max_length=10, blank=True)
112 text = models.TextField(_('text'))
113 image = models.FileField(blank=True, upload_to='annoy/inserts/')
116 class MediaInsertSet(models.Model):
117 file_format = models.CharField(max_length=8, choices=[
121 etag = models.CharField(max_length=64, blank=True)
123 def update_etag(self):
124 self.etag = hashlib.sha1(json.dumps(self.get_texts()).encode('utf-8')).hexdigest()
125 self.save(update_fields=['etag'])
128 return [t.text for t in self.mediainserttext_set.all()]
131 def get_for_format(cls, file_format):
132 return cls.objects.filter(file_format=file_format).first()
135 def get_texts_for(cls, file_format):
136 self = cls.get_for_format(file_format)
139 return self.get_texts()
142 class MediaInsertText(models.Model):
143 media_insert_set = models.ForeignKey(MediaInsertSet, models.CASCADE)
144 ordering = models.IntegerField()
145 text = models.TextField()
148 ordering = ('ordering',)
151 from django.db.models.signals import post_save, post_delete
152 from django.dispatch import receiver
154 @receiver(post_delete, sender=MediaInsertText)
155 @receiver(post_save, sender=MediaInsertText)
156 def update_etag(sender, instance, **kwargs):
157 instance.media_insert_set.update_etag()