[scrobbles] Add two new methods to Scrobble model
This commit is contained in:
@ -844,6 +844,12 @@ class Scrobble(TimeStampedModel):
|
||||
def is_long_play(self) -> bool:
|
||||
return self.media_obj.__class__.__name__ in LONG_PLAY_MEDIA.values()
|
||||
|
||||
@property
|
||||
def elapsed_time(self) -> int | None:
|
||||
if self.played_to_completion:
|
||||
return self.playback_position_seconds
|
||||
return (timezone.now() - self.timestamp).seconds
|
||||
|
||||
@property
|
||||
def percent_played(self) -> int:
|
||||
if not self.media_obj:
|
||||
@ -857,7 +863,7 @@ class Scrobble(TimeStampedModel):
|
||||
|
||||
playback_seconds = self.playback_position_seconds
|
||||
if not playback_seconds:
|
||||
playback_seconds = (timezone.now() - self.timestamp).seconds
|
||||
playback_seconds = self.elapsed_time
|
||||
|
||||
run_time_secs = self.media_obj.run_time_seconds
|
||||
percent = int((playback_seconds / run_time_secs) * 100)
|
||||
@ -973,6 +979,16 @@ class Scrobble(TimeStampedModel):
|
||||
)
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def by_date(cls, media_type: str = "Track"):
|
||||
cls.objects.filter(media_type=media_type).values(
|
||||
"timestamp__date"
|
||||
).annotate(count=models.Count("id")).values(
|
||||
"timestamp__date", "count"
|
||||
).order_by(
|
||||
"-count",
|
||||
)
|
||||
|
||||
@property
|
||||
def media_obj(self):
|
||||
media_obj = None
|
||||
|
||||
Reference in New Issue
Block a user