From da704c994399ee7bd776f417021d2be1bb8ab821 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Mon, 8 Jun 2020 15:27:38 +0200 Subject: [PATCH] fixed budget reporting in email The reported available rest of the budget should be calculated via an excel formula. This lead to the warning in excel, that this file was downloaded from the internet and you need to accept this warning for the calculation to happen. With this fix, the rest of the budget is directly entered into the excel sheet. --- superx_budget/pyramid/overview.py | 34 +++++++++++++------ .../pyramid/templates/overview.jinja2 | 5 ++- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/superx_budget/pyramid/overview.py b/superx_budget/pyramid/overview.py index 51ea0bc..b77a6ed 100644 --- a/superx_budget/pyramid/overview.py +++ b/superx_budget/pyramid/overview.py @@ -26,6 +26,22 @@ bei Fragen bitte an Holgi wenden """ +def _map_from_form_data(request, prefix): + if not prefix.endswith("-"): + prefix = f"{prefix}-" + result = {} + for key, value in request.POST.items(): + if key.startswith(prefix): + row_str = key.split("-")[-1] + row = int(row_str) + try: + value = float(value) + except ValueError: + value = 0 + result[row] = value + return result + + @view_config( context=Root, request_method="GET", @@ -112,22 +128,17 @@ def send_overview(context, request): recipients = tmp_recipients.splitlines() budget_template = request.POST.get("template") budget_file = request.budgets_dir / budget_template - expenses = {} - for key, value in request.POST.items(): - if key.startswith("expense-"): - row_str = key.split("-")[-1] - row = int(row_str) - try: - value = float(value) - except ValueError: - value = 0 - expenses[row] = value + + budgets = _map_from_form_data(request, "budget-") + expenses = _map_from_form_data(request, "expense-") + available = {k: (v - expenses[k]) for k, v in budgets.items()} # sanity check if ( not export_date or not recipients or not expenses + or not available or not budget_file.is_file() ): request.session.flash( @@ -140,6 +151,9 @@ def send_overview(context, request): for row, value in expenses.items(): cell = f"F{row}" sheet[cell] = value + for row, value in available.items(): + cell = f"G{row}" + sheet[cell] = value budget_year = budget_file.stem.split("-")[-1] message = Message( diff --git a/superx_budget/pyramid/templates/overview.jinja2 b/superx_budget/pyramid/templates/overview.jinja2 index b6fef14..a3dc499 100644 --- a/superx_budget/pyramid/templates/overview.jinja2 +++ b/superx_budget/pyramid/templates/overview.jinja2 @@ -31,7 +31,10 @@ {{budget.budget_data.project_name}} {{budget.budget_data.project}} - {{ "{:,.2f}".format(budget.budget_data.budget) }} € + + + {{ "{:,.2f}".format(budget.budget_data.budget) }} € + {% if budget.found %}