[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
|
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):
|
class Paper(LongPlayScrobblableMixin):
|
||||||
"""Keeps track of Academic Papers"""
|
"""Keeps track of Academic Papers"""
|
||||||
|
|
||||||
|
|||||||
@ -1,63 +0,0 @@
|
|||||||
from books.models import Book
|
|
||||||
from django.core.management.base import BaseCommand
|
|
||||||
from scrobbles.models import ScrobbledPage
|
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
|
||||||
def add_arguments(self, parser):
|
|
||||||
parser.add_argument(
|
|
||||||
"--commit",
|
|
||||||
action="store_true",
|
|
||||||
help="Actually populate data",
|
|
||||||
)
|
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
|
||||||
dry_run = True
|
|
||||||
if options["commit"]:
|
|
||||||
dry_run = False
|
|
||||||
|
|
||||||
pages_to_create = []
|
|
||||||
associated_scrobble = None
|
|
||||||
last_scrobble = None
|
|
||||||
for book in Book.objects.all():
|
|
||||||
for page in book.page_set.all().order_by("number"):
|
|
||||||
notes = ""
|
|
||||||
last_scrobble = associated_scrobble
|
|
||||||
if (
|
|
||||||
not associated_scrobble
|
|
||||||
or page.number > associated_scrobble.book_pages_read
|
|
||||||
):
|
|
||||||
associated_scrobble = page.user.scrobble_set.filter(
|
|
||||||
book=page.book,
|
|
||||||
timestamp__gte=page.start_time,
|
|
||||||
timestamp__lte=page.end_time,
|
|
||||||
).first()
|
|
||||||
|
|
||||||
if (
|
|
||||||
last_scrobble
|
|
||||||
and not associated_scrobble
|
|
||||||
and page.number > last_scrobble.book_pages_read
|
|
||||||
):
|
|
||||||
associated_scrobble = last_scrobble
|
|
||||||
notes = f"Extrapolated reading from scrobble {associated_scrobble.id}"
|
|
||||||
|
|
||||||
pages_to_create.append(
|
|
||||||
ScrobbledPage(
|
|
||||||
scrobble=associated_scrobble,
|
|
||||||
number=page.number,
|
|
||||||
start_time=page.start_time,
|
|
||||||
end_time=page.end_time,
|
|
||||||
duration_seconds=page.duration_seconds,
|
|
||||||
notes=notes,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
pages_to_move_len = len(pages_to_create)
|
|
||||||
if dry_run:
|
|
||||||
print(
|
|
||||||
f"Found {pages_to_move_len} to migrate. Use --commit to move them"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
ScrobbledPage.objects.bulk_create(pages_to_create)
|
|
||||||
print(f"Migrated {pages_to_move_len} generic pages to scrobbled pages")
|
|
||||||
@ -779,9 +779,10 @@ class Scrobble(TimeStampedModel):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def session_pages_read(self) -> Optional[int]:
|
def session_pages_read(self) -> Optional[int]:
|
||||||
if not self.log.get("pages_read"):
|
pages_read = 0
|
||||||
return 0
|
if self.log and isinstance(self.log, dict):
|
||||||
return self.log.get("pages_read")
|
pages_read = self.log.get("pages_read", 0)
|
||||||
|
return pages_read
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_long_play(self) -> bool:
|
def is_long_play(self) -> bool:
|
||||||
|
|||||||
Reference in New Issue
Block a user