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)