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,
17 smallfont = models.BooleanField('mały font', default=False)
18 text_color = models.CharField(max_length=10, blank=True)
19 background_color = models.CharField(max_length=10, blank=True)
20 action_label = models.CharField(
22 max_length=255, blank=True,
23 help_text='Jeśli pusta, cały banner będzie służył jako link.'
25 open_label = models.CharField('etykieta otwierania', max_length=255, blank=True)
26 close_label = models.CharField('etykieta zamykania', max_length=255, blank=True)
27 text = models.TextField('tekst')
28 image = models.FileField('obraz', upload_to='annoy/banners/', blank=True)
29 url = models.CharField('URL', max_length=1024)
30 priority = models.PositiveSmallIntegerField(
31 'priorytet', default=0,
32 help_text='Bannery z wyższym priorytetem mają pierwszeństwo.')
33 since = models.DateTimeField('od', null=True, blank=True)
34 until = models.DateTimeField('do', null=True, blank=True)
35 target = models.IntegerField('cel', null=True, blank=True)
36 progress = models.IntegerField('postęp', null=True, blank=True)
37 show_members = models.BooleanField('widoczny dla członków klubu', default=False)
38 staff_preview = models.BooleanField('podgląd tylko dla zespołu', default=False)
39 only_authenticated = models.BooleanField('tylko dla zalogowanych', default=False)
42 verbose_name = 'banner'
43 verbose_name_plural = 'bannery'
44 ordering = ('place', '-priority',)
50 return Template(self.text).render(Context())
53 def choice(cls, place, request, exemptions=True):
54 Membership = apps.get_model('club', 'Membership')
56 if exemptions and hasattr(request, 'annoy_banner_exempt'):
57 return cls.objects.none()
60 assert place in PLACES, f"Banner place `{place}` must be defined in annoy.places."
63 banners = cls.objects.filter(
69 ).order_by('-priority', '?')
71 if not request.user.is_authenticated:
72 banners = banners.filter(only_authenticated=False)
74 if not request.user.is_staff:
75 banners = banners.filter(staff_preview=False)
77 if Membership.is_active_for(request.user):
78 banners = banners.filter(show_members=True)
83 def progress_percent(self):
86 return (self.progress or 0) / self.target * 100
88 def update_progress(self):
89 # Total of new payments during the action.
90 # This definition will need to change for longer timespans.
91 if not self.since or not self.until or not self.target:
93 Schedule = apps.get_model('club', 'Schedule')
94 self.progress = Schedule.objects.filter(
95 payed_at__gte=self.since,
96 payed_at__lte=self.until,
97 ).aggregate(c=models.Sum('amount'))['c']
98 self.save(update_fields=['progress'])
101 def update_all_progress(cls):
102 for obj in cls.objects.exclude(target=None):
103 obj.update_progress()
106 class DynamicTextInsert(models.Model):
107 paragraphs = models.IntegerField('akapity')
108 url = models.CharField(max_length=1024)
111 verbose_name = 'dynamiczna wstawka'
112 verbose_name_plural = 'dynamiczne wstawki'
113 ordering = ('paragraphs', )
116 return str(self.paragraphs)
119 def get_all(cls, request):
120 Membership = apps.get_model('club', 'Membership')
121 if Membership.is_active_for(request.user) and not request.user.is_staff:
122 return cls.objects.none()
123 return cls.objects.all()
127 return self.dynamictextinserttext_set.order_by('?').first()
130 class DynamicTextInsertText(models.Model):
131 insert = models.ForeignKey(DynamicTextInsert, models.CASCADE)
132 own_colors = models.BooleanField(default=False)
133 background_color = models.CharField(max_length=10, blank=True)
134 text_color = models.CharField(max_length=10, blank=True)
135 text = models.TextField('tekst')
136 image = models.FileField(blank=True, upload_to='annoy/inserts/')
139 class MediaInsertSet(models.Model):
140 file_format = models.CharField(max_length=8, choices=[
145 etag = models.CharField(max_length=64, blank=True)
147 def update_etag(self):
148 self.etag = hashlib.sha1(json.dumps(self.get_texts()).encode('utf-8')).hexdigest()
149 self.save(update_fields=['etag'])
152 return [t.text for t in self.mediainserttext_set.all()]
155 def get_for_format(cls, file_format):
156 return cls.objects.filter(file_format=file_format).first()
159 def get_texts_for(cls, file_format):
160 self = cls.get_for_format(file_format)
163 return self.get_texts()
166 class MediaInsertText(models.Model):
167 media_insert_set = models.ForeignKey(MediaInsertSet, models.CASCADE)
168 ordering = models.IntegerField()
169 text = models.TextField()
172 ordering = ('ordering',)