[templates] Fix navigation and add AGENTS file
This commit is contained in:
@ -47,6 +47,42 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block lists %}
|
||||
<div class="row mb-3">
|
||||
<div class="col-auto">
|
||||
{% if prev_period %}
|
||||
<a href="{{ prev_period }}" class="btn btn-outline-secondary btn-sm">← Previous</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="col-auto text-center">
|
||||
<span class="badge bg-secondary">{{ period_type|title }}</span>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
{% if next_period %}
|
||||
<a href="{{ next_period }}" class="btn btn-outline-secondary btn-sm">Next →</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="col-auto ms-auto">
|
||||
{% if chart_years %}
|
||||
<div class="btn-group btn-group-sm">
|
||||
{% for y in chart_years|slice:":5" %}
|
||||
<a href="/charts/?date={{ y }}" class="btn btn-sm {% if year == y %}btn-primary{% else %}btn-outline-secondary{% endif %}">{{ y }}</a>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<small class="text-muted me-2">Months:</small>
|
||||
{% for m in "1,2,3,4,5,6,7,8,9,10,11,12"|split:"," %}
|
||||
<a href="/charts/?date={{ year }}-{{ m|stringformat:"02d" }}" class="btn btn-xs {% if month == m %}btn-primary{% else %}btn-outline-secondary{% endif %}" style="padding:1px 4px;font-size:10px;">{{ m }}</a>
|
||||
{% endfor %}
|
||||
<span class="text-muted mx-2">|</span>
|
||||
<small class="text-muted me-2">Weeks:</small>
|
||||
{% for w in "1,4,8,13,17,21,26,30,34,39,43,47,52"|split:"," %}
|
||||
<a href="/charts/?date={{ year }}-W{{ w|stringformat:"02d" }}" class="btn btn-xs {% if week == w %}btn-primary{% else %}btn-outline-secondary{% endif %}" style="padding:1px 3px;font-size:9px;">{{ w }}</a>
|
||||
{% endfor %}
|
||||
<a href="#" class="btn btn-xs btn-outline-secondary" style="padding:1px 3px;font-size:9px;" onclick="var w=prompt('Week number (1-52):');if(w)this.href='/charts/?date={{ year }}-W'+w.padStart(2,'0');return false;">+</a>
|
||||
</div>
|
||||
{% if chart_type == "maloja" %}
|
||||
{% include "scrobbles/_top_charts.html" %}
|
||||
{% else %}
|
||||
|
||||
@ -8,3 +8,8 @@ def get_item(dictionary, key):
|
||||
if isinstance(dictionary, dict):
|
||||
return dictionary.get(key)
|
||||
return None
|
||||
|
||||
|
||||
@register.filter
|
||||
def split(value, arg):
|
||||
return value.split(arg)
|
||||
|
||||
@ -414,4 +414,145 @@ class ChartRecordView(TemplateView):
|
||||
),
|
||||
}
|
||||
|
||||
context["chart_years"] = self.get_available_years(user)
|
||||
context["period_type"] = self.get_period_type()
|
||||
context["prev_period"] = self.get_prev_period_url(user)
|
||||
context["next_period"] = self.get_next_period_url(user)
|
||||
|
||||
return context
|
||||
|
||||
def get_available_years(self, user):
|
||||
return list(
|
||||
ChartRecord.objects.filter(user=user)
|
||||
.values_list("year", flat=True)
|
||||
.distinct()
|
||||
.order_by("-year")
|
||||
)
|
||||
|
||||
def get_period_type(self):
|
||||
date_param = self.request.GET.get("date")
|
||||
if not date_param:
|
||||
return "current"
|
||||
parts = date_param.split("-")
|
||||
if len(parts) >= 3 and not parts[2].startswith("W"):
|
||||
return "day"
|
||||
if len(parts) >= 2 and parts[1].startswith("W"):
|
||||
return "week"
|
||||
if len(parts) >= 2:
|
||||
return "month"
|
||||
return "year"
|
||||
|
||||
def parse_date_params(self):
|
||||
date_param = self.request.GET.get("date")
|
||||
if not date_param:
|
||||
return None, None, None, None
|
||||
|
||||
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:
|
||||
try:
|
||||
month = int(parts[1])
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if len(parts) >= 3:
|
||||
if parts[2].startswith("W"):
|
||||
week = int(parts[2].lstrip("W"))
|
||||
else:
|
||||
day = int(parts[2])
|
||||
|
||||
return year, month, week, day
|
||||
|
||||
def get_prev_period_url(self, user):
|
||||
period_type = self.get_period_type()
|
||||
|
||||
if period_type == "current":
|
||||
return None
|
||||
|
||||
year, month, week, day = self.parse_date_params()
|
||||
if year is None:
|
||||
return None
|
||||
|
||||
available_years = self.get_available_years(user)
|
||||
|
||||
if period_type == "day" and month and day:
|
||||
if month == 1:
|
||||
prev_month = 12
|
||||
prev_year = year - 1
|
||||
else:
|
||||
prev_month = month - 1
|
||||
prev_year = year
|
||||
return f"/charts/?date={prev_year}-{prev_month:02d}"
|
||||
elif period_type == "month" and month:
|
||||
if month == 1:
|
||||
prev_month = 12
|
||||
prev_year = year - 1
|
||||
else:
|
||||
prev_month = month - 1
|
||||
prev_year = year
|
||||
return f"/charts/?date={prev_year}-{prev_month:02d}"
|
||||
elif period_type == "week" and week:
|
||||
if week == 1:
|
||||
prev_week = 52
|
||||
prev_year = year - 1
|
||||
else:
|
||||
prev_week = week - 1
|
||||
prev_year = year
|
||||
return f"/charts/?date={prev_year}-W{prev_week:02d}"
|
||||
elif period_type == "year":
|
||||
if year in available_years:
|
||||
idx = available_years.index(year)
|
||||
if idx + 1 < len(available_years):
|
||||
return f"/charts/?date={available_years[idx + 1]}"
|
||||
return f"/charts/?date={year - 1}"
|
||||
return None
|
||||
|
||||
def get_next_period_url(self, user):
|
||||
period_type = self.get_period_type()
|
||||
|
||||
if period_type == "current":
|
||||
return None
|
||||
|
||||
year, month, week, day = self.parse_date_params()
|
||||
if year is None:
|
||||
return None
|
||||
|
||||
available_years = self.get_available_years(user)
|
||||
|
||||
if period_type == "day" and month and day:
|
||||
if month == 12:
|
||||
next_month = 1
|
||||
next_year = year + 1
|
||||
else:
|
||||
next_month = month + 1
|
||||
next_year = year
|
||||
return f"/charts/?date={next_year}-{next_month:02d}"
|
||||
elif period_type == "month" and month:
|
||||
if month == 12:
|
||||
next_month = 1
|
||||
next_year = year + 1
|
||||
else:
|
||||
next_month = month + 1
|
||||
next_year = year
|
||||
return f"/charts/?date={next_year}-{next_month:02d}"
|
||||
elif period_type == "week" and week:
|
||||
if week == 52:
|
||||
next_week = 1
|
||||
next_year = year + 1
|
||||
else:
|
||||
next_week = week + 1
|
||||
next_year = year
|
||||
return f"/charts/?date={next_year}-W{next_week:02d}"
|
||||
elif period_type == "year":
|
||||
if year in available_years:
|
||||
idx = available_years.index(year)
|
||||
if idx > 0:
|
||||
return f"/charts/?date={available_years[idx - 1]}"
|
||||
return f"/charts/?date={year + 1}"
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user