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 action_label = models.CharField(
20 max_length=255, blank=True,
21 help_text=_('If empty, whole banner will serve as a link')
23 open_label = models.CharField(_('open label'), max_length=255, blank=True)
24 close_label = models.CharField(_('close label'), max_length=255, blank=True)
25 text = models.TextField(_('text'))
26 image = models.FileField(_('image'), upload_to='annoy/banners/', blank=True)
27 url = models.CharField(_('url'), max_length=1024)
28 priority = models.PositiveSmallIntegerField(
29 _('priority'), default=0,
30 help_text=_('Banners with higher priority come first.'))
31 since = models.DateTimeField(_('since'), null=True, blank=True)
32 until = models.DateTimeField(_('until'), null=True, blank=True)
33 show_members = models.BooleanField(_('show members'), default=False)
34 staff_preview = models.BooleanField(_('staff preview'), default=False)
37 verbose_name = _('banner')
38 verbose_name_plural = _('banners')
39 ordering = ('place', '-priority',)
45 return Template(self.text).render(Context())
48 def choice(cls, place, request):
49 Membership = apps.get_model('club', 'Membership')
51 if hasattr(request, 'annoy_banner_exempt'):
52 return cls.objects.none()
55 assert place in PLACES, f"Banner place `{place}` must be defined in annoy.places."
58 banners = cls.objects.filter(
64 ).order_by('-priority', '?')
66 if not request.user.is_staff:
67 banners = banners.filter(staff_preview=False)
70 if Membership.is_active_for(request.user):
71 banners = banners.filter(show_members=True)
75 class DynamicTextInsert(models.Model):
76 paragraphs = models.IntegerField(_('pararaphs'))
77 url = models.CharField(max_length=1024)
80 verbose_name = _('dynamic insert')
81 verbose_name_plural = _('dynamic inserts')
82 ordering = ('paragraphs', )
85 return str(self.paragraphs)
88 def get_all(cls, request):
89 Membership = apps.get_model('club', 'Membership')
90 if Membership.is_active_for(request.user) and not request.user.is_staff:
91 return cls.objects.none()
92 return cls.objects.all()
96 return self.dynamictextinserttext_set.order_by('?').first()
99 class DynamicTextInsertText(models.Model):
100 insert = models.ForeignKey(DynamicTextInsert, models.CASCADE)
101 own_colors = models.BooleanField(default=False)
102 background_color = models.CharField(max_length=10, blank=True)
103 text_color = models.CharField(max_length=10, blank=True)
104 text = models.TextField(_('text'))
105 image = models.FileField(blank=True, upload_to='annoy/inserts/')