1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
4 from oauthlib.common import urlencode, generate_token
5 from random import randint
6 from django.db import models
7 from django.conf import settings
8 from django.contrib.auth.models import User
9 from django.core.exceptions import ValidationError
10 from django.core.mail import send_mail
11 from django.urls import reverse
12 from catalogue.models import Book
13 from wolnelektury.utils import cached_render, clear_cached_renders
16 class BannerGroup(models.Model):
17 name = models.CharField('nazwa', max_length=255, unique=True)
18 created_at = models.DateTimeField('utworzona', auto_now_add=True)
22 verbose_name = 'grupa bannerów'
23 verbose_name_plural = 'grupy bannerów'
28 def get_absolute_url(self):
29 """This is used for testing."""
30 return "%s?banner_group=%d" % (reverse('main_page'), self.id)
33 banners = self.cite_set.all()
34 count = banners.count()
37 return banners[randint(0, count-1)]
40 class Cite(models.Model):
41 book = models.ForeignKey(Book, models.CASCADE, verbose_name='książka', null=True, blank=True)
42 text = models.TextField('tekst', blank=True)
43 small = models.BooleanField(
44 'mały', default=False, help_text='Sprawia, że cytat wyświetla się mniejszym fontem.')
45 vip = models.CharField('VIP', max_length=128, null=True, blank=True)
46 link = models.URLField('odnośnik')
47 video = models.URLField('wideo', blank=True)
48 picture = models.ImageField('ilustracja', blank=True,
49 help_text='Najlepsze wymiary: 975 x 315 z tekstem, 487 x 315 bez tekstu.')
50 picture_alt = models.CharField('alternatywny tekst ilustracji', max_length=255, blank=True)
51 picture_title = models.CharField('tytuł ilustracji', max_length=255, null=True, blank=True)
52 picture_author = models.CharField('autor ilustracji', max_length=255, blank=True, null=True)
53 picture_link = models.URLField('link ilustracji', blank=True, null=True)
54 picture_license = models.CharField('nazwa licencji ilustracji', max_length=255, blank=True, null=True)
55 picture_license_link = models.URLField('adres licencji ilustracji', blank=True, null=True)
57 sticky = models.BooleanField('przyklejony', default=False, db_index=True,
58 help_text='Przyklejone cytaty mają pierwszeństwo.')
59 background_plain = models.BooleanField('jednobarwne tło', default=False)
60 background_color = models.CharField('kolor tła', max_length=32, blank=True)
61 image = models.ImageField(
62 'obraz tła', upload_to='social/cite', null=True, blank=True,
63 help_text='Najlepsze tło ma wielkość 975 x 315 px i waży poniżej 100kB.')
64 image_title = models.CharField('tytuł obrazu tła', max_length=255, null=True, blank=True)
65 image_author = models.CharField('autor obrazu tła', max_length=255, blank=True, null=True)
66 image_link = models.URLField('link obrazu tła', blank=True, null=True)
67 image_license = models.CharField('nazwa licencji obrazu tła', max_length=255, blank=True, null=True)
68 image_license_link = models.URLField('adres licencji obrazu tła', blank=True, null=True)
70 created_at = models.DateTimeField('utworzony', auto_now_add=True)
71 group = models.ForeignKey(BannerGroup, verbose_name='grupa', null=True, blank=True, on_delete=models.SET_NULL)
74 ordering = ('vip', 'text')
75 verbose_name = 'banner'
76 verbose_name_plural = 'bannery'
81 t.append(self.text[:60])
83 t.append('[ks.]'[:60])
84 t.append(self.link[:60])
86 t.append('vip: ' + self.vip)
93 def get_absolute_url(self):
94 """This is used for testing."""
95 return "%s?banner=%d" % (reverse('main_page'), self.id)
98 return self.video or self.picture
101 return self.vip or self.text or self.book
108 pieces.append('text')
109 return '-'.join(pieces)
112 def save(self, *args, **kwargs):
113 ret = super(Cite, self).save(*args, **kwargs)
117 @cached_render('social/cite_promo.html')
124 def clear_cache(self):
125 clear_cached_renders(self.main_box)
128 class Carousel(models.Model):
129 placement = models.SlugField('miejsce', choices=[
131 ('main_2022', 'main 2022'),
133 priority = models.SmallIntegerField('priorytet', default=0)
134 language = models.CharField('język', max_length=2, blank=True, default='', choices=settings.LANGUAGES)
137 # ordering = ('placement', '-priority')
138 verbose_name = 'karuzela'
139 verbose_name_plural = 'karuzele'
142 return self.placement
145 def get(cls, placement):
146 carousel = cls.objects.filter(placement=placement).order_by('-priority', '?').first()
148 carousel = cls.objects.create(placement=placement)
152 class CarouselItem(models.Model):
153 order = models.PositiveSmallIntegerField('kolejność')
154 carousel = models.ForeignKey(Carousel, models.CASCADE, verbose_name='karuzela')
155 banner = models.ForeignKey(
156 Cite, models.CASCADE, null=True, blank=True, verbose_name='banner')
157 banner_group = models.ForeignKey(
158 BannerGroup, models.CASCADE, null=True, blank=True, verbose_name='grupa bannerów')
161 ordering = ('order',)
162 verbose_name = 'element karuzeli'
163 verbose_name_plural = 'elementy karuzeli'
166 return str(self.banner or self.banner_group)
169 if not self.banner and not self.banner_group:
170 raise ValidationError('Proszę wskazać banner albo grupę bannerów.')
171 elif self.banner and self.banner_group:
172 raise ValidationError('Proszę wskazać banner albo grupę bannerów.')
174 def get_banner(self):
175 return self.banner or self.banner_group.get_banner()
178 class UserConfirmation(models.Model):
179 user = models.ForeignKey(User, models.CASCADE)
180 created_at = models.DateTimeField(auto_now_add=True)
181 key = models.CharField(max_length=128, unique=True)
185 'Potwierdź konto w bibliotece Wolne Lektury',
186 f'https://beta.wolnelektury.pl/ludzie/potwierdz/{self.key}/',
187 settings.CONTACT_EMAIL,
193 user.is_active = True
198 def request(cls, user):