2021-01-01 18:46:15 +00:00
|
|
|
''' progress in a book '''
|
|
|
|
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-01-14 03:45:36 +00:00
|
|
|
class ProgressMode(models.TextChoices):
|
|
|
|
PAGE = 'PG', 'page'
|
|
|
|
PERCENT = 'PCT', 'percent'
|
2021-01-01 18:46:15 +00:00
|
|
|
|
|
|
|
class ReadThrough(BookWyrmModel):
|
2021-01-14 03:45:36 +00:00
|
|
|
''' Store a read through a book in the database. '''
|
2021-01-01 18:46:15 +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-01-20 07:40:11 +00:00
|
|
|
validators=[validators.MinValueValidator(0)],
|
2021-01-01 18:46:15 +00:00
|
|
|
null=True,
|
|
|
|
blank=True)
|
2021-01-14 03:45:36 +00:00
|
|
|
progress_mode = models.CharField(
|
|
|
|
max_length=3,
|
|
|
|
choices=ProgressMode.choices,
|
|
|
|
default=ProgressMode.PAGE)
|
2021-01-01 18:46:15 +00:00
|
|
|
start_date = models.DateTimeField(
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
|
|
|
finish_date = models.DateTimeField(
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
''' update user active time '''
|
|
|
|
self.user.last_active_date = timezone.now()
|
|
|
|
self.user.save()
|
|
|
|
super().save(*args, **kwargs)
|
2021-01-14 03:45:36 +00:00
|
|
|
|
|
|
|
def create_update(self):
|
|
|
|
if self.progress:
|
|
|
|
return self.progressupdate_set.create(
|
|
|
|
user=self.user,
|
|
|
|
progress=self.progress,
|
|
|
|
mode=self.progress_mode)
|
|
|
|
|
|
|
|
class ProgressUpdate(BookWyrmModel):
|
|
|
|
''' Store progress through a book in the database. '''
|
|
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
2021-01-20 06:30:51 +00:00
|
|
|
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(
|
|
|
|
max_length=3,
|
|
|
|
choices=ProgressMode.choices,
|
|
|
|
default=ProgressMode.PAGE)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
''' update user active time '''
|
|
|
|
self.user.last_active_date = timezone.now()
|
|
|
|
self.user.save()
|
|
|
|
super().save(*args, **kwargs)
|