From 57279b1cda6abcc6ba7a3bfc64b2993f721dd596 Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Tue, 2 Jan 2024 14:21:58 +0100
Subject: [PATCH] Simplify duplicated code (archive views)

---
 nummi/transaction/views.py | 79 +++++++++++++++-----------------------
 1 file changed, 31 insertions(+), 48 deletions(-)

diff --git a/nummi/transaction/views.py b/nummi/transaction/views.py
index fb1a900..2421c12 100644
--- a/nummi/transaction/views.py
+++ b/nummi/transaction/views.py
@@ -110,68 +110,51 @@ class TransactionListView(NummiListView):
     context_object_name = "transactions"
 
 
-class TransactionMonthView(UserMixin, MonthArchiveView):
+class TransactionACMixin:
+    model = Transaction
+
+    def get_queryset(self):
+        if "account" in self.kwargs:
+            self.account = get_object_or_404(
+                Account.objects.filter(user=self.request.user),
+                pk=self.kwargs["account"],
+            )
+            return super().get_queryset().filter(account=self.account)
+        if "category" in self.kwargs:
+            self.category = get_object_or_404(
+                Category.objects.filter(user=self.request.user),
+                pk=self.kwargs["category"],
+            )
+            return super().get_queryset().filter(category=self.category)
+
+        return super().get_queryset()
+
+    def get_context_data(self, **kwargs):
+        context_data = super().get_context_data(**kwargs)
+        if "category" in self.kwargs:
+            return context_data | {"category": self.category}
+        if "account" in self.kwargs:
+            return context_data | {"account": self.account}
+        return context_data
+
+
+class TransactionMonthView(UserMixin, TransactionACMixin, MonthArchiveView):
     model = Transaction
     date_field = "date"
     context_object_name = "transactions"
     month_format = "%m"
 
-    def get_queryset(self):
-        if "account" in self.kwargs:
-            self.account = get_object_or_404(
-                Account.objects.filter(user=self.request.user),
-                pk=self.kwargs["account"],
-            )
-            return super().get_queryset().filter(account=self.account)
-        if "category" in self.kwargs:
-            self.category = get_object_or_404(
-                Category.objects.filter(user=self.request.user),
-                pk=self.kwargs["category"],
-            )
-            return super().get_queryset().filter(category=self.category)
 
-        return super().get_queryset()
-
-    def get_context_data(self, **kwargs):
-        context_data = super().get_context_data(**kwargs)
-        if "category" in self.kwargs:
-            return context_data | {"category": self.category}
-        if "account" in self.kwargs:
-            return context_data | {"account": self.account}
-        return context_data
-
-
-class TransactionYearView(UserMixin, YearArchiveView):
+class TransactionYearView(UserMixin, TransactionACMixin, YearArchiveView):
     model = Transaction
     date_field = "date"
     context_object_name = "transactions"
     make_object_list = True
 
-    def get_queryset(self):
-        if "account" in self.kwargs:
-            self.account = get_object_or_404(
-                Account.objects.filter(user=self.request.user),
-                pk=self.kwargs["account"],
-            )
-            return super().get_queryset().filter(account=self.account)
-        if "category" in self.kwargs:
-            self.category = get_object_or_404(
-                Category.objects.filter(user=self.request.user),
-                pk=self.kwargs["category"],
-            )
-            return super().get_queryset().filter(category=self.category)
-
-        return super().get_queryset()
-
     def get_context_data(self, **kwargs):
         context_data = super().get_context_data(**kwargs)
-        context_data |= {
+        return context_data | {
             "history": history(
                 context_data["transactions"].exclude(category__budget=False)
             ),
         }
-        if "category" in self.kwargs:
-            return context_data | {"category": self.category}
-        if "account" in self.kwargs:
-            return context_data | {"account": self.account}
-        return context_data
