2021-03-08 16:49:10 +00:00
|
|
|
""" progress in a book """
|
2021-08-11 18:19:06 +00:00
|
|
|
from django.core import validators
|
2022-01-09 00:38:52 +00:00
|
|
|
from django.core.cache import cache
|
2021-01-01 18:46:15 +00:00
|
|
|
from django.db import models
|
2021-08-11 18:19:06 +00:00
|
|
|
from django.db.models import F, Q
|
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-09-22 19:33:21 +00:00
|
|
|
is_active = models.BooleanField(default=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"""
|
2022-01-09 00:38:52 +00:00
|
|
|
cache.delete(f"latest_read_through-{self.user.id}-{self.book.id}")
|
2021-09-22 15:35:20 +00:00
|
|
|
self.user.update_active_date()
|
2021-09-22 22:59:48 +00:00
|
|
|
# an active readthrough must have an unset finish date
|
|
|
|
if self.finish_date:
|
|
|
|
self.is_active = 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-08-11 18:19:06 +00:00
|
|
|
class Meta:
|
|
|
|
"""Don't let readthroughs end before they start"""
|
|
|
|
|
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
|
|
|
check=Q(finish_date__gte=F("start_date")), name="chronology"
|
|
|
|
)
|
|
|
|
]
|
|
|
|
ordering = ("-start_date",)
|
|
|
|
|
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-09-22 15:35:20 +00:00
|
|
|
self.user.update_active_date()
|
2021-01-14 03:45:36 +00:00
|
|
|
super().save(*args, **kwargs)
|