moviewyrm/bookwyrm/models/readthrough.py

60 lines
1.9 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" progress in a book """
from django.db import models
from django.utils import timezone
from django.core import validators
from .base_model import BookWyrmModel
2021-03-08 16:49:10 +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"
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)
progress = models.IntegerField(
2021-03-08 16:49:10 +00:00
validators=[validators.MinValueValidator(0)], null=True, blank=True
)
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)
def save(self, *args, **kwargs):
2021-04-26 16:15:42 +00:00
"""update user active time"""
self.user.last_active_date = timezone.now()
self.user.save(broadcast=False)
super().save(*args, **kwargs)
def create_update(self):
2021-04-26 16:15:42 +00:00
"""add update to the readthrough"""
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
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)
progress = models.IntegerField(validators=[validators.MinValueValidator(0)])
mode = models.CharField(
2021-03-08 16:49:10 +00:00
max_length=3, choices=ProgressMode.choices, default=ProgressMode.PAGE
)
def save(self, *args, **kwargs):
2021-04-26 16:15:42 +00:00
"""update user active time"""
self.user.last_active_date = timezone.now()
2021-02-07 00:13:59 +00:00
self.user.save(broadcast=False)
super().save(*args, **kwargs)