Add MonthlyTrip model and MonthlyTripView

This commit is contained in:
Nyymix 2025-08-10 17:00:43 +03:00
parent 0a4f3da34c
commit 646845e44d
7 changed files with 169 additions and 42 deletions

View file

@ -1,17 +1,15 @@
from calendar import monthrange
from collections import defaultdict
from datetime import date, timedelta
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
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 django.views.generic import CreateView, ListView, TemplateView
from .forms import FuelPurchaseForm
from .models import FuelPurchase
from .models import FuelPurchase, MonthlyTrip
class FuelPurchaseMonthlyListView(ListView):
@ -25,17 +23,24 @@ class FuelPurchaseMonthlyListView(ListView):
self.current_year = year
self.current_month = month
start_date = date(year, month, 1)
end_date = date(year, month, monthrange(year, month)[1])
# Haetaan MonthlyTrip heti
self.monthly_trip = MonthlyTrip.objects.filter(year=year, month=month).first()
return FuelPurchase.objects.filter(
purchase_date__range=(start_date, end_date)
).order_by("-purchase_date")
if not self.monthly_trip:
# Jos ei ole olemassa, ei ole ostoksiakaan
return FuelPurchase.objects.none()
return (
FuelPurchase.objects
.filter(monthly_trip=self.monthly_trip)
.select_related("monthly_trip")
.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"),
@ -47,6 +52,7 @@ class FuelPurchaseMonthlyListView(ListView):
context["summary"] = summary
context["current_year"] = self.current_year
context["current_month"] = self.current_month
context["month_kilometers"] = self.monthly_trip.kilometers if self.monthly_trip else 0
# Kuukausinavigointi
current_date = date(self.current_year, self.current_month, 1)
@ -59,8 +65,9 @@ class FuelPurchaseMonthlyListView(ListView):
context["next_month"] = next_month.month
# Kaikki kuukaudet alasvetovalikkoon
all_dates = FuelPurchase.objects.dates("purchase_date", "month", order="DESC")
context["available_months"] = all_dates
context["available_months"] = FuelPurchase.objects.dates(
"purchase_date", "month", order="DESC"
)
return context
@ -68,5 +75,32 @@ class FuelPurchaseMonthlyListView(ListView):
class FuelPurchaseCreateView(LoginRequiredMixin, CreateView):
model = FuelPurchase
form_class = FuelPurchaseForm
template_name = "main/fuelpurchase_add.html"
template_name = "fuelpurchase_add.html"
success_url = "/"
class MonthlyTripView(TemplateView):
template_name = 'main/monthly_trips.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
all_distances = MonthlyTrip.objects.all().order_by('year', 'month')
years = sorted(set(d.year for d in all_distances))
table_data = defaultdict(dict)
year_totals = defaultdict(int)
for entry in all_distances:
table_data[entry.month][entry.year] = entry.kilometers
year_totals[entry.year] += entry.kilometers
month_names = dict(MonthlyTrip.MONTH_CHOICES)
context.update({
'years': years,
'table_data': table_data,
'month_names': month_names,
'year_totals': year_totals,
})
return context