-# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
-# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
#
+from oauthlib.common import urlencode, generate_token
from random import randint
from django.db import models
from django.conf import settings
+from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
+from django.core.mail import send_mail
from django.urls import reverse
from catalogue.models import Book
from wolnelektury.utils import cached_render, clear_cached_renders
def get_banner(self):
return self.banner or self.banner_group.get_banner()
+
+
+class UserConfirmation(models.Model):
+ user = models.ForeignKey(User, models.CASCADE)
+ created_at = models.DateTimeField(auto_now_add=True)
+ key = models.CharField(max_length=128, unique=True)
+
+ def send(self):
+ send_mail(
+ 'Potwierdź konto w bibliotece Wolne Lektury',
+ f'https://beta.wolnelektury.pl/ludzie/potwierdz/{self.key}/',
+ settings.CONTACT_EMAIL,
+ [self.user.email]
+ )
+
+ def use(self):
+ user = self.user
+ user.is_active = True
+ user.save()
+ self.delete()
+
+ @classmethod
+ def request(cls, user):
+ cls.objects.create(
+ user=user,
+ key=generate_token()
+ ).send()
+
+
+
+class Progress(models.Model):
+ user = models.ForeignKey(User, models.CASCADE)
+ book = models.ForeignKey('catalogue.Book', models.CASCADE)
+ created_at = models.DateTimeField(auto_now_add=True)
+ updated_at = models.DateTimeField(auto_now=True)
+ deleted = models.BooleanField(default=False)
+ last_mode = models.CharField(max_length=64, choices=[
+ ('text', 'text'),
+ ('audio', 'audio'),
+ ])
+ text_percent = models.FloatField(null=True, blank=True)
+ text_anchor = models.CharField(max_length=64, blank=True)
+ audio_percent = models.FloatField(null=True, blank=True)
+ audio_timestamp = models.FloatField(null=True, blank=True)
+ implicit_text_percent = models.FloatField(null=True, blank=True)
+ implicit_text_anchor = models.CharField(max_length=64, blank=True)
+ implicit_audio_percent = models.FloatField(null=True, blank=True)
+ implicit_audio_timestamp = models.FloatField(null=True, blank=True)
+
+ class Meta:
+ unique_together = [('user', 'book')]
+
+ @classmethod
+ def sync(cls, user, slug, ts, data):
+ obj, _created = cls.objects.get_or_create(user=user, book__slug=slug)
+ if _created or obj.updated_at < ts:
+ if data is not None:
+ obj.deleted = False
+ for k, v in data.items():
+ setattr(obj, k, v)
+ else:
+ obj.deleted = True
+ obj.save()
+
+ def save(self, *args, **kwargs):
+ audio_l = self.book.get_audio_length()
+ if self.text_anchor:
+ self.text_percent = 33
+ if audio_l:
+ self.implicit_audio_percent = 40
+ self.implicit_audio_timestamp = audio_l * .4
+ if self.audio_timestamp:
+ if self.audio_timestamp > audio_l:
+ self.audio_timestamp = audio_l
+ if audio_l:
+ self.audio_percent = 100 * self.audio_timestamp / audio_l
+ self.implicit_text_percent = 60
+ self.implicit_text_anchor = 'f20'
+ return super().save(*args, **kwargs)