Add history function to view
This commit is contained in:
parent
5534a4352d
commit
917531aa97
1 changed files with 24 additions and 56 deletions
|
@ -34,6 +34,27 @@ from .forms import (
|
||||||
from .models import Account, Category, Invoice, Snapshot, Transaction
|
from .models import Account, Category, Invoice, Snapshot, Transaction
|
||||||
|
|
||||||
|
|
||||||
|
def history(transaction_set):
|
||||||
|
_history = (
|
||||||
|
transaction_set.values(month=models.functions.TruncMonth("date"))
|
||||||
|
.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,
|
||||||
|
"max": max(
|
||||||
|
_history.aggregate(
|
||||||
|
max=models.Max("sum_p", default=0),
|
||||||
|
min=-models.Min("sum_m", default=0),
|
||||||
|
).values(),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class IndexView(LoginRequiredMixin, TemplateView):
|
class IndexView(LoginRequiredMixin, TemplateView):
|
||||||
template_name = "main/index.html"
|
template_name = "main/index.html"
|
||||||
|
|
||||||
|
@ -41,31 +62,13 @@ class IndexView(LoginRequiredMixin, TemplateView):
|
||||||
_max = 8
|
_max = 8
|
||||||
_transactions = Transaction.objects.filter(user=self.request.user)
|
_transactions = Transaction.objects.filter(user=self.request.user)
|
||||||
_snapshots = Snapshot.objects.filter(user=self.request.user)
|
_snapshots = Snapshot.objects.filter(user=self.request.user)
|
||||||
_history = (
|
|
||||||
_transactions.filter(category__budget=True)
|
|
||||||
.values(month=models.functions.TruncMonth("date"))
|
|
||||||
.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")
|
|
||||||
)
|
|
||||||
|
|
||||||
res = {
|
res = {
|
||||||
"accounts": Account.objects.filter(user=self.request.user),
|
"accounts": Account.objects.filter(user=self.request.user),
|
||||||
"transactions": _transactions[:_max],
|
"transactions": _transactions[:_max],
|
||||||
"categories": Category.objects.filter(user=self.request.user),
|
"categories": Category.objects.filter(user=self.request.user),
|
||||||
"snapshots": _snapshots[:_max],
|
"snapshots": _snapshots[:_max],
|
||||||
"history": {
|
"history": history(_transactions.filter(category__budget=True)),
|
||||||
"data": _history,
|
|
||||||
"max": max(
|
|
||||||
_history.aggregate(
|
|
||||||
max=models.Max("sum_p"),
|
|
||||||
min=-models.Min("sum_m"),
|
|
||||||
).values(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if _transactions.count() > _max:
|
if _transactions.count() > _max:
|
||||||
res["transactions_url"] = reverse_lazy("transactions")
|
res["transactions_url"] = reverse_lazy("transactions")
|
||||||
|
@ -194,31 +197,13 @@ class AccountUpdateView(NummiUpdateView):
|
||||||
"account_snapshots", args=(account.pk,)
|
"account_snapshots", args=(account.pk,)
|
||||||
)
|
)
|
||||||
|
|
||||||
_history = (
|
|
||||||
account.transaction_set.values(month=models.functions.TruncMonth("date"))
|
|
||||||
.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 | {
|
return data | {
|
||||||
"transactions": _transactions[:8],
|
"transactions": _transactions[:8],
|
||||||
"new_snapshot_url": reverse_lazy(
|
"new_snapshot_url": reverse_lazy(
|
||||||
"snapshot", kwargs={"account": account.pk}
|
"snapshot", kwargs={"account": account.pk}
|
||||||
),
|
),
|
||||||
"snapshots": _snapshots[:8],
|
"snapshots": _snapshots[:8],
|
||||||
"history": {
|
"history": history(account.transaction_set),
|
||||||
"data": _history,
|
|
||||||
"max": max(
|
|
||||||
_history.aggregate(
|
|
||||||
max=models.Max("sum_p", default=0),
|
|
||||||
min=-models.Min("sum_m", default=0),
|
|
||||||
).values(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -256,30 +241,13 @@ class CategoryUpdateView(NummiUpdateView):
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
data = super().get_context_data(**kwargs)
|
data = super().get_context_data(**kwargs)
|
||||||
category = data["form"].instance
|
category = data["form"].instance
|
||||||
_history = (
|
|
||||||
category.transaction_set.values(month=models.functions.TruncMonth("date"))
|
|
||||||
.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 | {
|
return data | {
|
||||||
"transactions": category.transaction_set.all()[:8],
|
"transactions": category.transaction_set.all()[:8],
|
||||||
"transactions_url": reverse_lazy(
|
"transactions_url": reverse_lazy(
|
||||||
"category_transactions", args=(category.pk,)
|
"category_transactions", args=(category.pk,)
|
||||||
),
|
),
|
||||||
"history": {
|
"history": history(category.transaction_set),
|
||||||
"data": _history,
|
|
||||||
"max": max(
|
|
||||||
_history.aggregate(
|
|
||||||
max=models.Max("sum_p", default=0),
|
|
||||||
min=-models.Min("sum_m", default=0),
|
|
||||||
).values(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue