From 73e019de9a7e623b8f3cb19f89a3d598873a2dd4 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 5 Dec 2024 17:03:42 +0100 Subject: [PATCH] htmx single page application + work --- core/templates/component/report.html | 7 ++- core/templates/header.html | 5 --- core/templates/index.html | 19 +++----- core/templates/report.html | 22 +++------ core/templates/shell.html | 67 ++++++++++++++++++++++++++++ core/templates/write.html | 26 ++++------- core/templatetags/markdown.py | 10 +++++ core/util.py | 36 +++++++++++++++ core/views.py | 12 +++-- requirements.txt | 4 +- 10 files changed, 150 insertions(+), 58 deletions(-) delete mode 100644 core/templates/header.html create mode 100644 core/templates/shell.html create mode 100644 core/templatetags/markdown.py diff --git a/core/templates/component/report.html b/core/templates/component/report.html index e79c3c1..05d8bd4 100644 --- a/core/templates/component/report.html +++ b/core/templates/component/report.html @@ -1,4 +1,7 @@ - + diff --git a/core/templates/header.html b/core/templates/header.html deleted file mode 100644 index 567861a..0000000 --- a/core/templates/header.html +++ /dev/null @@ -1,5 +0,0 @@ -
-
-

{{ title }}

-
-
diff --git a/core/templates/index.html b/core/templates/index.html index 888542e..5e0bfc9 100644 --- a/core/templates/index.html +++ b/core/templates/index.html @@ -1,13 +1,4 @@ - - - Azube - {% include 'head.html' %} - - - - {% include 'header.html' with title="Deine Berichtshefte" center=False %} - -
+ {{ title|safe }} {% if late_reports > 1 %} @@ -33,9 +24,11 @@ {% for report in reports %} {% if forloop.first %} {% if report.week != week_now %} - + {% endif %} {% endif %} {% include 'component/report.html' with report=report %} @@ -50,5 +43,3 @@
- - diff --git a/core/templates/report.html b/core/templates/report.html index 04db2cb..de52a08 100644 --- a/core/templates/report.html +++ b/core/templates/report.html @@ -1,14 +1,5 @@ - - - Berichtsheft {{ report.num }} - {% include 'head.html' %} - - - - {% with title="Berichtsheft "|add:report.num %} - {% include 'header.html' with title="Berichtsheft" %} -{% endwith %} - +{{ title|safe }} +

@@ -25,14 +16,13 @@

Content:

+ {% load markdown %} {% for key, value in report.content.items %} -
-
{{ key }}
-
{{ value }}
+
+

{{ key }}

+ {{ value|markdown|safe }}
{% endfor %}
- - diff --git a/core/templates/shell.html b/core/templates/shell.html new file mode 100644 index 0000000..0c35906 --- /dev/null +++ b/core/templates/shell.html @@ -0,0 +1,67 @@ + + + {{ title }} + {% include 'head.html' %} + + + + +
+
+ + +

{{ title }}

+
+ + +
+ +
+
    +
  • + +
  • +
+
+ + +
+ {{ main_content|safe }} +
+
+
+ + + + + + diff --git a/core/templates/write.html b/core/templates/write.html index 3bff28f..62e1b84 100644 --- a/core/templates/write.html +++ b/core/templates/write.html @@ -1,13 +1,7 @@ - - - Neues Berichtsheft - {% include 'head.html' %} - - - - {% include 'header.html' with title="Neues Berichtsheft" %} -
+ + {{ title|safe }} +

{{ user.display_name }}

@@ -20,8 +14,7 @@

- {% load set_content %} - {% load access %} + {% load access set_content %} {% for field in form %}
@@ -31,13 +24,13 @@ {% if field.id_for_label == "id_department" %} - {{ field|set_content:draft.department}} + {{ field|set_content:draft.department }} - {%else%} + {% else %} {% with content=draft.content|access:field.id_for_label %} {{ field|set_content:content }} {% endwith %} -{%endif%} +{% endif %} {% if field.errors %}

{{ field.errors|join:", " }}

{% endif %} @@ -45,6 +38,7 @@ {% endfor %} {% csrf_token %} +
- - diff --git a/core/templatetags/markdown.py b/core/templatetags/markdown.py new file mode 100644 index 0000000..c6bea54 --- /dev/null +++ b/core/templatetags/markdown.py @@ -0,0 +1,10 @@ +from django import template +import markdown +import bleach + +register = template.Library() + + +@register.filter(name="markdown") +def markdown_tag(value): + return markdown.markdown(bleach.clean(value, tags=[], attributes=[])) diff --git a/core/util.py b/core/util.py index c93ebe8..b383732 100644 --- a/core/util.py +++ b/core/util.py @@ -1,5 +1,9 @@ import datetime +from django.template.loader import render_to_string +from django.shortcuts import render +from django.http import HttpResponse + def next_date(year: int, week: int) -> (int, int): if week >= 52: @@ -30,3 +34,35 @@ def get_week_range(p_year, p_week): ).date() lastdayofweek = firstdayofweek + datetime.timedelta(days=6.9) return firstdayofweek, lastdayofweek + + +def is_htmx_request(request) -> bool: + return request.headers.get("HX-Request") is not None + + +def title(t): + return f""" + + """ + + +def htmx_request(request, content_template, vars, page_title): + vars["title"] = title(page_title) + vars["htmx"] = is_htmx_request(request) + + main_content = render_to_string(content_template, vars, request=request) + + if is_htmx_request(request): + return HttpResponse(main_content, content_type="text/html") + else: + return render( + request, + "shell.html", + { + "title": page_title, + "main_content": main_content, + }, + ) diff --git a/core/views.py b/core/views.py index 980b5fc..e7b122c 100644 --- a/core/views.py +++ b/core/views.py @@ -5,7 +5,9 @@ from core.util import get_week_range, next_date from .azure_auth import AzureUser from .models import Berichtsheft from django.core.paginator import Paginator +from django.template.loader import render_to_string from core.styles import STYLE +from .util import is_htmx_request, title, htmx_request import datetime import json @@ -91,7 +93,7 @@ def write_new_report_get(request): form = user.get_report_kind_form() - return render( + return htmx_request( request, "write.html", { @@ -105,6 +107,7 @@ def write_new_report_get(request): "form": form, "draft": draft, }, + "Neues Berichtsheft", ) @@ -116,7 +119,7 @@ def index(request): year_now, week_now, _ = datetime.datetime.today().isocalendar() - return render( + return htmx_request( request, "index.html", { @@ -126,6 +129,7 @@ def index(request): "late_reports": user.late_reports(), "style": STYLE, }, + "Berichtshefte", ) @@ -159,7 +163,9 @@ def report_detail_page(request, report_id): if report.user != user.id: return HttpResponse("Nah", status=401) - return render(request, "report.html", {"report": report}) + return htmx_request( + request, "report.html", {"report": report}, f"Berichtsheft {report.num}" + ) from django.shortcuts import get_object_or_404 diff --git a/requirements.txt b/requirements.txt index 7a4844b..f19d158 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ Django~=4.2.11 -psycopg2 \ No newline at end of file +psycopg2 +markdown +bleach \ No newline at end of file