Add stats link, year selector

This commit is contained in:
Lukáš Kucharczyk 2023-11-02 09:20:09 +01:00
parent 3ee36932c3
commit 71b90b8202
6 changed files with 31 additions and 7 deletions

View File

@ -1,5 +1,9 @@
## Unreleased
### New
* Add Stats to the main navigation
* Allow selecting year on the Stats page
### Fixed
* Correctly limit sessions to a single year for stats

View File

@ -791,11 +791,6 @@ select {
margin-bottom: 1rem;
}
.my-5 {
margin-top: 1.25rem;
margin-bottom: 1.25rem;
}
.my-6 {
margin-top: 1.5rem;
margin-bottom: 1.5rem;
@ -805,6 +800,10 @@ select {
margin-bottom: 0.25rem;
}
.mb-10 {
margin-bottom: 2.5rem;
}
.mb-4 {
margin-bottom: 1rem;
}
@ -829,6 +828,10 @@ select {
display: block;
}
.inline-block {
display: inline-block;
}
.inline {
display: inline;
}

View File

@ -38,6 +38,7 @@
<li><a class="block py-2 pl-3 pr-4 hover:underline" href="{% url 'add_device' %}">New Device</a></li>
{% endif %}
{% if session_count > 0 %}
<li><a class="block py-2 pl-3 pr-4 hover:underline" href="{% url 'stats_current_year' %}">Stats</a></li>
<li><a class="block py-2 pl-3 pr-4 hover:underline" href="{% url 'list_sessions' %}">All Sessions</a></li>
{% endif %}
</ul>

View File

@ -6,7 +6,15 @@
{% block content %}
<div class="dark:text-white max-w-sm sm:max-w-xl lg:max-w-3xl mx-auto">
<h1 class="text-5xl text-center my-6">Stats for {{ year }}</h1>
<div class="flex justify-center items-center">
<form method="get" class="text-center">
<label class="text-5xl text-center inline-block mb-10" for="yearSelect">Stats for:</label>
<select name="year" id="yearSelect" onchange="this.form.submit();" class="mx-2">
<option value="2022" {% if year == 2022 %}selected{% endif %}>2022</option>
<option value="2023" {% if year == 2023 %}selected{% endif %}>2023</option>
</select>
</form>
</div>
<table class="responsive-table">
<thead>
<tr>

View File

@ -73,6 +73,7 @@ urlpatterns = [
{"filter": "ownership_type"},
name="list_sessions_by_ownership_type",
),
path("stats/", views.stats, name="stats_current_year"),
path(
"stats/<int:year>",
views.stats,

View File

@ -6,6 +6,8 @@ from common.time import format_duration
from django.conf import settings
from django.shortcuts import redirect, render
from django.db.models import Sum, F
from django.http import HttpResponseRedirect
from django.urls import reverse
from .forms import (
GameForm,
@ -230,7 +232,12 @@ def list_sessions(
return render(request, "list_sessions.html", context)
def stats(request, year: int):
def stats(request, year: int = 0):
selected_year = request.GET.get("year")
if selected_year:
return HttpResponseRedirect(reverse("stats_by_year", args=[selected_year]))
if year == 0:
year = now_with_tz().year
first_day_of_year = datetime(year, 1, 1)
last_day_of_year = datetime(year + 1, 1, 1)
year_sessions = Session.objects.filter(