[charts] Big revamp of the charts app
This commit is contained in:
@ -48,7 +48,7 @@ from scrobbles.constants import (
|
||||
)
|
||||
from scrobbles.importers.lastfm import LastFM
|
||||
from scrobbles.notifications import ScrobbleNtfyNotification
|
||||
from scrobbles.stats import build_charts
|
||||
from charts.utils import build_charts
|
||||
from scrobbles.utils import get_file_md5_hash, media_class_to_foreign_key
|
||||
from sports.models import SportEvent
|
||||
from tasks.models import Task
|
||||
@ -344,140 +344,6 @@ class RetroarchImport(BaseFileImportMixin):
|
||||
self.mark_finished()
|
||||
|
||||
|
||||
class ChartRecord(TimeStampedModel):
|
||||
"""Sort of like a materialized view for what we could dynamically generate,
|
||||
but would kill the DB as it gets larger. Collects time-based records
|
||||
generated by a cron-like archival job
|
||||
|
||||
1972 by Josh Rouse - #3 in 2023, January
|
||||
|
||||
"""
|
||||
|
||||
user = models.ForeignKey(User, on_delete=models.DO_NOTHING, **BNULL)
|
||||
rank = models.IntegerField(db_index=True)
|
||||
count = models.IntegerField(default=0)
|
||||
year = models.IntegerField(**BNULL)
|
||||
month = models.IntegerField(**BNULL)
|
||||
week = models.IntegerField(**BNULL)
|
||||
day = models.IntegerField(**BNULL)
|
||||
video = models.ForeignKey(Video, on_delete=models.DO_NOTHING, **BNULL)
|
||||
series = models.ForeignKey(Series, on_delete=models.DO_NOTHING, **BNULL)
|
||||
artist = models.ForeignKey(Artist, on_delete=models.DO_NOTHING, **BNULL)
|
||||
track = models.ForeignKey(Track, on_delete=models.DO_NOTHING, **BNULL)
|
||||
period_start = models.DateTimeField(**BNULL)
|
||||
period_end = models.DateTimeField(**BNULL)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
profile = self.user.profile
|
||||
|
||||
if self.week:
|
||||
# set start and end to start and end of week
|
||||
period = datetime.date.fromisocalendar(self.year, self.week, 1)
|
||||
self.period_start = start_of_week(period, profile)
|
||||
self.period_start = end_of_week(period, profile)
|
||||
if self.day:
|
||||
period = datetime.datetime(self.year, self.month, self.day)
|
||||
self.period_start = start_of_day(period, profile)
|
||||
self.period_end = end_of_day(period, profile)
|
||||
if self.month and not self.day:
|
||||
period = datetime.datetime(self.year, self.month, 1)
|
||||
self.period_start = start_of_month(period, profile)
|
||||
self.period_end = end_of_month(period, profile)
|
||||
super(ChartRecord, self).save(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def media_obj(self):
|
||||
media_obj = None
|
||||
if self.video:
|
||||
media_obj = self.video
|
||||
if self.track:
|
||||
media_obj = self.track
|
||||
if self.artist:
|
||||
media_obj = self.artist
|
||||
return media_obj
|
||||
|
||||
@property
|
||||
def month_str(self) -> str:
|
||||
month_str = ""
|
||||
if self.month:
|
||||
month_str = calendar.month_name[self.month]
|
||||
return month_str
|
||||
|
||||
@property
|
||||
def day_str(self) -> str:
|
||||
day_str = ""
|
||||
if self.day:
|
||||
day_str = str(self.day)
|
||||
return day_str
|
||||
|
||||
@property
|
||||
def week_str(self) -> str:
|
||||
week_str = ""
|
||||
if self.week:
|
||||
week_str = str(self.week)
|
||||
return "Week " + week_str
|
||||
|
||||
@property
|
||||
def period(self) -> str:
|
||||
period = str(self.year)
|
||||
if self.month:
|
||||
period = " ".join([self.month_str, period])
|
||||
if self.week:
|
||||
period = " ".join([self.week_str, period])
|
||||
if self.day:
|
||||
period = " ".join([self.day_str, period])
|
||||
return period
|
||||
|
||||
@property
|
||||
def period_type(self) -> str:
|
||||
period = "year"
|
||||
if self.month:
|
||||
period = "month"
|
||||
if self.week:
|
||||
period = "week"
|
||||
if self.day:
|
||||
period = "day"
|
||||
return period
|
||||
|
||||
def __str__(self):
|
||||
title = f"#{self.rank} in {self.period}"
|
||||
if self.day or self.week:
|
||||
title = f"#{self.rank} on {self.period}"
|
||||
return title
|
||||
|
||||
def link(self):
|
||||
get_params = f"?date={self.year}"
|
||||
if self.week:
|
||||
get_params = get_params = get_params + f"-W{self.week}"
|
||||
if self.month:
|
||||
get_params = get_params = get_params + f"-{self.month}"
|
||||
if self.day:
|
||||
get_params = get_params = get_params + f"-{self.day}"
|
||||
if self.artist:
|
||||
get_params = get_params + "&media=Artist"
|
||||
return reverse("scrobbles:charts-home") + get_params
|
||||
|
||||
@classmethod
|
||||
def build(cls, user, **kwargs):
|
||||
build_charts(user=user, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def for_year(cls, user, year):
|
||||
return cls.objects.filter(year=year, user=user)
|
||||
|
||||
@classmethod
|
||||
def for_month(cls, user, year, month):
|
||||
return cls.objects.filter(year=year, month=month, user=user)
|
||||
|
||||
@classmethod
|
||||
def for_day(cls, user, year, day, month):
|
||||
return cls.objects.filter(year=year, month=month, day=day, user=user)
|
||||
|
||||
@classmethod
|
||||
def for_week(cls, user, year, week):
|
||||
return cls.objects.filter(year=year, week=week, user=user)
|
||||
|
||||
|
||||
class ScrobbleQuerySet(models.QuerySet):
|
||||
def with_related(self):
|
||||
return self.select_related("user").prefetch_related(
|
||||
|
||||
Reference in New Issue
Block a user