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 ugettext_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)
39 verbose_name = _('banner')
40 verbose_name_plural = _('banners')
41 ordering = ('place', '-priority',)
47 return Template(self.text).render(Context())
50 def choice(cls, place, request):
51 Membership = apps.get_model('club', 'Membership')
53 if hasattr(request, 'annoy_banner_exempt'):
54 return cls.objects.none()
57 assert place in PLACES, f"Banner place `{place}` must be defined in annoy.places."
60 banners = cls.objects.filter(
66 ).order_by('-priority', '?')
68 if not request.user.is_staff:
69 banners = banners.filter(staff_preview=False)
72 if Membership.is_active_for(request.user):
73 banners = banners.filter(show_members=True)
77 class DynamicTextInsert(models.Model):
78 paragraphs = models.IntegerField(_('pararaphs'))
79 url = models.CharField(max_length=1024)
82 verbose_name = _('dynamic insert')
83 verbose_name_plural = _('dynamic inserts')
84 ordering = ('paragraphs', )
87 return str(self.paragraphs)
90 def get_all(cls, request):
91 Membership = apps.get_model('club', 'Membership')
92 if Membership.is_active_for(request.user) and not request.user.is_staff:
93 return cls.objects.none()
94 return cls.objects.all()
98 return self.dynamictextinserttext_set.order_by('?').first()
101 class DynamicTextInsertText(models.Model):
102 insert = models.ForeignKey(DynamicTextInsert, models.CASCADE)
103 own_colors = models.BooleanField(default=False)
104 background_color = models.CharField(max_length=10, blank=True)
105 text_color = models.CharField(max_length=10, blank=True)
106 text = models.TextField(_('text'))
107 image = models.FileField(blank=True, upload_to='annoy/inserts/')