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(choices=PLACE_CHOICES)
11 action_label = models.CharField(
12 max_length=255, blank=True,
15 open_label = models.CharField(max_length=255, blank=True)
16 close_label = models.CharField(max_length=255, blank=True)
17 text = models.TextField()
18 url = models.CharField(max_length=1024)
19 priority = models.PositiveSmallIntegerField(default=0)
20 since = models.DateTimeField(null=True, blank=True)
21 until = models.DateTimeField(null=True, blank=True)
22 show_members = models.BooleanField(default=False)
23 staff_preview = models.BooleanField(default=False)
26 verbose_name = _('banner')
27 verbose_name_plural = _('banners')
28 ordering = ('place', '-priority',)
34 def choice(cls, place, request):
35 Membership = apps.get_model('club', 'Membership')
37 if hasattr(request, 'annoy_banner_exempt'):
38 return cls.objects.none()
41 assert place in PLACES, "Banner place `{}` must be defined in annoy.places.".format(place)
44 banners = cls.objects.filter(
50 ).order_by('-priority', '?')
52 if not request.user.is_staff:
53 banners = banners.filter(staff_preview=False)
56 if Membership.is_active_for(request.user):
57 banners = banners.filter(show_members=True)
61 class DynamicTextInsert(models.Model):
62 paragraphs = models.IntegerField(_('pararaphs'))
63 url = models.CharField(max_length=1024)
66 verbose_name = _('dynamic insert')
67 verbose_name_plural = _('dynamic inserts')
68 ordering = ('paragraphs', )
71 return str(self.paragraphs)
74 return self.dynamictextinserttext_set.order_by('?').first()
77 class DynamicTextInsertText(models.Model):
78 insert = models.ForeignKey(DynamicTextInsert, models.CASCADE)
79 background_color = models.CharField(max_length=10, blank=True)
80 text_color = models.CharField(max_length=10, blank=True)
81 text = models.TextField(_('text'))
82 image = models.FileField(blank=True)