[charts] Big revamp of the charts app
This commit is contained in:
0
vrobbler/apps/charts/__init__.py
Normal file
0
vrobbler/apps/charts/__init__.py
Normal file
50
vrobbler/apps/charts/admin.py
Normal file
50
vrobbler/apps/charts/admin.py
Normal file
@ -0,0 +1,50 @@
|
||||
from charts.models import ChartRecord
|
||||
from django.contrib import admin
|
||||
|
||||
|
||||
@admin.register(ChartRecord)
|
||||
class ChartRecordAdmin(admin.ModelAdmin):
|
||||
date_hierarchy = "created"
|
||||
list_display = (
|
||||
"user",
|
||||
"rank",
|
||||
"count",
|
||||
"year",
|
||||
"month",
|
||||
"week",
|
||||
"day",
|
||||
"media_name",
|
||||
"media_type",
|
||||
)
|
||||
list_filter = (
|
||||
"user",
|
||||
"year",
|
||||
"month",
|
||||
"week",
|
||||
"day",
|
||||
"tv_series",
|
||||
"podcast",
|
||||
"board_game",
|
||||
"book",
|
||||
"food",
|
||||
"trail",
|
||||
)
|
||||
ordering = ("-created",)
|
||||
search_fields = (
|
||||
"artist__name",
|
||||
"album__name",
|
||||
"track__title",
|
||||
"tv_series__name",
|
||||
"video__title",
|
||||
"podcast__name",
|
||||
"board_game__name",
|
||||
"book__title",
|
||||
"food__name",
|
||||
"trail__name",
|
||||
)
|
||||
|
||||
def media_name(self, obj):
|
||||
return obj.media_obj
|
||||
|
||||
def media_type(self, obj):
|
||||
return obj.media_type
|
||||
6
vrobbler/apps/charts/apps.py
Normal file
6
vrobbler/apps/charts/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ChartsConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "charts"
|
||||
0
vrobbler/apps/charts/management/__init__.py
Normal file
0
vrobbler/apps/charts/management/__init__.py
Normal file
59
vrobbler/apps/charts/management/commands/build_charts.py
Normal file
59
vrobbler/apps/charts/management/commands/build_charts.py
Normal file
@ -0,0 +1,59 @@
|
||||
import logging
|
||||
|
||||
from charts.utils import build_all_historical_charts, rebuild_all_charts
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Build missing charts for all users"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
"--user-id",
|
||||
type=int,
|
||||
help="Build charts for a specific user only",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--full-rebuild",
|
||||
action="store_true",
|
||||
help="Delete existing chart records and rebuild from scratch",
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
User = get_user_model()
|
||||
full_rebuild = options["full_rebuild"]
|
||||
|
||||
if full_rebuild:
|
||||
self.stdout.write("Full rebuild requested - will delete existing records")
|
||||
|
||||
if options["user_id"]:
|
||||
user = User.objects.filter(id=options["user_id"]).first()
|
||||
if not user:
|
||||
self.stderr.write(
|
||||
self.style.ERROR(f"User with id {options['user_id']} not found")
|
||||
)
|
||||
return
|
||||
users = [user]
|
||||
action = "Rebuilding" if full_rebuild else "Building charts for"
|
||||
self.stdout.write(f"{action} user: {user}")
|
||||
else:
|
||||
users = User.objects.all()
|
||||
action = "Rebuilding" if full_rebuild else "Building charts for"
|
||||
self.stdout.write(f"{action} charts for {users.count()} users...")
|
||||
|
||||
for user in users:
|
||||
try:
|
||||
logger.info(f"Building all historical charts for {user}")
|
||||
if full_rebuild:
|
||||
rebuild_all_charts(user)
|
||||
else:
|
||||
build_all_historical_charts(user)
|
||||
self.stdout.write(self.style.SUCCESS(f" OK {user}"))
|
||||
except Exception as e:
|
||||
self.stderr.write(f" FAILED {user}: {e}")
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("Done!"))
|
||||
@ -0,0 +1,20 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import connection
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Copy ChartRecord data from scrobbles to charts app"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute("""
|
||||
INSERT OR IGNORE INTO charts_chartrecord
|
||||
(created, modified, user_id, rank, count, year, month, week, day,
|
||||
video_id, series_id, artist_id, track_id, period_start, period_end)
|
||||
SELECT created, modified, user_id, rank, count, year, month, week, day,
|
||||
video_id, series_id, artist_id, track_id, period_start, period_end
|
||||
FROM scrobbles_chartrecord
|
||||
""")
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS("Copied ChartRecord data to charts app")
|
||||
)
|
||||
239
vrobbler/apps/charts/migrations/0001_initial.py
Normal file
239
vrobbler/apps/charts/migrations/0001_initial.py
Normal file
@ -0,0 +1,239 @@
|
||||
# Generated by Django 4.2.29 on 2026-03-21 19:34
|
||||
|
||||
import django.db.models.deletion
|
||||
import django_extensions.db.fields
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
("boardgames", "0013_boardgame_publishers"),
|
||||
("locations", "0008_remove_geolocation_run_time_seconds_and_more"),
|
||||
("podcasts", "0018_remove_podcastepisode_run_time_seconds_and_more"),
|
||||
("music", "0029_remove_track_run_time_seconds_and_more"),
|
||||
("videos", "0025_add_imdb_id_unique"),
|
||||
("trails", "0006_remove_trail_run_time_seconds_and_more"),
|
||||
("books", "0033_alter_book_issue_number_alter_book_volume_number"),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
("foods", "0005_food_nutrition_and_sources"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="ChartRecord",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"created",
|
||||
django_extensions.db.fields.CreationDateTimeField(
|
||||
auto_now_add=True, verbose_name="created"
|
||||
),
|
||||
),
|
||||
(
|
||||
"modified",
|
||||
django_extensions.db.fields.ModificationDateTimeField(
|
||||
auto_now=True, verbose_name="modified"
|
||||
),
|
||||
),
|
||||
("year", models.IntegerField()),
|
||||
("month", models.IntegerField(blank=True, null=True)),
|
||||
("week", models.IntegerField(blank=True, null=True)),
|
||||
("day", models.IntegerField(blank=True, null=True)),
|
||||
("rank", models.IntegerField()),
|
||||
("count", models.IntegerField(default=0)),
|
||||
(
|
||||
"album",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="music.album",
|
||||
),
|
||||
),
|
||||
(
|
||||
"artist",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="music.artist",
|
||||
),
|
||||
),
|
||||
(
|
||||
"board_game",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="boardgames.boardgame",
|
||||
),
|
||||
),
|
||||
(
|
||||
"book",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="books.book",
|
||||
),
|
||||
),
|
||||
(
|
||||
"food",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="foods.food",
|
||||
),
|
||||
),
|
||||
(
|
||||
"geo_location",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="locations.geolocation",
|
||||
),
|
||||
),
|
||||
(
|
||||
"podcast",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="podcasts.podcast",
|
||||
),
|
||||
),
|
||||
(
|
||||
"podcast_episode",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="podcasts.podcastepisode",
|
||||
),
|
||||
),
|
||||
(
|
||||
"track",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="music.track",
|
||||
),
|
||||
),
|
||||
(
|
||||
"trail",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="trails.trail",
|
||||
),
|
||||
),
|
||||
(
|
||||
"tv_series",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="videos.series",
|
||||
),
|
||||
),
|
||||
(
|
||||
"user",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
(
|
||||
"video",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="videos.video",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"indexes": [
|
||||
models.Index(
|
||||
fields=["user", "year", "artist", "rank"],
|
||||
name="charts_char_user_id_dc09e6_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "album", "rank"],
|
||||
name="charts_char_user_id_218be5_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "track", "rank"],
|
||||
name="charts_char_user_id_db95f8_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "tv_series", "rank"],
|
||||
name="charts_char_user_id_2a16e9_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "video", "rank"],
|
||||
name="charts_char_user_id_717866_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "podcast", "rank"],
|
||||
name="charts_char_user_id_c79e3b_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "board_game", "rank"],
|
||||
name="charts_char_user_id_73fbd2_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "trail", "rank"],
|
||||
name="charts_char_user_id_ad3c30_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "geo_location", "rank"],
|
||||
name="charts_char_user_id_245e01_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "food", "rank"],
|
||||
name="charts_char_user_id_f9fe30_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "book", "rank"],
|
||||
name="charts_char_user_id_e0321e_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "week", "artist", "rank"],
|
||||
name="charts_char_user_id_18f06e_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "week", "tv_series", "rank"],
|
||||
name="charts_char_user_id_8dcc60_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "month", "artist", "rank"],
|
||||
name="charts_char_user_id_3ef8c3_idx",
|
||||
),
|
||||
models.Index(
|
||||
fields=["user", "year", "month", "tv_series", "rank"],
|
||||
name="charts_char_user_id_dd54d7_idx",
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
]
|
||||
0
vrobbler/apps/charts/migrations/__init__.py
Normal file
0
vrobbler/apps/charts/migrations/__init__.py
Normal file
138
vrobbler/apps/charts/models.py
Normal file
138
vrobbler/apps/charts/models.py
Normal file
@ -0,0 +1,138 @@
|
||||
import calendar
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import models
|
||||
from django.urls import reverse
|
||||
from django_extensions.db.models import TimeStampedModel
|
||||
|
||||
User = get_user_model()
|
||||
BNULL = {"blank": True, "null": True}
|
||||
|
||||
|
||||
class ChartRecord(TimeStampedModel):
|
||||
"""Stores rankings for a user/media/time period combination.
|
||||
|
||||
Example entries:
|
||||
- Bjork was #3 artist in 2022
|
||||
- Homogenic was #10 album in April 2025
|
||||
- The Office was #1 TV series in Week #34 2024
|
||||
"""
|
||||
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE, **BNULL)
|
||||
|
||||
year = models.IntegerField()
|
||||
month = models.IntegerField(**BNULL)
|
||||
week = models.IntegerField(**BNULL)
|
||||
day = models.IntegerField(**BNULL)
|
||||
|
||||
artist = models.ForeignKey("music.Artist", on_delete=models.CASCADE, **BNULL)
|
||||
album = models.ForeignKey("music.Album", on_delete=models.CASCADE, **BNULL)
|
||||
track = models.ForeignKey("music.Track", on_delete=models.CASCADE, **BNULL)
|
||||
tv_series = models.ForeignKey("videos.Series", on_delete=models.CASCADE, **BNULL)
|
||||
video = models.ForeignKey("videos.Video", on_delete=models.CASCADE, **BNULL)
|
||||
podcast = models.ForeignKey("podcasts.Podcast", on_delete=models.CASCADE, **BNULL)
|
||||
podcast_episode = models.ForeignKey(
|
||||
"podcasts.PodcastEpisode", on_delete=models.CASCADE, **BNULL
|
||||
)
|
||||
board_game = models.ForeignKey(
|
||||
"boardgames.BoardGame", on_delete=models.CASCADE, **BNULL
|
||||
)
|
||||
trail = models.ForeignKey("trails.Trail", on_delete=models.CASCADE, **BNULL)
|
||||
geo_location = models.ForeignKey(
|
||||
"locations.GeoLocation", on_delete=models.CASCADE, **BNULL
|
||||
)
|
||||
food = models.ForeignKey("foods.Food", on_delete=models.CASCADE, **BNULL)
|
||||
book = models.ForeignKey("books.Book", on_delete=models.CASCADE, **BNULL)
|
||||
|
||||
rank = models.IntegerField()
|
||||
count = models.IntegerField(default=0)
|
||||
|
||||
class Meta:
|
||||
indexes = [
|
||||
models.Index(fields=["user", "year", "artist", "rank"]),
|
||||
models.Index(fields=["user", "year", "album", "rank"]),
|
||||
models.Index(fields=["user", "year", "track", "rank"]),
|
||||
models.Index(fields=["user", "year", "tv_series", "rank"]),
|
||||
models.Index(fields=["user", "year", "video", "rank"]),
|
||||
models.Index(fields=["user", "year", "podcast", "rank"]),
|
||||
models.Index(fields=["user", "year", "board_game", "rank"]),
|
||||
models.Index(fields=["user", "year", "trail", "rank"]),
|
||||
models.Index(fields=["user", "year", "geo_location", "rank"]),
|
||||
models.Index(fields=["user", "year", "food", "rank"]),
|
||||
models.Index(fields=["user", "year", "book", "rank"]),
|
||||
models.Index(fields=["user", "year", "week", "artist", "rank"]),
|
||||
models.Index(fields=["user", "year", "week", "tv_series", "rank"]),
|
||||
models.Index(fields=["user", "year", "month", "artist", "rank"]),
|
||||
models.Index(fields=["user", "year", "month", "tv_series", "rank"]),
|
||||
]
|
||||
|
||||
@property
|
||||
def media_obj(self):
|
||||
for attr in [
|
||||
"artist",
|
||||
"album",
|
||||
"track",
|
||||
"tv_series",
|
||||
"video",
|
||||
"podcast",
|
||||
"podcast_episode",
|
||||
"board_game",
|
||||
"trail",
|
||||
"geo_location",
|
||||
"food",
|
||||
"book",
|
||||
]:
|
||||
obj = getattr(self, attr)
|
||||
if obj:
|
||||
return obj
|
||||
return None
|
||||
|
||||
@property
|
||||
def media_type(self):
|
||||
mapping = {
|
||||
"artist": "Artist",
|
||||
"album": "Album",
|
||||
"track": "Track",
|
||||
"tv_series": "TV Series",
|
||||
"video": "Video",
|
||||
"podcast": "Podcast",
|
||||
"podcast_episode": "Podcast Episode",
|
||||
"board_game": "Board Game",
|
||||
"trail": "Trail",
|
||||
"geo_location": "Location",
|
||||
"food": "Food",
|
||||
"book": "Book",
|
||||
}
|
||||
for attr, name in mapping.items():
|
||||
if getattr(self, attr) is not None:
|
||||
return name
|
||||
return None
|
||||
|
||||
@property
|
||||
def period_str(self) -> str:
|
||||
period = str(self.year)
|
||||
if self.month:
|
||||
period = f"{calendar.month_name[self.month]} {period}"
|
||||
if self.week:
|
||||
period = f"Week {self.week}, {period}"
|
||||
if self.day:
|
||||
period = f"{calendar.month_name[self.month]} {self.day}, {period}"
|
||||
return period
|
||||
|
||||
@property
|
||||
def period_type(self) -> str:
|
||||
if self.day:
|
||||
return "day"
|
||||
if self.week:
|
||||
return "week"
|
||||
if self.month:
|
||||
return "month"
|
||||
return "year"
|
||||
|
||||
def __str__(self):
|
||||
obj = self.media_obj
|
||||
obj_name = str(obj) if obj else "Unknown"
|
||||
return f"#{self.rank} {obj_name} in {self.period_str}"
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse("charts:charts-home")
|
||||
315
vrobbler/apps/charts/templates/charts/chart_index.html
Normal file
315
vrobbler/apps/charts/templates/charts/chart_index.html
Normal file
@ -0,0 +1,315 @@
|
||||
{% extends "base_list.html" %}
|
||||
{% load chart_tags %}
|
||||
|
||||
{% block title %}Charts{% endblock %}
|
||||
|
||||
{% block head_extra %}
|
||||
<style>
|
||||
.container { margin-bottom:100px; }
|
||||
h2 { padding-top:20px; }
|
||||
.image-wrapper {
|
||||
contain: content;
|
||||
}
|
||||
.image-wrapper :hover {
|
||||
background:rgba(0,0,0,0.3);
|
||||
}
|
||||
.caption {
|
||||
position: fixed;
|
||||
top: 5px;
|
||||
left: 5px;
|
||||
padding: 3px;
|
||||
font-size: 90%;
|
||||
color:white;
|
||||
background:rgba(0,0,0,0.4);
|
||||
}
|
||||
|
||||
.caption-medium {
|
||||
position: fixed;
|
||||
top: 5px;
|
||||
left: 5px;
|
||||
padding: 3px;
|
||||
font-size: 75%;
|
||||
color:white;
|
||||
background:rgba(0,0,0,0.4);
|
||||
|
||||
}
|
||||
.caption-small {
|
||||
position: fixed;
|
||||
top: 5px;
|
||||
left: 5px;
|
||||
padding: 3px;
|
||||
font-size: 60%;
|
||||
color:white;
|
||||
background:rgba(0,0,0,0.4);
|
||||
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block lists %}
|
||||
{% if chart_type == "maloja" %}
|
||||
{% include "scrobbles/_top_charts.html" %}
|
||||
{% else %}
|
||||
<div class="row">
|
||||
{% if charts.artist %}
|
||||
<div class="col-md">
|
||||
<h2>🎤 Top Artists</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Rank</th>
|
||||
<th scope="col">Artist</th>
|
||||
<th scope="col">Scrobbles</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for chart in charts.artist %}
|
||||
<tr>
|
||||
<td>{{chart.rank}}</td>
|
||||
<td><a href="{{chart.artist.get_absolute_url}}">{{chart.artist.name}}</a></td>
|
||||
<td>{{chart.count}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if charts.track %}
|
||||
<div class="col-md">
|
||||
<h2>🎵 Top Tracks</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Rank</th>
|
||||
<th scope="col">Track</th>
|
||||
<th scope="col">Scrobbles</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for chart in charts.track %}
|
||||
<tr>
|
||||
<td>{{chart.rank}}</td>
|
||||
<td><a href="{{chart.track.get_absolute_url}}">{{chart.track.title}}</a></td>
|
||||
<td>{{chart.count}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if charts.tv_series %}
|
||||
<div class="col-md">
|
||||
<h2>📺 Top TV Shows</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Rank</th>
|
||||
<th scope="col">TV Show</th>
|
||||
<th scope="col">Episodes Watched</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for chart in charts.tv_series %}
|
||||
<tr>
|
||||
<td>{{chart.rank}}</td>
|
||||
<td><a href="{{chart.tv_series.get_absolute_url}}">{{chart.tv_series.name}}</a></td>
|
||||
<td>{{chart.count}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if charts.album %}
|
||||
<div class="col-md">
|
||||
<h2>💿 Top Albums</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Rank</th>
|
||||
<th scope="col">Album</th>
|
||||
<th scope="col">Scrobbles</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for chart in charts.album %}
|
||||
<tr>
|
||||
<td>{{chart.rank}}</td>
|
||||
<td><a href="{{chart.album.get_absolute_url}}">{{chart.album.title}}</a></td>
|
||||
<td>{{chart.count}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if charts.video %}
|
||||
<div class="col-md">
|
||||
<h2>🎬 Top Videos</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Rank</th>
|
||||
<th scope="col">Video</th>
|
||||
<th scope="col">Plays</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for chart in charts.video %}
|
||||
<tr>
|
||||
<td>{{chart.rank}}</td>
|
||||
<td><a href="{{chart.video.get_absolute_url}}">{{chart.video.title}}</a></td>
|
||||
<td>{{chart.count}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if charts.board_game %}
|
||||
<div class="col-md">
|
||||
<h2>🎲 Top Board Games</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Rank</th>
|
||||
<th scope="col">Board Game</th>
|
||||
<th scope="col">Plays</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for chart in charts.board_game %}
|
||||
<tr>
|
||||
<td>{{chart.rank}}</td>
|
||||
<td><a href="{{chart.board_game.get_absolute_url}}">{{chart.board_game.title}}</a></td>
|
||||
<td>{{chart.count}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if charts.book %}
|
||||
<div class="col-md">
|
||||
<h2>📚 Top Books</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Rank</th>
|
||||
<th scope="col">Book</th>
|
||||
<th scope="col">Pages Read</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for chart in charts.book %}
|
||||
<tr>
|
||||
<td>{{chart.rank}}</td>
|
||||
<td><a href="{{chart.book.get_absolute_url}}">{{chart.book.title}}</a></td>
|
||||
<td>{{chart.count}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if charts.food %}
|
||||
<div class="col-md">
|
||||
<h2>🍽️ Top Foods</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Rank</th>
|
||||
<th scope="col">Food</th>
|
||||
<th scope="col">Times Eaten</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for chart in charts.food %}
|
||||
<tr>
|
||||
<td>{{chart.rank}}</td>
|
||||
<td><a href="{{chart.food.get_absolute_url}}">{{chart.food.title}}</a></td>
|
||||
<td>{{chart.count}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if charts.podcast %}
|
||||
<div class="col-md">
|
||||
<h2>🎙️ Top Podcasts</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Rank</th>
|
||||
<th scope="col">Podcast</th>
|
||||
<th scope="col">Episodes</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for chart in charts.podcast %}
|
||||
<tr>
|
||||
<td>{{chart.rank}}</td>
|
||||
<td><a href="{{chart.podcast.get_absolute_url}}">{{chart.podcast.title}}</a></td>
|
||||
<td>{{chart.count}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if charts.trail %}
|
||||
<div class="col-md">
|
||||
<h2>🥾 Top Trails</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Rank</th>
|
||||
<th scope="col">Trail</th>
|
||||
<th scope="col">Hikes</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for chart in charts.trail %}
|
||||
<tr>
|
||||
<td>{{chart.rank}}</td>
|
||||
<td><a href="{{chart.trail.get_absolute_url}}">{{chart.trail.name}}</a></td>
|
||||
<td>{{chart.count}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
0
vrobbler/apps/charts/templatetags/__init__.py
Normal file
0
vrobbler/apps/charts/templatetags/__init__.py
Normal file
10
vrobbler/apps/charts/templatetags/chart_tags.py
Normal file
10
vrobbler/apps/charts/templatetags/chart_tags.py
Normal file
@ -0,0 +1,10 @@
|
||||
from django import template
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.filter
|
||||
def get_item(dictionary, key):
|
||||
if isinstance(dictionary, dict):
|
||||
return dictionary.get(key)
|
||||
return None
|
||||
8
vrobbler/apps/charts/urls.py
Normal file
8
vrobbler/apps/charts/urls.py
Normal file
@ -0,0 +1,8 @@
|
||||
from charts.views import ChartRecordView
|
||||
from django.urls import path
|
||||
|
||||
app_name = "charts"
|
||||
|
||||
urlpatterns = [
|
||||
path("charts/", ChartRecordView.as_view(), name="charts-home"),
|
||||
]
|
||||
339
vrobbler/apps/charts/utils.py
Normal file
339
vrobbler/apps/charts/utils.py
Normal file
@ -0,0 +1,339 @@
|
||||
import calendar
|
||||
import datetime
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
import pytz
|
||||
from django.apps import apps
|
||||
from django.conf import settings
|
||||
from django.db.models import Count, Q
|
||||
from django.utils import timezone
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_time_filter(
|
||||
year: Optional[int],
|
||||
month: Optional[int],
|
||||
week: Optional[int],
|
||||
day: Optional[int],
|
||||
user,
|
||||
):
|
||||
"""Build a Q filter for scrobble timestamps based on time period."""
|
||||
if not year:
|
||||
return Q()
|
||||
|
||||
if user and user.is_authenticated:
|
||||
tz = pytz.timezone(user.profile.timezone)
|
||||
else:
|
||||
tz = pytz.timezone(settings.TIME_ZONE)
|
||||
|
||||
if day and month:
|
||||
start = timezone.make_aware(
|
||||
datetime.datetime(year, month, day, 0, 0, 0)
|
||||
)
|
||||
end = timezone.make_aware(
|
||||
datetime.datetime(year, month, day, 23, 59, 59)
|
||||
)
|
||||
elif week:
|
||||
period = datetime.date.fromisocalendar(year, week, 1)
|
||||
start = timezone.make_aware(
|
||||
datetime.datetime.combine(period, datetime.time.min)
|
||||
)
|
||||
end = start + datetime.timedelta(days=6, seconds=86399)
|
||||
elif month:
|
||||
last_day = calendar.monthrange(year, month)[1]
|
||||
start = timezone.make_aware(datetime.datetime(year, month, 1))
|
||||
end = timezone.make_aware(
|
||||
datetime.datetime(year, month, last_day, 23, 59, 59)
|
||||
)
|
||||
else:
|
||||
start = timezone.make_aware(datetime.datetime(year, 1, 1))
|
||||
end = timezone.make_aware(datetime.datetime(year, 12, 31, 23, 59, 59))
|
||||
|
||||
return Q(timestamp__gte=start, timestamp__lte=end)
|
||||
|
||||
|
||||
def build_charts(
|
||||
user,
|
||||
year: Optional[int] = None,
|
||||
month: Optional[int] = None,
|
||||
week: Optional[int] = None,
|
||||
day: Optional[int] = None,
|
||||
media_types: Optional[list] = None,
|
||||
):
|
||||
"""Build chart records for all or specified media types."""
|
||||
ChartRecord = apps.get_model("charts", "ChartRecord")
|
||||
Scrobble = apps.get_model("scrobbles", "Scrobble")
|
||||
|
||||
if media_types is None:
|
||||
media_types = [
|
||||
"artist",
|
||||
"album",
|
||||
"track",
|
||||
"tv_series",
|
||||
"video",
|
||||
"podcast",
|
||||
"podcast_episode",
|
||||
"board_game",
|
||||
"trail",
|
||||
"geo_location",
|
||||
"food",
|
||||
"book",
|
||||
]
|
||||
|
||||
period_filter = Q(year=year)
|
||||
if month:
|
||||
period_filter &= Q(month=month)
|
||||
if week:
|
||||
period_filter &= Q(week=week)
|
||||
if day:
|
||||
period_filter &= Q(day=day)
|
||||
|
||||
ChartRecord.objects.filter(period_filter, user=user).delete()
|
||||
|
||||
time_filter = get_time_filter(year, month, week, day, user)
|
||||
base_qs = Scrobble.objects.filter(user=user, played_to_completion=True)
|
||||
|
||||
if time_filter:
|
||||
base_qs = base_qs.filter(time_filter)
|
||||
|
||||
media_config = {
|
||||
"artist": {
|
||||
"filter": Q(track__isnull=False) & Q(track__artist__isnull=False),
|
||||
"values": "track__artist",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"album": {
|
||||
"filter": Q(track__isnull=False) & Q(track__album__isnull=False),
|
||||
"values": "track__album",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"track": {
|
||||
"filter": Q(track__isnull=False),
|
||||
"values": "track",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"tv_series": {
|
||||
"filter": Q(video__isnull=False)
|
||||
& Q(video__tv_series__isnull=False),
|
||||
"values": "video__tv_series",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"video": {
|
||||
"filter": Q(video__isnull=False),
|
||||
"values": "video",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"podcast": {
|
||||
"filter": Q(podcast_episode__isnull=False)
|
||||
& Q(podcast_episode__podcast__isnull=False),
|
||||
"values": "podcast_episode__podcast",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"podcast_episode": {
|
||||
"filter": Q(podcast_episode__isnull=False),
|
||||
"values": "podcast_episode",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"board_game": {
|
||||
"filter": Q(board_game__isnull=False),
|
||||
"values": "board_game",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"trail": {
|
||||
"filter": Q(trail__isnull=False),
|
||||
"values": "trail",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"geo_location": {
|
||||
"filter": Q(geo_location__isnull=False),
|
||||
"values": "geo_location",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"food": {
|
||||
"filter": Q(food__isnull=False),
|
||||
"values": "food",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
"book": {
|
||||
"filter": Q(book__isnull=False),
|
||||
"values": "book",
|
||||
"annotate": Count("id", distinct=True),
|
||||
},
|
||||
}
|
||||
|
||||
BATCH_SIZE = 500
|
||||
|
||||
for media_type in media_types:
|
||||
if media_type not in media_config:
|
||||
continue
|
||||
|
||||
config = media_config[media_type]
|
||||
|
||||
results = list(
|
||||
base_qs.filter(config["filter"])
|
||||
.values(config["values"])
|
||||
.annotate(scrobble_count=config["annotate"])
|
||||
.order_by("-scrobble_count")
|
||||
)
|
||||
|
||||
if not results:
|
||||
continue
|
||||
|
||||
unique_counts = sorted(
|
||||
set(r["scrobble_count"] for r in results), reverse=True
|
||||
)
|
||||
ranks = {
|
||||
count: rank for rank, count in enumerate(unique_counts, start=1)
|
||||
}
|
||||
|
||||
chart_records = []
|
||||
total_created = 0
|
||||
for result in results:
|
||||
chart_record_data = {
|
||||
"user_id": user.id,
|
||||
"year": year,
|
||||
"month": month,
|
||||
"week": week,
|
||||
"day": day,
|
||||
"rank": ranks[result["scrobble_count"]],
|
||||
"count": result["scrobble_count"],
|
||||
}
|
||||
chart_record_data[f"{media_type}_id"] = result[config["values"]]
|
||||
chart_records.append(ChartRecord(**chart_record_data))
|
||||
|
||||
if len(chart_records) >= BATCH_SIZE:
|
||||
ChartRecord.objects.bulk_create(
|
||||
chart_records, batch_size=BATCH_SIZE
|
||||
)
|
||||
total_created += len(chart_records)
|
||||
chart_records = []
|
||||
|
||||
if chart_records:
|
||||
ChartRecord.objects.bulk_create(
|
||||
chart_records, batch_size=BATCH_SIZE
|
||||
)
|
||||
total_created += len(chart_records)
|
||||
|
||||
logger.info(
|
||||
f"Built {total_created} chart records for {media_type}, period "
|
||||
f"{year}-{month or ''}-{week or ''}-{day or ''} for {user}"
|
||||
)
|
||||
|
||||
|
||||
def build_yesterdays_charts(user, media_types: Optional[list] = None) -> None:
|
||||
"""Build charts for yesterday's date."""
|
||||
tz = pytz.timezone(settings.TIME_ZONE)
|
||||
if user and user.is_authenticated:
|
||||
tz = pytz.timezone(user.profile.timezone)
|
||||
|
||||
now = timezone.now().astimezone(tz)
|
||||
yesterday = now - datetime.timedelta(days=1)
|
||||
|
||||
logger.info(f"Building charts for {yesterday.date()} for {user}")
|
||||
|
||||
build_charts(
|
||||
user=user,
|
||||
year=yesterday.year,
|
||||
month=yesterday.month,
|
||||
day=yesterday.day,
|
||||
media_types=media_types,
|
||||
)
|
||||
|
||||
|
||||
def build_weekly_charts(
|
||||
user, year: int, week: int, media_types: Optional[list] = None
|
||||
) -> None:
|
||||
"""Build weekly charts for a specific ISO week."""
|
||||
build_charts(
|
||||
user=user,
|
||||
year=year,
|
||||
week=week,
|
||||
media_types=media_types,
|
||||
)
|
||||
|
||||
|
||||
def build_monthly_charts(
|
||||
user, year: int, month: int, media_types: Optional[list] = None
|
||||
) -> None:
|
||||
"""Build monthly charts for a specific month."""
|
||||
build_charts(
|
||||
user=user,
|
||||
year=year,
|
||||
month=month,
|
||||
media_types=media_types,
|
||||
)
|
||||
|
||||
|
||||
def build_yearly_charts(
|
||||
user, year: int, media_types: Optional[list] = None
|
||||
) -> None:
|
||||
"""Build yearly charts for a specific year."""
|
||||
build_charts(
|
||||
user=user,
|
||||
year=year,
|
||||
media_types=media_types,
|
||||
)
|
||||
|
||||
|
||||
def build_all_historical_charts(
|
||||
user, media_types: Optional[list] = None
|
||||
) -> None:
|
||||
"""Build all historical charts for a user."""
|
||||
Scrobble = apps.get_model("scrobbles", "Scrobble")
|
||||
|
||||
first_scrobble = (
|
||||
Scrobble.objects.filter(user=user, played_to_completion=True)
|
||||
.order_by("timestamp")
|
||||
.first()
|
||||
)
|
||||
|
||||
if not first_scrobble:
|
||||
logger.info(f"No scrobbles found for {user}")
|
||||
return
|
||||
|
||||
now = timezone.now()
|
||||
current = first_scrobble.timestamp.date()
|
||||
last_year = None
|
||||
last_week = None
|
||||
|
||||
while current <= now.date():
|
||||
year = current.year
|
||||
month = current.month
|
||||
day = current.day
|
||||
week = current.isocalendar()[1]
|
||||
|
||||
build_monthly_charts(user, year, month, media_types)
|
||||
build_daily_charts(user, year, month, day, media_types)
|
||||
|
||||
if last_week != week:
|
||||
build_weekly_charts(user, year, week, media_types)
|
||||
last_week = week
|
||||
|
||||
if last_year != year:
|
||||
build_yearly_charts(user, year, media_types)
|
||||
last_year = year
|
||||
|
||||
current += datetime.timedelta(days=1)
|
||||
|
||||
|
||||
def build_daily_charts(
|
||||
user, year: int, month: int, day: int, media_types: Optional[list] = None
|
||||
) -> None:
|
||||
"""Build daily charts for a specific day."""
|
||||
build_charts(
|
||||
user=user,
|
||||
year=year,
|
||||
month=month,
|
||||
day=day,
|
||||
media_types=media_types,
|
||||
)
|
||||
|
||||
|
||||
def rebuild_all_charts(user, media_types: Optional[list] = None) -> None:
|
||||
"""Delete all existing chart records for a user and rebuild them."""
|
||||
ChartRecord = apps.get_model("charts", "ChartRecord")
|
||||
deleted_count = ChartRecord.objects.filter(user=user).delete()[0]
|
||||
logger.info(f"Deleted {deleted_count} existing chart records for {user}")
|
||||
build_all_historical_charts(user, media_types)
|
||||
417
vrobbler/apps/charts/views.py
Normal file
417
vrobbler/apps/charts/views.py
Normal file
@ -0,0 +1,417 @@
|
||||
import calendar
|
||||
from datetime import timedelta
|
||||
|
||||
from charts.models import ChartRecord
|
||||
from django.db.models import Q
|
||||
from django.utils import timezone
|
||||
from django.views.generic import TemplateView
|
||||
from profiles.utils import now_user_timezone
|
||||
|
||||
MEDIA_TYPE_FILTERS = {
|
||||
"artist": Q(artist__isnull=False),
|
||||
"album": Q(album__isnull=False),
|
||||
"track": Q(track__isnull=False),
|
||||
"tv_series": Q(tv_series__isnull=False),
|
||||
"video": Q(video__isnull=False),
|
||||
"podcast": Q(podcast__isnull=False),
|
||||
"podcast_episode": Q(podcast_episode__isnull=False),
|
||||
"board_game": Q(board_game__isnull=False),
|
||||
"trail": Q(trail__isnull=False),
|
||||
"geo_location": Q(geo_location__isnull=False),
|
||||
"food": Q(food__isnull=False),
|
||||
"book": Q(book__isnull=False),
|
||||
}
|
||||
|
||||
|
||||
class ChartRecordView(TemplateView):
|
||||
template_name = "charts/chart_index.html"
|
||||
|
||||
def get_charts(
|
||||
self,
|
||||
user,
|
||||
media_type: str,
|
||||
year=None,
|
||||
month=None,
|
||||
week=None,
|
||||
day=None,
|
||||
limit=20,
|
||||
):
|
||||
params = {"user": user}
|
||||
if year is not None:
|
||||
params["year"] = year
|
||||
if month is not None:
|
||||
params["month"] = month
|
||||
if week is not None:
|
||||
params["week"] = week
|
||||
if day is not None:
|
||||
params["day"] = day
|
||||
|
||||
filter_key = media_type.lower()
|
||||
media_filter = MEDIA_TYPE_FILTERS.get(filter_key, Q())
|
||||
|
||||
return ChartRecord.objects.filter(media_filter, **params).order_by("rank")[
|
||||
:limit
|
||||
]
|
||||
|
||||
def get_charts_for_period(
|
||||
self,
|
||||
user,
|
||||
media_type: str,
|
||||
year=None,
|
||||
month=None,
|
||||
week=None,
|
||||
day=None,
|
||||
limit=20,
|
||||
):
|
||||
"""Get charts for a specific period, properly filtering by period type."""
|
||||
params = {"user": user}
|
||||
if year is not None:
|
||||
params["year"] = year
|
||||
if month is not None:
|
||||
params["month"] = month
|
||||
if week is not None:
|
||||
params["week"] = week
|
||||
if day is not None:
|
||||
params["day"] = day
|
||||
|
||||
filter_key = media_type.lower()
|
||||
media_filter = MEDIA_TYPE_FILTERS.get(filter_key, Q())
|
||||
|
||||
qs = ChartRecord.objects.filter(media_filter, **params)
|
||||
|
||||
if day is not None:
|
||||
qs = qs.filter(day__isnull=False)
|
||||
elif week is not None:
|
||||
qs = qs.filter(week__isnull=False, day__isnull=True)
|
||||
elif month is not None:
|
||||
qs = qs.filter(month__isnull=False, week__isnull=True, day__isnull=True)
|
||||
else:
|
||||
qs = qs.filter(month__isnull=True, week__isnull=True, day__isnull=True)
|
||||
|
||||
return qs.order_by("rank")[:limit]
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
user = self.request.user
|
||||
chart_type = self.request.GET.get("chart_type", "standard")
|
||||
context["chart_type"] = chart_type
|
||||
|
||||
date_param = self.request.GET.get("date")
|
||||
|
||||
now = timezone.now()
|
||||
if user.is_authenticated:
|
||||
now = now_user_timezone(user.profile)
|
||||
today = now.date()
|
||||
current_year = today.year
|
||||
current_month = today.month
|
||||
current_week = today.isocalendar()[1]
|
||||
current_day = today.day
|
||||
|
||||
context["current_year"] = current_year
|
||||
context["current_month"] = current_month
|
||||
context["current_week"] = current_week
|
||||
context["current_day"] = current_day
|
||||
|
||||
if chart_type == "maloja":
|
||||
context["chart_keys"] = {
|
||||
"today": "Today",
|
||||
"week": "This Week",
|
||||
"month": "This Month",
|
||||
"year": "This Year",
|
||||
"all": "All Time",
|
||||
}
|
||||
|
||||
context["maloja_charts"] = {
|
||||
"artist": {
|
||||
"today": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"artist",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
day=current_day,
|
||||
)
|
||||
),
|
||||
"week": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"artist",
|
||||
year=current_year,
|
||||
week=current_week,
|
||||
)
|
||||
),
|
||||
"month": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"artist",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
)
|
||||
),
|
||||
"year": list(
|
||||
self.get_charts_for_period(user, "artist", year=current_year)
|
||||
),
|
||||
"all": list(self.get_charts_for_period(user, "artist")),
|
||||
},
|
||||
"track": {
|
||||
"today": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"track",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
day=current_day,
|
||||
)
|
||||
),
|
||||
"week": list(
|
||||
self.get_charts_for_period(
|
||||
user, "track", year=current_year, week=current_week
|
||||
)
|
||||
),
|
||||
"month": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"track",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
)
|
||||
),
|
||||
"year": list(
|
||||
self.get_charts_for_period(user, "track", year=current_year)
|
||||
),
|
||||
"all": list(self.get_charts_for_period(user, "track")),
|
||||
},
|
||||
"tv_series": {
|
||||
"today": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"tv_series",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
day=current_day,
|
||||
)
|
||||
),
|
||||
"week": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"tv_series",
|
||||
year=current_year,
|
||||
week=current_week,
|
||||
)
|
||||
),
|
||||
"month": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"tv_series",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
)
|
||||
),
|
||||
"year": list(
|
||||
self.get_charts_for_period(user, "tv_series", year=current_year)
|
||||
),
|
||||
"all": list(self.get_charts_for_period(user, "tv_series")),
|
||||
},
|
||||
}
|
||||
return context
|
||||
|
||||
if not date_param:
|
||||
context["period"] = "current"
|
||||
context["year"] = current_year
|
||||
context["month"] = current_month
|
||||
context["week"] = current_week
|
||||
context["day"] = current_day
|
||||
|
||||
context["charts"] = {
|
||||
"artist": list(
|
||||
self.get_charts_for_period(
|
||||
user, "artist", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"album": list(
|
||||
self.get_charts_for_period(
|
||||
user, "album", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"track": list(
|
||||
self.get_charts_for_period(
|
||||
user, "track", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"tv_series": list(
|
||||
self.get_charts_for_period(
|
||||
user, "tv_series", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"video": list(
|
||||
self.get_charts_for_period(
|
||||
user, "video", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"board_game": list(
|
||||
self.get_charts_for_period(
|
||||
user, "board_game", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"book": list(
|
||||
self.get_charts_for_period(
|
||||
user, "book", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"food": list(
|
||||
self.get_charts_for_period(
|
||||
user, "food", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"podcast": list(
|
||||
self.get_charts_for_period(
|
||||
user, "podcast", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"trail": list(
|
||||
self.get_charts_for_period(
|
||||
user, "trail", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
}
|
||||
else:
|
||||
parts = date_param.split("-")
|
||||
year = int(parts[0])
|
||||
|
||||
week = None
|
||||
month = None
|
||||
day = None
|
||||
|
||||
if len(parts) >= 2 and parts[1].startswith("W"):
|
||||
week = int(parts[1].lstrip("W"))
|
||||
elif len(parts) >= 2 and parts[1]:
|
||||
try:
|
||||
month = int(parts[1])
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if len(parts) >= 3:
|
||||
if parts[2].startswith("W"):
|
||||
week = int(parts[2].lstrip("W"))
|
||||
elif not parts[2].startswith("W"):
|
||||
day = int(parts[2])
|
||||
|
||||
context["period"] = "historical"
|
||||
context["year"] = year
|
||||
context["month"] = month
|
||||
context["week"] = week
|
||||
context["day"] = day
|
||||
|
||||
period_str = str(year)
|
||||
if month:
|
||||
period_str = f"{calendar.month_name[month]} {period_str}"
|
||||
if week:
|
||||
period_str = f"Week {week}, {period_str}"
|
||||
if day:
|
||||
period_str = f"{calendar.month_name[month]} {day}, {year}"
|
||||
context["period_str"] = period_str
|
||||
|
||||
context["charts"] = {
|
||||
"artist": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"artist",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"album": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"album",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"track": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"track",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"tv_series": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"tv_series",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"video": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"video",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"board_game": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"board_game",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"book": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"book",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"food": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"food",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"podcast": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"podcast",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"trail": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"trail",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
}
|
||||
|
||||
return context
|
||||
Reference in New Issue
Block a user