From da62f7feb6d76432065dd7ae1c9d0f37d5522e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Madet?= Date: Fri, 20 Oct 2023 12:31:58 +0200 Subject: [PATCH] Small improvements in user shift account log. --- tapir/shifts/models.py | 10 +++++----- .../shifts/user_shift_account_log.html | 10 ++++++---- tapir/shifts/views/views.py | 17 ++++++++++++----- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/tapir/shifts/models.py b/tapir/shifts/models.py index efec0ca7a..8874f72b5 100644 --- a/tapir/shifts/models.py +++ b/tapir/shifts/models.py @@ -918,12 +918,12 @@ def get_upcoming_shift_attendances(self): slot__shift__start_time__gt=timezone.localtime() ).with_valid_state() - def get_account_balance(self): + def get_account_balance(self, at_date: datetime.datetime | None = None): + entries = self.user.shift_account_entries + if at_date: + entries = entries.filter(date__lte=at_date) # Might return None if no objects, so "or 0" - return ( - self.user.shift_account_entries.aggregate(balance=Sum("value"))["balance"] - or 0 - ) + return entries.aggregate(balance=Sum("value"))["balance"] or 0 def is_balance_ok(self): balance = self.get_account_balance() diff --git a/tapir/shifts/templates/shifts/user_shift_account_log.html b/tapir/shifts/templates/shifts/user_shift_account_log.html index 61dc2389b..06534a0af 100644 --- a/tapir/shifts/templates/shifts/user_shift_account_log.html +++ b/tapir/shifts/templates/shifts/user_shift_account_log.html @@ -27,15 +27,17 @@
{% translate 'Date' %} {% translate 'Value' %} + {% translate 'Balance at date' %} {% translate 'Description' %} - {% for entry in entries %} + {% for entry_data in entries_data %} - {{ entry.date }} - {{ entry.value }} - {{ entry.description }} + {{ entry_data.entry.date|date:"d.m.Y H:i" }} + {{ entry_data.entry.value }} + {{ entry_data.balance_at_date }} + {{ entry_data.entry.description }} {% endfor %} diff --git a/tapir/shifts/views/views.py b/tapir/shifts/views/views.py index 878503449..e4f392d40 100644 --- a/tapir/shifts/views/views.py +++ b/tapir/shifts/views/views.py @@ -128,7 +128,7 @@ def dictionary_get(dic, key): class UserShiftAccountLog(LoginRequiredMixin, PermissionRequiredMixin, TemplateView): template_name = "shifts/user_shift_account_log.html" - def get_target_user(self): + def get_target_user(self) -> TapirUser: return TapirUser.objects.get(pk=self.kwargs["user_pk"]) def get_permission_required(self): @@ -138,10 +138,17 @@ def get_permission_required(self): def get_context_data(self, **kwargs): context = super().get_context_data() - context["user"] = self.get_target_user() - context["entries"] = ShiftAccountEntry.objects.filter( - user=self.get_target_user() - ).order_by("-date") + user = self.get_target_user() + context["user"] = user + context["entries_data"] = [ + { + "entry": entry, + "balance_at_date": user.shift_user_data.get_account_balance( + at_date=entry.date + ), + } + for entry in ShiftAccountEntry.objects.filter(user=user).order_by("-date") + ] return context