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
|
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 django.utils import timezone
|
|
|
|
|
|
|
|
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-08-03 16:12:22 +00:00
|
|
|
self.user.save(broadcast=False, update_fields=["last_active_date"])
|
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-01-14 03:45:36 +00:00
|
|
|
self.user.last_active_date = timezone.now()
|
2021-08-03 16:12:22 +00:00
|
|
|
self.user.save(broadcast=False, update_fields=["last_active_date"])
|
2021-01-14 03:45:36 +00:00
|
|
|
super().save(*args, **kwargs)
|