[books] Clean up old page model and bug in page calc
This commit is contained in:
@ -397,97 +397,6 @@ class Book(LongPlayScrobblableMixin):
|
||||
return book
|
||||
|
||||
|
||||
class Page(TimeStampedModel):
|
||||
"""DEPRECATED, we need to migrate pages into page_data on scrobbles and move on"""
|
||||
|
||||
book = models.ForeignKey(Book, on_delete=models.CASCADE)
|
||||
number = models.IntegerField()
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
start_time = models.DateTimeField(**BNULL)
|
||||
end_time = models.DateTimeField(**BNULL)
|
||||
duration_seconds = models.IntegerField(**BNULL)
|
||||
|
||||
class Meta:
|
||||
unique_together = (
|
||||
"book",
|
||||
"number",
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return f"Page {self.number} of {self.book.pages} in {self.book.title}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.end_time and self.duration_seconds:
|
||||
self._set_end_time()
|
||||
|
||||
return super(Page, self).save(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def next(self):
|
||||
page = self.book.page_set.filter(number=self.number + 1).first()
|
||||
if not page:
|
||||
page = (
|
||||
self.book.page_set.filter(created__gt=self.created)
|
||||
.order_by("created")
|
||||
.first()
|
||||
)
|
||||
return page
|
||||
|
||||
@property
|
||||
def previous(self):
|
||||
page = self.book.page_set.filter(number=self.number - 1).first()
|
||||
if not page:
|
||||
page = (
|
||||
self.book.page_set.filter(created__lt=self.created)
|
||||
.order_by("-created")
|
||||
.first()
|
||||
)
|
||||
return page
|
||||
|
||||
@property
|
||||
def seconds_to_next_page(self) -> int:
|
||||
seconds = 999999 # Effectively infnity time as we have no next
|
||||
if not self.end_time:
|
||||
self._set_end_time()
|
||||
if self.next:
|
||||
seconds = (self.next.start_time - self.end_time).seconds
|
||||
return seconds
|
||||
|
||||
@property
|
||||
def is_scrobblable(self) -> bool:
|
||||
"""A page defines the start of a scrobble if the seconds to next page
|
||||
are greater than an hour, or 3600 seconds, and it's not a single page,
|
||||
so the next seconds to next_page is less than an hour as well.
|
||||
|
||||
As a special case, the first recorded page is a scrobble, so we establish
|
||||
when the book was started.
|
||||
|
||||
"""
|
||||
is_scrobblable = False
|
||||
over_an_hour_since_last_page = False
|
||||
if not self.previous:
|
||||
is_scrobblable = True
|
||||
|
||||
if self.previous:
|
||||
over_an_hour_since_last_page = (
|
||||
self.previous.seconds_to_next_page >= 3600
|
||||
)
|
||||
blip = self.seconds_to_next_page >= 3600
|
||||
|
||||
if over_an_hour_since_last_page and not blip:
|
||||
is_scrobblable = True
|
||||
return is_scrobblable
|
||||
|
||||
def _set_end_time(self) -> None:
|
||||
if self.end_time:
|
||||
return
|
||||
|
||||
self.end_time = self.start_time + timedelta(
|
||||
seconds=self.duration_seconds
|
||||
)
|
||||
self.save(update_fields=["end_time"])
|
||||
|
||||
|
||||
class Paper(LongPlayScrobblableMixin):
|
||||
"""Keeps track of Academic Papers"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user