From 783e5e92cbb58544e8c1075f82e32faeb54a8343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9DNyymix=E2=80=9D?= Date: Sat, 9 Aug 2025 20:37:38 +0300 Subject: [PATCH] Add monthly list and summary data --- main/templates/main/fuelpurchase_add.html | 2 +- main/templates/main/fuelpurchase_list.html | 41 +++++++++++++-- main/urls.py | 3 +- main/views.py | 59 +++++++++++++++++++--- 4 files changed, 92 insertions(+), 13 deletions(-) diff --git a/main/templates/main/fuelpurchase_add.html b/main/templates/main/fuelpurchase_add.html index 54b88e6..f6b097b 100644 --- a/main/templates/main/fuelpurchase_add.html +++ b/main/templates/main/fuelpurchase_add.html @@ -24,7 +24,7 @@ {% endfor %} - Cancel + Cancel diff --git a/main/templates/main/fuelpurchase_list.html b/main/templates/main/fuelpurchase_list.html index 45c6ee0..239c9b0 100644 --- a/main/templates/main/fuelpurchase_list.html +++ b/main/templates/main/fuelpurchase_list.html @@ -1,20 +1,53 @@ {% extends "base.html" %} -{% block title %}Fuel Purchases{% endblock %} +{% block title %}Fuel Purchases - {{ current_month }}.{{ current_year }}{% endblock %} {% block content %}
-

Fuel Purchases

+

+ Fuel Purchases – {{ current_month }}.{{ current_year }} +

Add Purchase
+ +
+ ← Previous + +
+ +
+ + Next → +
+ + +
+

Summary

+ +
+ + {% if purchases %} - + @@ -30,6 +63,6 @@
Date Total Cost (€)Price/Litre (€)Litre Price (€) Amount (litres)
{% else %} -

No purchases recorded yet.

+

No purchases recorded for this month.

{% endif %} {% endblock %} diff --git a/main/urls.py b/main/urls.py index dd70134..4be77b0 100644 --- a/main/urls.py +++ b/main/urls.py @@ -3,6 +3,7 @@ from django.urls import path from . import views urlpatterns = [ - path("", views.FuelPurchaseListView.as_view(), name="fuelpurchase_list"), + path("", views.FuelPurchaseMonthlyListView.as_view(), name="fuelpurchase_list_current"), + path("//", views.FuelPurchaseMonthlyListView.as_view(), name="fuelpurchase_list"), path("add/", views.FuelPurchaseCreateView.as_view(), name="fuelpurchase_add"), ] diff --git a/main/views.py b/main/views.py index 2c54c44..2a00f61 100644 --- a/main/views.py +++ b/main/views.py @@ -1,21 +1,66 @@ +from calendar import monthrange +from datetime import date, timedelta + +from django.db.models import Avg, Max, Min, Sum from django.http import HttpResponse from django.shortcuts import render from django.urls import reverse_lazy +from django.utils import timezone from django.views.generic import CreateView, ListView from .forms import FuelPurchaseForm from .models import FuelPurchase -def index(request): - return HttpResponse("Hello, world.") - - -class FuelPurchaseListView(ListView): +class FuelPurchaseMonthlyListView(ListView): model = FuelPurchase - template_name = "main/fuelpurchase_list.html" + template_name = "fuelpurchase_list.html" context_object_name = "purchases" - ordering = ['-purchase_date'] + + def get_queryset(self): + year = int(self.kwargs.get("year", timezone.now().year)) + month = int(self.kwargs.get("month", timezone.now().month)) + self.current_year = year + self.current_month = month + + start_date = date(year, month, 1) + end_date = date(year, month, monthrange(year, month)[1]) + + return FuelPurchase.objects.filter( + purchase_date__range=(start_date, end_date) + ).order_by("-purchase_date") + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + + qs = self.object_list + summary = qs.aggregate( + total_cost_sum=Sum("total_cost"), + total_litres_sum=Sum("amount_litres"), + avg_price=Avg("price_per_litre"), + min_price=Min("price_per_litre"), + max_price=Max("price_per_litre"), + ) + + context["summary"] = summary + context["current_year"] = self.current_year + context["current_month"] = self.current_month + + # Kuukausinavigointi + current_date = date(self.current_year, self.current_month, 1) + prev_month = current_date - timedelta(days=1) + next_month = (current_date.replace(day=28) + timedelta(days=4)).replace(day=1) + + context["prev_year"] = prev_month.year + context["prev_month"] = prev_month.month + context["next_year"] = next_month.year + context["next_month"] = next_month.month + + # Kaikki kuukaudet alasvetovalikkoon + all_dates = FuelPurchase.objects.dates("purchase_date", "month", order="DESC") + context["available_months"] = all_dates + + return context class FuelPurchaseCreateView(CreateView):