3 from django.apps import apps
4 from django.conf import settings
5 from django.db import models
6 from django.template import Context, Template
7 from django.utils.timezone import now
8 from .places import PLACES, PLACE_CHOICES, STYLES
11 class Banner(models.Model):
12 place = models.SlugField('miejsce', choices=PLACE_CHOICES)
13 style = models.CharField(
14 'styl', max_length=255, blank=True,
16 help_text='Dotyczy blackoutu.'
18 smallfont = models.BooleanField('mały font', default=False)
19 text_color = models.CharField(max_length=10, blank=True)
20 background_color = models.CharField(max_length=10, blank=True)
21 action_label = models.CharField(
23 max_length=255, blank=True,
24 help_text='Jeśli pusta, cały banner będzie służył jako link.'
26 open_label = models.CharField('etykieta otwierania', max_length=255, blank=True)
27 close_label = models.CharField('etykieta zamykania', max_length=255, blank=True)
28 text = models.TextField('tekst')
29 image = models.FileField('obraz', upload_to='annoy/banners/', blank=True)
30 url = models.CharField('URL', max_length=1024)
31 priority = models.PositiveSmallIntegerField(
32 'priorytet', default=0,
33 help_text='Bannery z wyższym priorytetem mają pierwszeństwo.')
34 since = models.DateTimeField('od', null=True, blank=True)
35 until = models.DateTimeField('do', null=True, blank=True)
36 target = models.IntegerField('cel', null=True, blank=True)
37 progress = models.IntegerField('postęp', null=True, blank=True)
38 show_members = models.BooleanField('widoczny dla członków klubu', default=False)
39 staff_preview = models.BooleanField('podgląd tylko dla zespołu', default=False)
40 only_authenticated = models.BooleanField('tylko dla zalogowanych', default=False)
43 verbose_name = 'banner'
44 verbose_name_plural = 'bannery'
45 ordering = ('place', '-priority',)
51 return Template(self.text).render(Context())
54 def choice(cls, place, request, exemptions=True):
55 Membership = apps.get_model('club', 'Membership')
57 if exemptions and hasattr(request, 'annoy_banner_exempt'):
58 return cls.objects.none()
61 assert place in PLACES, f"Banner place `{place}` must be defined in annoy.places."
64 banners = cls.objects.filter(
70 ).order_by('-priority', '?')
72 if not request.user.is_authenticated:
73 banners = banners.filter(only_authenticated=False)
75 if not request.user.is_staff:
76 banners = banners.filter(staff_preview=False)
78 if Membership.is_active_for(request.user):
79 banners = banners.filter(show_members=True)
84 def progress_percent(self):
87 return (self.progress or 0) / self.target * 100
89 def update_progress(self):
90 # Total of new payments during the action.
91 # This definition will need to change for longer timespans.
92 if not self.since or not self.until or not self.target:
94 Schedule = apps.get_model('club', 'Schedule')
95 self.progress = Schedule.objects.filter(
96 payed_at__gte=self.since,
97 payed_at__lte=self.until,
98 ).aggregate(c=models.Sum('amount'))['c']
99 self.save(update_fields=['progress'])
102 def update_all_progress(cls):
103 for obj in cls.objects.exclude(target=None):
104 obj.update_progress()
107 class DynamicTextInsert(models.Model):
108 paragraphs = models.IntegerField('akapity')
109 url = models.CharField(max_length=1024)
112 verbose_name = 'dynamiczna wstawka'
113 verbose_name_plural = 'dynamiczne wstawki'
114 ordering = ('paragraphs', )
117 return str(self.paragraphs)
120 def get_all(cls, request):
121 Membership = apps.get_model('club', 'Membership')
122 if Membership.is_active_for(request.user) and not request.user.is_staff:
123 return cls.objects.none()
124 return cls.objects.all()
128 return self.dynamictextinserttext_set.order_by('?').first()
131 class DynamicTextInsertText(models.Model):
132 insert = models.ForeignKey(DynamicTextInsert, models.CASCADE)
133 own_colors = models.BooleanField(default=False)
134 background_color = models.CharField(max_length=10, blank=True)
135 text_color = models.CharField(max_length=10, blank=True)
136 text = models.TextField('tekst')
137 image = models.FileField(blank=True, upload_to='annoy/inserts/')
140 class MediaInsertSet(models.Model):
141 file_format = models.CharField(max_length=8, choices=[
146 etag = models.CharField(max_length=64, blank=True)
148 def update_etag(self):
149 self.etag = hashlib.sha1(json.dumps(self.get_texts()).encode('utf-8')).hexdigest()
150 self.save(update_fields=['etag'])
153 return [t.text for t in self.mediainserttext_set.all()]
156 def get_for_format(cls, file_format):
157 return cls.objects.filter(file_format=file_format).first()
160 def get_texts_for(cls, file_format):
161 self = cls.get_for_format(file_format)
164 return self.get_texts()
167 class MediaInsertText(models.Model):
168 media_insert_set = models.ForeignKey(MediaInsertSet, models.CASCADE)
169 ordering = models.IntegerField()
170 text = models.TextField()
173 ordering = ('ordering',)