From 8d3013bef9d8ef0eaf46153629daf51fba904a68 Mon Sep 17 00:00:00 2001 From: Emilia Marczyk Date: Mon, 11 Aug 2025 19:15:18 +0200 Subject: [PATCH 1/2] * Throw 404 when accessing not registered organisation * Add custom 404 template * Fix .gitignore - do not ignore files from site/ directory --- .gitignore | 3 --- site/server.py | 14 ++++++++++++-- site/templates/404.html | 7 +++++++ 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 site/templates/404.html diff --git a/.gitignore b/.gitignore index a8b7697..acf7bbf 100644 --- a/.gitignore +++ b/.gitignore @@ -169,9 +169,6 @@ venv.bak/ # Rope project settings .ropeproject -# mkdocs documentation -/site - # mypy .mypy_cache/ .dmypy.json diff --git a/site/server.py b/site/server.py index fa2aceb..d1c034e 100644 --- a/site/server.py +++ b/site/server.py @@ -2,7 +2,7 @@ import sys import yaml -from flask import Flask, render_template, send_from_directory, url_for +from flask import Flask, abort, render_template, send_from_directory, url_for from flask_frozen import Freezer # Added from config import ORGANIZATIONS_DIR_PATH, ORGANIZATIONS_SLUG_FIELD_NAME @@ -26,6 +26,7 @@ def get_organizations() -> dict[str, str]: organizations[organization.get(ORGANIZATIONS_SLUG_FIELD_NAME)] = ( organization_file ) + print(organizations) return organizations @@ -46,7 +47,9 @@ def get_static_files_list(): for filename in get_static_files_list(): - app.route(f"/{filename}", strict_slashes=False, endpoint=filename)(lambda f=filename: static_file(f)) + app.route(f"/{filename}", strict_slashes=False, endpoint=filename)( + lambda f=filename: static_file(f) + ) @freezer.register_generator @@ -57,6 +60,11 @@ def generate_favicon_statics(): yield url_for(static) +@app.errorhandler(404) +def page_not_found(e): + return render_template("404.html"), 404 + + # Generated css @app.route("/output.css") def outputCss(): @@ -74,6 +82,8 @@ def index(): @app.route("//", strict_slashes=False) def organization_page(org_name): filename = organizations.get(org_name) + if not filename: + abort(404) with open(f"{ORGANIZATIONS_DIR_PATH}/{filename}") as org: data = yaml.safe_load(org) return render_template("organization.html", data=data) diff --git a/site/templates/404.html b/site/templates/404.html new file mode 100644 index 0000000..93465b1 --- /dev/null +++ b/site/templates/404.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} {% block content %} + +
+

Oops, page not found

+
+ +{% endblock content %} From 86f8b7f057037b7304a11e68fb34c88bcb23705f Mon Sep 17 00:00:00 2001 From: Emilia Marczyk Date: Mon, 11 Aug 2025 19:17:35 +0200 Subject: [PATCH 2/2] Cleanup --- site/server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/site/server.py b/site/server.py index d1c034e..08be52e 100644 --- a/site/server.py +++ b/site/server.py @@ -26,7 +26,6 @@ def get_organizations() -> dict[str, str]: organizations[organization.get(ORGANIZATIONS_SLUG_FIELD_NAME)] = ( organization_file ) - print(organizations) return organizations