+import hashlib
+import json
from django.apps import apps
from django.conf import settings
from django.db import models
-from django.utils.translation import ugettext_lazy as _
+from django.template import Context, Template
+from django.utils.translation import gettext_lazy as _
from django.utils.timezone import now
-from .places import PLACES, PLACE_CHOICES
+from .places import PLACES, PLACE_CHOICES, STYLES
class Banner(models.Model):
place = models.SlugField(_('place'), choices=PLACE_CHOICES)
+ style = models.CharField(
+ _('style'), max_length=255, blank=True,
+ choices=STYLES,
+ help_text=_('Affects blackout.')
+ )
+ smallfont = models.BooleanField(_('small font'), default=False)
+ text_color = models.CharField(max_length=10, blank=True)
+ background_color = models.CharField(max_length=10, blank=True)
action_label = models.CharField(
_('action label'),
max_length=255, blank=True,
until = models.DateTimeField(_('until'), null=True, blank=True)
show_members = models.BooleanField(_('show members'), default=False)
staff_preview = models.BooleanField(_('staff preview'), default=False)
+ only_authenticated = models.BooleanField(_('only for authenticated users'), default=False)
class Meta:
verbose_name = _('banner')
def __str__(self):
return self.text
+ def get_text(self):
+ return Template(self.text).render(Context())
+
@classmethod
def choice(cls, place, request):
Membership = apps.get_model('club', 'Membership')
until__lt=n
).order_by('-priority', '?')
+ if not request.user.is_authenticated:
+ banners = banners.filter(only_authenticated=False)
+
if not request.user.is_staff:
banners = banners.filter(staff_preview=False)
- if request:
- if Membership.is_active_for(request.user):
- banners = banners.filter(show_members=True)
+ if Membership.is_active_for(request.user):
+ banners = banners.filter(show_members=True)
+
return banners
text_color = models.CharField(max_length=10, blank=True)
text = models.TextField(_('text'))
image = models.FileField(blank=True, upload_to='annoy/inserts/')
+
+
+class MediaInsertSet(models.Model):
+ file_format = models.CharField(max_length=8, choices=[
+ ('epub', 'epub'),
+ ('mobi', 'mobi'),
+ ])
+ etag = models.CharField(max_length=64, blank=True)
+
+ def update_etag(self):
+ self.etag = hashlib.sha1(json.dumps(self.get_texts()).encode('utf-8')).hexdigest()
+ self.save(update_fields=['etag'])
+
+ def get_texts(self):
+ return [t.text for t in self.mediainserttext_set.all()]
+
+ @classmethod
+ def get_for_format(cls, file_format):
+ return cls.objects.filter(file_format=file_format).first()
+
+ @classmethod
+ def get_texts_for(cls, file_format):
+ self = cls.get_for_format(file_format)
+ if self is None:
+ return []
+ return self.get_texts()
+
+
+class MediaInsertText(models.Model):
+ media_insert_set = models.ForeignKey(MediaInsertSet, models.CASCADE)
+ ordering = models.IntegerField()
+ text = models.TextField()
+
+ class Meta:
+ ordering = ('ordering',)
+
+
+from django.db.models.signals import post_save, post_delete
+from django.dispatch import receiver
+
+@receiver(post_delete, sender=MediaInsertText)
+@receiver(post_save, sender=MediaInsertText)
+def update_etag(sender, instance, **kwargs):
+ instance.media_insert_set.update_etag()