From 4792388ccdd852044dce75a416687905d75bb5b1 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Thu, 20 Apr 2023 09:19:15 +0200 Subject: [PATCH] Move function definition to utils --- nummi/main/models.py | 23 +++++++------------- nummi/main/utils.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ nummi/main/views.py | 41 +--------------------------------- 3 files changed, 61 insertions(+), 55 deletions(-) create mode 100644 nummi/main/utils.py diff --git a/nummi/main/models.py b/nummi/main/models.py index ffcd65d..3b0ed62 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -9,6 +9,8 @@ from django.urls import reverse from django.utils.text import format_lazy from django.utils.translation import gettext_lazy as _ +from .utils import get_path + class UserModel(models.Model): user = models.ForeignKey( @@ -22,7 +24,7 @@ class UserModel(models.Model): abstract = True -class CustomModel(UserModel): +class NummiModel(UserModel): @property def adding(self): return self._state.adding @@ -31,16 +33,7 @@ class CustomModel(UserModel): abstract = True -def get_path(instance, filename): - return pathlib.Path( - "user", - str(instance.user.username), - instance._meta.model_name, - str(instance.pk), - ).with_suffix(".pdf") - - -class Account(CustomModel): +class Account(NummiModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) name = models.CharField(max_length=64, default=_("Account"), verbose_name=_("Name")) icon = models.SlugField( @@ -80,7 +73,7 @@ class Account(CustomModel): verbose_name_plural = _("Accounts") -class AccountModel(CustomModel): +class AccountModel(NummiModel): account = models.ForeignKey( Account, on_delete=models.CASCADE, @@ -91,7 +84,7 @@ class AccountModel(CustomModel): abstract = True -class Category(CustomModel): +class Category(NummiModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) name = models.CharField( max_length=64, default=_("Category"), verbose_name=_("Name") @@ -202,7 +195,7 @@ class Snapshot(AccountModel): verbose_name_plural = _("Statements") -class Transaction(CustomModel): +class Transaction(NummiModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) name = models.CharField( max_length=256, default=_("Transaction"), verbose_name=_("Name") @@ -272,7 +265,7 @@ class Transaction(CustomModel): verbose_name_plural = _("Transactions") -class Invoice(CustomModel): +class Invoice(NummiModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) name = models.CharField( max_length=256, default=_("Invoice"), verbose_name=_("Name") diff --git a/nummi/main/utils.py b/nummi/main/utils.py new file mode 100644 index 0000000..f034df6 --- /dev/null +++ b/nummi/main/utils.py @@ -0,0 +1,52 @@ +import pathlib + +from django.db import models +from django.db.models import Func, Max, Min, Q, Sum, Value +from django.db.models.functions import Now, TruncMonth + + +def get_path(instance, filename): + return pathlib.Path( + "user", + str(instance.user.username), + instance._meta.model_name, + str(instance.pk), + ).with_suffix(".pdf") + + +class GenerateMonth(Func): + function = "generate_series" + template = "%(function)s(%(expressions)s, '1 month')::date" + + +def history(transaction_set): + _transaction_month = transaction_set.values(month=TruncMonth("date")).order_by( + "-date" + ) + _months = ( + transaction_set.values( + month=GenerateMonth( + _transaction_month.last()["month"], + Now(output_field=models.DateField()), + ) + ) + .annotate(sum_m=Value(0), sum_p=Value(0), sum=Value(0)) + .difference( + _transaction_month.annotate(sum_m=Value(0), sum_p=Value(0), sum=Value(0)) + ) + ) + _history = _transaction_month.annotate( + sum_p=Sum("value", filter=Q(value__gt=0)), + sum_m=Sum("value", filter=Q(value__lt=0)), + sum=Sum("value"), + ).order_by("-month") + + return { + "data": _history.union(_months).order_by("-month"), + "max": max( + _history.aggregate( + max=Max("sum_p", default=0), + min=-Min("sum_m", default=0), + ).values(), + ), + } diff --git a/nummi/main/views.py b/nummi/main/views.py index 552d6a5..40f5276 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -32,46 +32,7 @@ from .forms import ( TransactionForm, ) from .models import Account, Category, Invoice, Snapshot, Transaction - - -class GenerateMonth(models.Func): - function = "generate_series" - template = "%(function)s(%(expressions)s, '1 month')::date" - - -def history(transaction_set): - _transaction_month = transaction_set.values( - month=models.functions.TruncMonth("date") - ).order_by("-date") - _months = ( - transaction_set.values( - month=GenerateMonth( - _transaction_month.last()["month"], - models.functions.Now(output_field=models.DateField()), - ) - ) - .annotate(sum_m=models.Value(0), sum_p=models.Value(0), sum=models.Value(0)) - .difference( - _transaction_month.annotate( - sum_m=models.Value(0), sum_p=models.Value(0), sum=models.Value(0) - ) - ) - ) - _history = _transaction_month.annotate( - sum_p=models.Sum("value", filter=models.Q(value__gt=0)), - sum_m=models.Sum("value", filter=models.Q(value__lt=0)), - sum=models.Sum("value"), - ).order_by("-month") - - return { - "data": _history.union(_months).order_by("-month"), - "max": max( - _history.aggregate( - max=models.Max("sum_p", default=0), - min=-models.Min("sum_m", default=0), - ).values(), - ), - } +from .utils import history class IndexView(LoginRequiredMixin, TemplateView):