[locations] Remove RawGeoLocation model and ScrobbledPage model

This commit is contained in:
2024-02-19 19:22:13 -05:00
parent b981c090c6
commit 7fa21c27ea
6 changed files with 33 additions and 129 deletions

View File

@ -1,6 +1,6 @@
from django.contrib import admin
from locations.models import GeoLocation, RawGeoLocation
from locations.models import GeoLocation
from scrobbles.admin import ScrobbleInline
@ -19,18 +19,3 @@ class GeoLocationAdmin(admin.ModelAdmin):
inlines = [
ScrobbleInline,
]
@admin.register(RawGeoLocation)
class RawGeoLocationAdmin(admin.ModelAdmin):
date_hierarchy = "created"
list_display = (
"lat",
"lon",
"altitude",
"speed",
)
ordering = (
"lat",
"lon",
)

View File

@ -0,0 +1,16 @@
# Generated by Django 4.2.9 on 2024-02-20 00:20
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("locations", "0005_alter_rawgeolocation_options_and_more"),
]
operations = [
migrations.DeleteModel(
name="RawGeoLocation",
),
]

View File

@ -120,12 +120,3 @@ class GeoLocation(ScrobblableMixin):
lon__lte=lon_max,
lon__gte=lon_min,
)
class RawGeoLocation(TimeStampedModel):
user = models.ForeignKey(User, on_delete=models.CASCADE)
lat = models.FloatField()
lon = models.FloatField()
altitude = models.FloatField(**BNULL)
speed = models.FloatField(**BNULL)
timestamp = models.DateTimeField(**BNULL)

View File

@ -7,7 +7,6 @@ from scrobbles.models import (
LastFmImport,
RetroarchImport,
Scrobble,
ScrobbledPage,
)
from scrobbles.mixins import Genre
@ -126,13 +125,3 @@ class ScrobbleAdmin(admin.ModelAdmin):
def playback_percent(self, obj):
return obj.percent_played
@admin.register(ScrobbledPage)
class ScrobbledPageAdmin(admin.ModelAdmin):
list_display = (
"number",
"scrobble",
"notes",
)
raw_id_fields = ("scrobble",)

View File

@ -0,0 +1,16 @@
# Generated by Django 4.2.9 on 2024-02-20 00:21
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("scrobbles", "0049_rename_webpage_scrobble_web_page"),
]
operations = [
migrations.DeleteModel(
name="ScrobbledPage",
),
]

View File

@ -955,96 +955,3 @@ class Scrobble(TimeStampedModel):
f"{self.id} - {self.playback_position_seconds} - {self.source}"
)
self.save(update_fields=["playback_position_seconds"])
class ScrobbledPage(TimeStampedModel):
scrobble = models.ForeignKey(Scrobble, on_delete=models.DO_NOTHING)
number = models.IntegerField()
start_time = models.DateTimeField(**BNULL)
end_time = models.DateTimeField(**BNULL)
duration_seconds = models.IntegerField(**BNULL)
notes = models.CharField(max_length=255, **BNULL)
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(ScrobbledPage, self).save(*args, **kwargs)
@cached_property
def book(self):
return self.scrobble.book
@property
def next(self):
user_pages_qs = self.book.scrobbledpage_set.filter(
user=self.scrobble.user
)
page = user_pages_qs.filter(number=self.number + 1).first()
if not page:
page = (
user_pages_qs.filter(created__gt=self.created)
.order_by("created")
.first()
)
return page
@property
def previous(self):
user_pages_qs = self.book.scrobbledpage_set.filter(
user=self.scrobble.user
)
page = user_pages_qs.filter(number=self.number - 1).first()
if not page:
page = (
user_pages_qs.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 + datetime.timedelta(
seconds=self.duration_seconds
)
self.save(update_fields=["end_time"])