Files
gaehsnitz/gaehsnitz/views.py
2022-09-08 19:01:49 +02:00

67 lines
2.3 KiB
Python

from collections import defaultdict
from datetime import date, timezone
from django.utils import timezone
from django.views.generic import TemplateView
from gaehsnitz.models import Payment
festival_start_date, festival_end_date = date(2022, 8, 25), date(2022, 8, 28)
class GaehsnitzTemplateView(TemplateView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
delta_til_start = festival_start_date - date.today()
days_til_start = max(delta_til_start.days, 0)
context.update({
"days_til_festival_start": days_til_start,
})
return context
class NewsView(GaehsnitzTemplateView):
template_name = "gaehsnitz/news.html"
class AToZView(GaehsnitzTemplateView):
template_name = "gaehsnitz/atoz.html"
class ProgramView(GaehsnitzTemplateView):
template_name = "gaehsnitz/program.html"
class FinanceView(GaehsnitzTemplateView):
template_name = "gaehsnitz/finance.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
balance_dict = defaultdict(lambda: defaultdict(int))
detailed_payments = defaultdict(list)
overall_sum = 0
for payment in Payment.objects.filter(date__year=timezone.now().year).order_by("topic", "date"):
topic_name = Payment.Topic(payment.topic).label
sign = 1 if payment.is_incoming() else -1
balance_dict[topic_name]["total"] += sign * payment.amount
if payment.is_estimated:
balance_dict[topic_name]["estimated"] += sign * payment.amount
overall_sum += sign * payment.amount
if payment.is_outgoing() and payment.topic != Payment.Topic.bands:
detailed_payments[topic_name].append(payment)
context.update({
# apparently defaultdicts do not work
"balance_dict": dict(balance_dict),
"overall_sum": overall_sum,
"detailed_payments": dict(detailed_payments),
# drinks are delivered and no longer estimated, thus the table is not relevant any more
# "drinks_table": generate_drinks_table(),
})
return context
class ForBandsView(GaehsnitzTemplateView):
template_name = "gaehsnitz/for-bands.html"