Add monthly list and summary data
This commit is contained in:
parent
fa19b593d8
commit
783e5e92cb
4 changed files with 92 additions and 13 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue