Move function definition to utils

This commit is contained in:
Edgar P. Burkhart 2023-04-20 09:19:15 +02:00
parent 5a0958d52c
commit 4792388ccd
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
3 changed files with 61 additions and 55 deletions

View file

@ -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")

52
nummi/main/utils.py Normal file
View file

@ -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(),
),
}

View file

@ -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):