1 from django.apps import apps
2 from django.conf import settings
3 from django.db import models
4 from django.utils.translation import ugettext_lazy as _
5 from django.utils.timezone import now
6 from .places import PLACES, PLACE_CHOICES
9 class Banner(models.Model):
10 place = models.SlugField(_('place'), choices=PLACE_CHOICES)
11 action_label = models.CharField(
13 max_length=255, blank=True,
14 help_text=_('If empty, whole banner will serve as a link')
16 open_label = models.CharField(_('open label'), max_length=255, blank=True)
17 close_label = models.CharField(_('close label'), max_length=255, blank=True)
18 text = models.TextField(_('text'))
19 image = models.FileField(_('image'), upload_to='annoy/banners/', blank=True)
20 url = models.CharField(_('url'), max_length=1024)
21 priority = models.PositiveSmallIntegerField(
22 _('priority'), default=0,
23 help_text=_('Banners with higher priority come first.'))
24 since = models.DateTimeField(_('since'), null=True, blank=True)
25 until = models.DateTimeField(_('until'), null=True, blank=True)
26 show_members = models.BooleanField(_('show members'), default=False)
27 staff_preview = models.BooleanField(_('staff preview'), default=False)
30 verbose_name = _('banner')
31 verbose_name_plural = _('banners')
32 ordering = ('place', '-priority',)
38 def choice(cls, place, request):
39 Membership = apps.get_model('club', 'Membership')
41 if hasattr(request, 'annoy_banner_exempt'):
42 return cls.objects.none()
45 assert place in PLACES, "Banner place `{}` must be defined in annoy.places.".format(place)
48 banners = cls.objects.filter(
54 ).order_by('-priority', '?')
56 if not request.user.is_staff:
57 banners = banners.filter(staff_preview=False)
60 if Membership.is_active_for(request.user):
61 banners = banners.filter(show_members=True)
65 class DynamicTextInsert(models.Model):
66 paragraphs = models.IntegerField(_('pararaphs'))
67 url = models.CharField(max_length=1024)
70 verbose_name = _('dynamic insert')
71 verbose_name_plural = _('dynamic inserts')
72 ordering = ('paragraphs', )
75 return str(self.paragraphs)
78 def get_all(cls, request):
79 Membership = apps.get_model('club', 'Membership')
80 if Membership.is_active_for(request.user) and not request.user.is_staff:
81 return cls.objects.none()
82 return cls.objects.all()
86 return self.dynamictextinserttext_set.order_by('?').first()
89 class DynamicTextInsertText(models.Model):
90 insert = models.ForeignKey(DynamicTextInsert, models.CASCADE)
91 own_colors = models.BooleanField(default=False)
92 background_color = models.CharField(max_length=10, blank=True)
93 text_color = models.CharField(max_length=10, blank=True)
94 text = models.TextField(_('text'))
95 image = models.FileField(blank=True, upload_to='annoy/inserts/')