diff options
author | luxagraf <sng@luxagraf.net> | 2022-11-12 10:50:36 -0600 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2022-11-12 10:50:36 -0600 |
commit | 07e428e80b74e61ee7fd11e3ed61e9315b012e68 (patch) | |
tree | 9f391fe70213fb4576f3f1eebbc47adc07dcad64 /app/budget/models.py | |
parent | 2db52b9aecda64220777983a5a4ce26b9eb237f6 (diff) |
bdgt: added some aggregate stats
Diffstat (limited to 'app/budget/models.py')
-rw-r--r-- | app/budget/models.py | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/app/budget/models.py b/app/budget/models.py index acf2b69..f4c6ec0 100644 --- a/app/budget/models.py +++ b/app/budget/models.py @@ -1,4 +1,10 @@ +import calendar +import datetime from django.db import models +from django.db.models import Sum +from django.urls import reverse +from django.utils import timezone + class LuxSource(models.Model): name = models.CharField(max_length=200) @@ -7,11 +13,29 @@ class LuxSource(models.Model): def __str__(self): return self.name + +class LuxPurchaseStatsManager(models.Manager): + + def get_monthly_spending(self, month=timezone.now().month): + last_day = calendar.monthrange(timezone.now().year, month)[1] + start_date = datetime.date(timezone.now().year, month, 1) + end_date = datetime.date(timezone.now().year, month, last_day) + return self.filter(date_recorded__range=(start_date, end_date)).aggregate(Sum('amount')) + + def get_monthly_spending_by_category(self, cat, number_of_months=1): + today = timezone.now() + month = today.replace(day=1).month + start_month = month - number_of_months + 1 + start_date = datetime.date(timezone.now().year, start_month, 1) + last_day = calendar.monthrange(timezone.now().year, month)[1] + end_date = datetime.date(timezone.now().year, month, last_day) + return self.filter(date_recorded__range=(start_date, end_date)).filter(category=cat).aggregate(Sum('amount')) + class LuxPurchase(models.Model): - amount = models.IntegerField() + amount = models.DecimalField(max_digits=6, decimal_places=2) source = models.ForeignKey(LuxSource, on_delete=models.CASCADE) CATEGORY = ( - (0, 'Grocery and Home'), + (0, 'Grocery & Home'), (1, 'Gas'), (2, 'Bus'), (3, 'Lodging'), @@ -27,5 +51,11 @@ class LuxPurchase(models.Model): ordering = ('-date_recorded',) def __str__(self): - return self.name + return "%s - %s" %(self.amount, self.source.name) + + def get_absolute_url(self): + return reverse("luxbudget:detail", kwargs={"pk": self.pk}) + + objects = models.Manager() # The default manager. + stats = LuxPurchaseStatsManager() |