From d248c99242ad142839adc84fc180efb590ecc89d Mon Sep 17 00:00:00 2001 From: Angelo Rodriguez Date: Wed, 4 Dec 2024 11:41:57 +0100 Subject: [PATCH] work+htmx --- core/azure_auth.py | 20 +++++++++++- core/models.py | 1 + core/styles.py | 2 +- core/templates/component/report.html | 9 ++++++ core/templates/head.html | 2 ++ core/templates/htmx/reports.html | 12 +++++++ core/templates/index.html | 47 ++++++++++++++++------------ core/templates/report.html | 3 +- core/templates/write.html | 1 + core/urls.py | 1 + core/views.py | 42 +++++++++++++++++++++---- 11 files changed, 111 insertions(+), 29 deletions(-) create mode 100755 core/templates/component/report.html create mode 100755 core/templates/head.html create mode 100755 core/templates/htmx/reports.html diff --git a/core/azure_auth.py b/core/azure_auth.py index 091cc33..8121de2 100755 --- a/core/azure_auth.py +++ b/core/azure_auth.py @@ -1,8 +1,10 @@ +import datetime import json import base64 from core.models import Berichtsheft from core.reports import DailyReport, WeeklyReport +import core.util class AzureUser: @@ -33,7 +35,7 @@ class AzureUser: self.id = "anon" def reports(self): - return Berichtsheft.objects.filter(user=self.id) + return Berichtsheft.objects.filter(user=self.id).order_by("-year", "-week") def get_report_kind(self): # TODO : Implement @@ -45,3 +47,19 @@ class AzureUser: return WeeklyReport(request) case "daily": return DailyReport(request) + + def latest_report(self): + return self.reports().order_by("-year", "-week").first() + + def late_reports(self) -> int: + year_now, week_now, _ = datetime.datetime.today().isocalendar() + count = 0 + latest = self.latest_report() + + new_year, new_week = (latest.year, latest.week) + + while week_now != new_week or (week_now == new_week and year_now != new_year): + count += 1 + new_year, new_week = core.util.next_date(new_year, new_week) + + return count diff --git a/core/models.py b/core/models.py index ff5c798..08bd28a 100755 --- a/core/models.py +++ b/core/models.py @@ -43,6 +43,7 @@ class Berichtsheft(models.Model): def __str__(self): return f"Berichtsheft: {self.user}, Year: {self.year}, Week: {self.week}" + @property def is_approved(self): approvals = Approval.objects.filter(report=self.id) return len(approvals) >= 2 diff --git a/core/styles.py b/core/styles.py index a996d57..c0a672b 100755 --- a/core/styles.py +++ b/core/styles.py @@ -2,5 +2,5 @@ STYLE = { "red_btn": "text-white bg-red-700 hover:bg-red-800 focus:outline-none focus:ring-4 focus:ring-red-300 font-medium rounded-full text-sm px-5 py-2.5 text-center me-2 mb-2 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-900", - "card": "bg-white drop-shadow-md p-6 mx-auto my-10 aspect-[2/3] hover:drop-shadow-xl hover:scale-[0.95] scale-[0.9] transition-all duration-50 transform ease-in-out", + "card": "bg-white drop-shadow-md p-6 mx-auto my-10 aspect-[2/3] hover:drop-shadow-xl hover:scale-[0.95] scale-[0.9] transition-all duration-50 transform ease-in-out w-80", } diff --git a/core/templates/component/report.html b/core/templates/component/report.html new file mode 100755 index 0000000..e79c3c1 --- /dev/null +++ b/core/templates/component/report.html @@ -0,0 +1,9 @@ + + {% if report.is_approved %} +
+ ✓ +
+ {% endif %} {{ report }} +
diff --git a/core/templates/head.html b/core/templates/head.html new file mode 100755 index 0000000..078f27d --- /dev/null +++ b/core/templates/head.html @@ -0,0 +1,2 @@ + + diff --git a/core/templates/htmx/reports.html b/core/templates/htmx/reports.html new file mode 100755 index 0000000..dc0fb8f --- /dev/null +++ b/core/templates/htmx/reports.html @@ -0,0 +1,12 @@ +{% if reports|length != 0 %} + {% for report in reports %} + + {% include 'component/report.html' with report=report %} + + {% endfor %} + +
+ + {% endif %} diff --git a/core/templates/index.html b/core/templates/index.html index e3d44e5..2549c47 100755 --- a/core/templates/index.html +++ b/core/templates/index.html @@ -1,43 +1,50 @@ Azube - + {% include 'head.html' %} - {{ user.display_name }} - -
+

Deine Berichtshefte

+ {% if late_reports > 1 %} + + + {% endif %} + {% if reports|length == 0 %} -

wau, such empty!

+

wau, such empty!

- {% else %}
{% for report in reports %} - - {% if approved %} -
- ✓ -
- {% endif %} {{ report }} + {% if forloop.first %} {% if report.week != week_now %} +
+ + + {% endif %} {% endif %} + + {% include 'component/report.html' with report=report %} + + {% endfor %} + +
+ + {% endif %} - {% if forloop.last %} {% if report.week != week_now %} - - {% endif %} {% endif %} {% endfor %} {% endif %}
diff --git a/core/templates/report.html b/core/templates/report.html index f9feea1..5f93e64 100755 --- a/core/templates/report.html +++ b/core/templates/report.html @@ -1,13 +1,14 @@ Berichtsheft {{ report.num }} + {% include 'head.html' %}

Berichtsheft {{ report.num }}

Berichtsheft {{ report.year }} / {{ report.week }}

-

Is Approved ? {{ approved }}

+

Is Approved ? {{ report.is_approved }}

{% for key, value in report.content.items %} diff --git a/core/templates/write.html b/core/templates/write.html index 22bcc7c..98966c2 100755 --- a/core/templates/write.html +++ b/core/templates/write.html @@ -1,6 +1,7 @@ Neues Berichtsheft + {% include 'head.html' %}

Neues Berichtsheft

diff --git a/core/urls.py b/core/urls.py index ffbc220..0253d91 100755 --- a/core/urls.py +++ b/core/urls.py @@ -5,4 +5,5 @@ urlpatterns = [ path("", views.index, name="index"), path("write", views.write_new_report, name="write"), path("report/", views.report_detail_page, name="report_detail"), + path("reports", views.reports_list, name="reports_list"), ] diff --git a/core/views.py b/core/views.py index d2e0ab7..c95bfe2 100755 --- a/core/views.py +++ b/core/views.py @@ -4,6 +4,7 @@ from django.shortcuts import get_object_or_404, redirect, render 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 core.styles import STYLE import datetime @@ -22,6 +23,9 @@ def write_new_report_post(request): report_form = user.get_report_kind_form(request.POST) + if not report_form.is_valid(): + return HttpResponse("Bad Request", status=400) + # TODO : Implement values current_year, current_week, _, _, current_num = get_current_report_values( @@ -65,7 +69,7 @@ def write_new_report_get(request): report_kind = user.get_report_kind() # Get the latest year and week - latest = user.reports().order_by("-year", "-week").first() + latest = user.latest_report() year_now, week_now, _ = datetime.datetime.today().isocalendar() @@ -102,14 +106,42 @@ def index(request): user = AzureUser(request) # Get all berichtshefte - all_reports = user.reports() + reports = Paginator(user.reports(), 30).get_page(1) year_now, week_now, _ = datetime.datetime.today().isocalendar() return render( request, "index.html", - {"user": user, "reports": all_reports, "week_now": week_now, "style": STYLE}, + { + "user": user, + "reports": reports, + "week_now": week_now, + "late_reports": user.late_reports(), + "style": STYLE, + }, + ) + + +def reports_list(request): + user = AzureUser(request) + + p = Paginator(user.reports(), 30) + + try: + page_num = int(request.GET.get("page")) + except: + return HttpResponse("Page should be a number", 400) + + if p.num_pages >= page_num: + objs = p.get_page(page_num) + else: + objs = [] + + return render( + request, + "htmx/reports.html", + {"reports": objs, "next": page_num + 1, "style": STYLE}, ) @@ -121,6 +153,4 @@ def report_detail_page(request, report_id): if report.user != user.id: return HttpResponse("Nah", status=401) - return render( - request, "report.html", {"report": report, "approved": report.is_approved()} - ) + return render(request, "report.html", {"report": report})