2021-03-08 16:49:10 +00:00
|
|
|
""" progress in a book """
|
2021-01-01 18:46:15 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
2021-01-20 07:40:11 +00:00
|
|
|
from django.core import validators
|
2021-01-01 18:46:15 +00:00
|
|
|
|
|
|
|
from .base_model import BookWyrmModel
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-14 03:45:36 +00:00
|
|
|
class ProgressMode(models.TextChoices):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""types of prgress available"""
|
2021-03-21 00:39:05 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
PAGE = "PG", "page"
|
|
|
|
PERCENT = "PCT", "percent"
|
|
|
|
|
2021-01-01 18:46:15 +00:00
|
|
|
|
|
|
|
class ReadThrough(BookWyrmModel):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""Store a read through a book in the database."""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey("User", on_delete=models.PROTECT)
|
|
|
|
book = models.ForeignKey("Edition", on_delete=models.PROTECT)
|
2021-01-14 03:45:36 +00:00
|
|
|
progress = models.IntegerField(
|
2021-03-08 16:49:10 +00:00
|
|
|
validators=[validators.MinValueValidator(0)], null=True, blank=True
|
|
|
|
)
|
2021-01-14 03:45:36 +00:00
|
|
|
progress_mode = models.CharField(
|
2021-03-08 16:49:10 +00:00
|
|
|
max_length=3, choices=ProgressMode.choices, default=ProgressMode.PAGE
|
|
|
|
)
|
|
|
|
start_date = models.DateTimeField(blank=True, null=True)
|
|
|
|
finish_date = models.DateTimeField(blank=True, null=True)
|
2021-01-01 18:46:15 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""update user active time"""
|
2021-01-01 18:46:15 +00:00
|
|
|
self.user.last_active_date = timezone.now()
|
2021-02-06 20:00:47 +00:00
|
|
|
self.user.save(broadcast=False)
|
2021-01-01 18:46:15 +00:00
|
|
|
super().save(*args, **kwargs)
|
2021-01-14 03:45:36 +00:00
|
|
|
|
|
|
|
def create_update(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""add update to the readthrough"""
|
2021-01-14 03:45:36 +00:00
|
|
|
if self.progress:
|
|
|
|
return self.progressupdate_set.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
user=self.user, progress=self.progress, mode=self.progress_mode
|
|
|
|
)
|
2021-03-21 00:18:18 +00:00
|
|
|
return None
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-14 03:45:36 +00:00
|
|
|
|
|
|
|
class ProgressUpdate(BookWyrmModel):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""Store progress through a book in the database."""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey("User", on_delete=models.PROTECT)
|
|
|
|
readthrough = models.ForeignKey("ReadThrough", on_delete=models.CASCADE)
|
2021-01-20 07:40:11 +00:00
|
|
|
progress = models.IntegerField(validators=[validators.MinValueValidator(0)])
|
2021-01-14 03:45:36 +00:00
|
|
|
mode = models.CharField(
|
2021-03-08 16:49:10 +00:00
|
|
|
max_length=3, choices=ProgressMode.choices, default=ProgressMode.PAGE
|
|
|
|
)
|
2021-01-14 03:45:36 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""update user active time"""
|
2021-01-14 03:45:36 +00:00
|
|
|
self.user.last_active_date = timezone.now()
|
2021-02-07 00:13:59 +00:00
|
|
|
self.user.save(broadcast=False)
|
2021-01-14 03:45:36 +00:00
|
|
|
super().save(*args, **kwargs)
|