- Yearly view: replace N*12 queries with annotate/ExtractMonth (6 queries) - Fixed expenses: bulk_create instead of get_or_create loop - Use DB aggregate(Sum) instead of Python sum() for totals - Remove 10 unused CRUD views, 13 URLs, 6 templates, 3 forms - Fix overly broad Exception catch in save_expense - Move update_budget to app/monthly/services.py (no cross-handler imports)
29 lines
734 B
Python
29 lines
734 B
Python
from decimal import Decimal, InvalidOperation
|
|
|
|
from liveview import liveview_handler
|
|
|
|
from app.monthly.models import MonthlyNote
|
|
from app.monthly.services import update_budget
|
|
|
|
|
|
@liveview_handler("save_savings_target")
|
|
def save_savings_target(consumer, content):
|
|
form = content.get("form", {})
|
|
year = int(form.get("year", 0))
|
|
month = int(form.get("month", 0))
|
|
amount_raw = form.get("savings_amount", "").replace(",", ".")
|
|
|
|
if not year or not month:
|
|
return
|
|
|
|
try:
|
|
amount = Decimal(amount_raw) if amount_raw else Decimal("0")
|
|
except (InvalidOperation, ValueError):
|
|
return
|
|
|
|
note, _ = MonthlyNote.objects.get_or_create(year=year, month=month)
|
|
note.savings_target = amount
|
|
note.save()
|
|
|
|
update_budget(consumer, year, month)
|