2023-12-31 12:59:13 +00:00
|
|
|
from flask import Flask, render_template, send_from_directory
|
|
|
|
from jinja2 import TemplateNotFound
|
|
|
|
|
|
|
|
import json
|
|
|
|
import pathlib
|
2024-05-27 15:37:30 +00:00
|
|
|
import os
|
2024-05-29 13:04:57 +00:00
|
|
|
import datetime
|
2023-12-31 12:59:13 +00:00
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
2024-05-29 13:04:57 +00:00
|
|
|
from helpers.finances import (
|
|
|
|
generate_transparency_table,
|
|
|
|
get_transparency_data,
|
|
|
|
get_latest_month,
|
|
|
|
)
|
2024-05-29 12:50:52 +00:00
|
|
|
|
2023-12-31 12:59:13 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
|
|
|
|
@app.route("/assets/<path:path>")
|
2023-12-31 12:59:13 +00:00
|
|
|
def send_assets(path):
|
2024-04-17 08:45:28 +00:00
|
|
|
return send_from_directory("assets", path)
|
|
|
|
|
2023-12-31 12:59:13 +00:00
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
@app.route("/", defaults={"path": "index"})
|
|
|
|
@app.route("/<path:path>.html")
|
2023-12-31 12:59:13 +00:00
|
|
|
def catch_all(path):
|
|
|
|
try:
|
2024-05-29 13:04:57 +00:00
|
|
|
kwargs = {}
|
2024-05-27 15:37:30 +00:00
|
|
|
|
|
|
|
if app.development_mode:
|
2024-05-29 13:04:57 +00:00
|
|
|
kwargs.update(
|
|
|
|
{
|
|
|
|
"warning": render_template("prod-warning.html"),
|
|
|
|
}
|
|
|
|
)
|
2024-05-27 15:37:30 +00:00
|
|
|
|
2024-05-29 13:04:57 +00:00
|
|
|
if path in (
|
|
|
|
"index",
|
|
|
|
"simple",
|
|
|
|
):
|
|
|
|
services = json.loads(
|
|
|
|
(pathlib.Path(__file__).parent / "data" / "services.json").read_text()
|
|
|
|
)
|
|
|
|
|
|
|
|
kwargs.update(
|
|
|
|
{
|
|
|
|
"services": services,
|
|
|
|
}
|
|
|
|
)
|
2024-05-29 12:50:52 +00:00
|
|
|
|
|
|
|
if path == "membership":
|
|
|
|
finances = json.loads(
|
2024-05-29 13:04:57 +00:00
|
|
|
(pathlib.Path(__file__).parent / "data" / "finances.json").read_text()
|
2024-05-29 12:50:52 +00:00
|
|
|
)
|
|
|
|
|
2024-05-29 13:04:57 +00:00
|
|
|
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")
|
|
|
|
|
2024-05-29 12:50:52 +00:00
|
|
|
finances_table = generate_transparency_table(
|
|
|
|
get_transparency_data(finances)
|
|
|
|
)
|
|
|
|
|
|
|
|
kwargs.update(
|
|
|
|
{
|
|
|
|
"finances": finances_table,
|
2024-05-29 13:04:57 +00:00
|
|
|
"finances_period": finances_period_str,
|
2024-05-29 12:50:52 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-06-03 14:12:21 +00:00
|
|
|
if path == "transparency":
|
|
|
|
finances = json.loads(
|
|
|
|
(pathlib.Path(__file__).parent / "data" / "finances.json").read_text()
|
|
|
|
)
|
|
|
|
|
|
|
|
finance_data = {}
|
|
|
|
|
|
|
|
for year in sorted(finances.keys(), reverse=True):
|
|
|
|
for month in sorted(finances[year].keys(), reverse=True):
|
|
|
|
if year not in finance_data:
|
|
|
|
finance_data[year] = {}
|
|
|
|
print(get_transparency_data(finances, year, month))
|
|
|
|
finance_data[year][month] = generate_transparency_table(
|
|
|
|
get_transparency_data(finances, year, month)
|
|
|
|
)
|
|
|
|
|
|
|
|
kwargs.update(
|
|
|
|
{
|
|
|
|
"finances": finance_data,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-05-29 12:50:52 +00:00
|
|
|
return render_template(f"{path}.html", **kwargs)
|
2024-05-29 13:04:57 +00:00
|
|
|
|
2023-12-31 12:59:13 +00:00
|
|
|
except TemplateNotFound:
|
|
|
|
return "404 Not Found", 404
|
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
|
2024-05-27 15:37:30 +00:00
|
|
|
app.development_mode = False
|
|
|
|
|
|
|
|
if os.environ.get("PRIVATECOFFEE_DEV"):
|
|
|
|
app.development_mode = True
|
|
|
|
|
|
|
|
|
2024-05-29 16:57:32 +00:00
|
|
|
def icon(icon_name):
|
|
|
|
file = send_from_directory("assets", f"dist/icons/{icon_name}.svg")
|
2024-05-29 17:02:19 +00:00
|
|
|
try:
|
|
|
|
file_content = file.response.file.read().decode("utf-8")
|
|
|
|
except AttributeError:
|
|
|
|
file_content = file.response.read().decode("utf-8")
|
2024-05-29 16:57:32 +00:00
|
|
|
return file_content
|
|
|
|
|
2024-05-29 17:02:19 +00:00
|
|
|
|
2024-05-29 16:57:32 +00:00
|
|
|
app.add_template_filter(icon)
|
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = ArgumentParser(description="Run the private.coffee web server.")
|
|
|
|
parser.add_argument("--port", type=int, default=9810)
|
|
|
|
parser.add_argument("--debug", action="store_true")
|
2024-05-27 15:37:30 +00:00
|
|
|
parser.add_argument("--dev", action="store_true")
|
2024-04-17 08:45:28 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-05-27 15:37:30 +00:00
|
|
|
app.development_mode = args.dev or app.development_mode
|
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
app.run(port=args.port, debug=args.debug)
|