From 2a174aa3425ef9bfed4a9e36d50f51e7eb58fdd3 Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 29 May 2024 15:04:57 +0200 Subject: [PATCH] feat: Enhance data structure and transparency features - Moved `finances.json` and `services.json` to a new `data` directory for better organization. - Introduced `get_latest_month` function in `helpers/finances.py` to dynamically determine the latest financial reporting period. - Updated `main.py` to use the new data paths and implement dynamic generation of the transparency report date, improving the clarity of financial reports. - Modified `membership.html` template to display financial report for the dynamically determined latest month, enhancing transparency for donations. These changes aim to improve data management, make financial reporting more transparent, and ensure users have access to the most current financial information. Moving JSON files to a dedicated directory helps with organizing resources better. By dynamically determining the latest financial period, the application now avoids hard-coded dates, increasing accuracy and reducing maintenance effort. --- finances.json => data/finances.json | 0 services.json => data/services.json | 0 helpers/finances.py | 8 ++++++ main.py | 44 +++++++++++++++++++++-------- templates/membership.html | 9 +++--- 5 files changed, 44 insertions(+), 17 deletions(-) rename finances.json => data/finances.json (100%) rename services.json => data/services.json (100%) diff --git a/finances.json b/data/finances.json similarity index 100% rename from finances.json rename to data/finances.json diff --git a/services.json b/data/services.json similarity index 100% rename from services.json rename to data/services.json diff --git a/helpers/finances.py b/helpers/finances.py index 32b5647..e4a0b32 100644 --- a/helpers/finances.py +++ b/helpers/finances.py @@ -1,6 +1,14 @@ from decimal import Decimal +def get_latest_month(data): + years = sorted(data.keys()) + latest_year = years[-1] + months = sorted(data[latest_year].keys()) + latest_month = months[-1] + + return int(latest_month), int(latest_year) + def get_transparency_data(data, year=None, month=None): if year is None: year = max(data.keys()) diff --git a/main.py b/main.py index f871dfe..dc49b56 100644 --- a/main.py +++ b/main.py @@ -4,10 +4,15 @@ from jinja2 import TemplateNotFound import json import pathlib import os +import datetime from argparse import ArgumentParser -from helpers.finances import generate_transparency_table, get_transparency_data +from helpers.finances import ( + generate_transparency_table, + get_transparency_data, + get_latest_month, +) app = Flask(__name__) @@ -21,25 +26,38 @@ def send_assets(path): @app.route("/.html") def catch_all(path): try: - services = json.loads( - (pathlib.Path(__file__).parent / "services.json").read_text() - ) - - warning = None + kwargs = {} if app.development_mode: - warning = render_template("prod-warning.html") + kwargs.update( + { + "warning": render_template("prod-warning.html"), + } + ) - kwargs = { - "services": services, - "warning": warning, - } + if path in ( + "index", + "simple", + ): + services = json.loads( + (pathlib.Path(__file__).parent / "data" / "services.json").read_text() + ) + + kwargs.update( + { + "services": services, + } + ) if path == "membership": finances = json.loads( - (pathlib.Path(__file__).parent / "finances.json").read_text() + (pathlib.Path(__file__).parent / "data" / "finances.json").read_text() ) + finances_month, finances_year = get_latest_month(finances) + finances_period = datetime.date(finances_year, finances_month, 1) + finances_period_str = finances_period.strftime("%B %Y") + finances_table = generate_transparency_table( get_transparency_data(finances) ) @@ -47,10 +65,12 @@ def catch_all(path): kwargs.update( { "finances": finances_table, + "finances_period": finances_period_str, } ) return render_template(f"{path}.html", **kwargs) + except TemplateNotFound: return "404 Not Found", 404 diff --git a/templates/membership.html b/templates/membership.html index 9e25a14..bd4f88b 100644 --- a/templates/membership.html +++ b/templates/membership.html @@ -78,14 +78,13 @@
-
Transparency Report for May 2024
+
Transparency Report for {{ finances_period }}

We believe in transparency and accountability. Below is a summary of our - income and expenses for the last month. + income and expenses for the last month, so you can see how your + donations are being used.

-
- {{ finances|safe }} -
+
{{ finances|safe }}