From f17051224d83fb055c5217af5a56f55abbb65bf6 Mon Sep 17 00:00:00 2001 From: Yaro Kasear Date: Mon, 21 Jul 2025 13:43:01 -0500 Subject: [PATCH] Enable debug mode in config, enhance HTML response handling with minification, and clean up worklog template by removing commented button --- inventory/config.py | 2 +- inventory/routes/hooks.py | 26 +++++++++++++++++++------- inventory/templates/inventory.html | 30 ++++++++++++++---------------- inventory/templates/worklog.html | 5 ----- pyproject.toml | 1 + 5 files changed, 35 insertions(+), 29 deletions(-) diff --git a/inventory/config.py b/inventory/config.py index 0372d87..f0f7608 100644 --- a/inventory/config.py +++ b/inventory/config.py @@ -9,7 +9,7 @@ def quote(value: str) -> str: class Config: SQLALCHEMY_TRACK_MODIFICATIONS = False - DEBUG = False + DEBUG = True TESTING = False DB_BACKEND = os.getenv('DB_BACKEND', 'sqlite').lower() diff --git a/inventory/routes/hooks.py b/inventory/routes/hooks.py index 7186220..298a709 100644 --- a/inventory/routes/hooks.py +++ b/inventory/routes/hooks.py @@ -1,18 +1,30 @@ from bs4 import BeautifulSoup from flask import current_app as app +import re from . import main @main.after_request -def prettify_html_response(response): - if app.debug and response.content_type.startswith("text/html"): +def prettify_or_minify_html_response(response): + print(app.debug, response.content_type) + if response.content_type.startswith("text/html"): try: - soup = BeautifulSoup(response.get_data(as_text=True), 'html5lib') - pretty_html = soup.prettify() + html = response.get_data(as_text=True) + soup = BeautifulSoup(html, 'html5lib') + + if app.debug: + pretty_html = soup.prettify() + response.set_data(pretty_html.encode("utf-8")) # type: ignore + else: + # Minify by stripping extra whitespace between tags and inside text + minified_html = re.sub(r">\s+<", "><", str(soup)) # collapse whitespace between tags + minified_html = re.sub(r"\s{2,}", " ", minified_html) # collapse multi-spaces to one + minified_html = re.sub(r"\n+", "", minified_html) # remove newlines + + response.set_data(minified_html.encode("utf-8")) # type: ignore - response.set_data(pretty_html.encode("utf-8")) # type: ignore response.headers['Content-Type'] = 'text/html; charset=utf-8' - except Exception as e: - print(f"⚠️ Prettifying failed: {e}") + print(f"⚠️ Prettifying/Minifying failed: {e}") + return response diff --git a/inventory/templates/inventory.html b/inventory/templates/inventory.html index 2011f8d..8126665 100644 --- a/inventory/templates/inventory.html +++ b/inventory/templates/inventory.html @@ -263,37 +263,35 @@ ) }} {% if worklog %} -
+
Work Log Entries {% set id_list = worklog_rows | map(attribute='id') | list %} {{ links.export_link( id = (item.identifier | replace('Name: ', '') - | replace('ID:', '') - | replace('Serial: ', '') - | replace('Barcode: ', '') - | lower) + '_worklog', + | replace('ID:', '') + | replace('Serial: ', '') + | replace('Barcode: ', '') + | lower) + '_worklog', endpoint = 'worklog', ids = {'ids': id_list} ) }}
-
-
+
+
{% for note in notes %} {% set title %} {{ note.timestamp.strftime('%Y-%m-%d %H:%M:%S') }}{{ links.entry_link('worklog_entry', note.work_log_id) }} {% endset %} -
- {{ editor.render_editor( - id = 'updates' + (note.id | string), - title = title, - content = note.content, - mode = 'view', - enabled = false - ) }} -
+ {{ editor.render_editor( + id = 'updates' + (note.id | string), + title = title, + content = note.content, + mode = 'view', + enabled = false + ) }} {% endfor %}
{% endif %} diff --git a/inventory/templates/worklog.html b/inventory/templates/worklog.html index 6eafdcb..03706f3 100644 --- a/inventory/templates/worklog.html +++ b/inventory/templates/worklog.html @@ -219,11 +219,6 @@
- {# - - #} {% set addUpdateLogic %} function formatDate(date) { const pad = (n) => String(n).padStart(2, '0'); diff --git a/pyproject.toml b/pyproject.toml index 3455bb3..6fed6e1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ dependencies = [ "pandas", "pyodbc", "python-dotenv", + "waitress", "Werkzeug" ]