From 34f3367a08a76bd781327f326871a829842ab113 Mon Sep 17 00:00:00 2001 From: Angelo Rodriguez Date: Wed, 4 Dec 2024 09:37:01 +0100 Subject: [PATCH] work --- core/azure_auth.py | 13 +- core/forms.py | 5 - core/migrations/0009_berichtsheft_kind.py | 22 + .../0010_berichtsheft_department.py | 18 + core/models.py | 9 + core/report_templates.py | 29 -- core/reports.py | 26 ++ core/styles.py | 2 +- core/templates/index.html | 63 +-- core/templates/report.html | 2 +- core/templates/test.html | 384 ------------------ core/templates/write.html | 56 +-- core/views.py | 65 +-- 13 files changed, 175 insertions(+), 519 deletions(-) delete mode 100755 core/forms.py create mode 100755 core/migrations/0009_berichtsheft_kind.py create mode 100755 core/migrations/0010_berichtsheft_department.py delete mode 100755 core/report_templates.py create mode 100755 core/reports.py delete mode 100755 core/templates/test.html diff --git a/core/azure_auth.py b/core/azure_auth.py index 3797d8a..091cc33 100755 --- a/core/azure_auth.py +++ b/core/azure_auth.py @@ -2,7 +2,7 @@ import json import base64 from core.models import Berichtsheft -from core.report_templates import ReportTemplates +from core.reports import DailyReport, WeeklyReport class AzureUser: @@ -35,6 +35,13 @@ class AzureUser: def reports(self): return Berichtsheft.objects.filter(user=self.id) - def get_report_template(self): + def get_report_kind(self): # TODO : Implement - return ReportTemplates.get_template("weekly") + return "weekly" + + def get_report_kind_form(self, request=None): + match self.get_report_kind(): + case "weekly": + return WeeklyReport(request) + case "daily": + return DailyReport(request) diff --git a/core/forms.py b/core/forms.py deleted file mode 100755 index be9c917..0000000 --- a/core/forms.py +++ /dev/null @@ -1,5 +0,0 @@ -def extract_post_values(post, vars) -> dict: - res = {} - for var in vars: - res[var["name"]] = post.get(var["name"], "") - return res diff --git a/core/migrations/0009_berichtsheft_kind.py b/core/migrations/0009_berichtsheft_kind.py new file mode 100755 index 0000000..004495f --- /dev/null +++ b/core/migrations/0009_berichtsheft_kind.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.16 on 2024-12-03 14:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0008_berichtsheft_needs_rewrite"), + ] + + operations = [ + migrations.AddField( + model_name="berichtsheft", + name="kind", + field=models.CharField( + choices=[("weekly", "Weekly"), ("daily", "Daily")], + default="weekly", + max_length=20, + ), + ), + ] diff --git a/core/migrations/0010_berichtsheft_department.py b/core/migrations/0010_berichtsheft_department.py new file mode 100755 index 0000000..d330c89 --- /dev/null +++ b/core/migrations/0010_berichtsheft_department.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.16 on 2024-12-03 15:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0009_berichtsheft_kind"), + ] + + operations = [ + migrations.AddField( + model_name="berichtsheft", + name="department", + field=models.CharField(default="", max_length=160), + ), + ] diff --git a/core/models.py b/core/models.py index b441b63..ff5c798 100755 --- a/core/models.py +++ b/core/models.py @@ -21,12 +21,21 @@ class Group(BuiltinGroup): proxy = True +class ReportKind(models.TextChoices): + WEEKLY = "weekly" + DAILY = "daily" + + class Berichtsheft(models.Model): id = models.AutoField(primary_key=True) user = models.TextField() + kind = models.CharField( + max_length=20, choices=ReportKind.choices, default=ReportKind.WEEKLY + ) num = models.PositiveBigIntegerField(default=0) year = models.PositiveIntegerField() week = models.PositiveSmallIntegerField() + department = models.CharField(max_length=160, default="") content = models.JSONField() needs_rewrite = models.BooleanField(default=False) created = models.DateTimeField(auto_now_add=True) diff --git a/core/report_templates.py b/core/report_templates.py deleted file mode 100755 index a5f2073..0000000 --- a/core/report_templates.py +++ /dev/null @@ -1,29 +0,0 @@ -class ReportTemplates: - report_global = { - "vars": [ - {"name": "name", "display_name": "Name"}, - {"name": "num_doc", "display_name": "Berichtsheft Nummer"}, - {"name": "year", "display_name": "Jahr"}, - {"name": "week", "display_name": "Kalenderwoche"}, - {"name": "start_date", "display_name": "Von"}, - {"name": "end_date", "display_name": "bis"}, - {"name": "department", "display_name": "Abteilung"}, - ] - } - - def get_template(id) -> dict: - match id: - case "weekly": - weekly = { - "vars": [ - { - "name": "company_text", - "display_name": "Betriebliche Tätigkeiten", - }, - {"name": "week_topic", "display_name": "Thema der Woche"}, - {"name": "school_text", "display_name": "Berufsschule"}, - ] - } - - weekly["vars"].extend(ReportTemplates.report_global["vars"]) - return weekly diff --git a/core/reports.py b/core/reports.py new file mode 100755 index 0000000..3fcd9ff --- /dev/null +++ b/core/reports.py @@ -0,0 +1,26 @@ +from django import forms + + +class WeeklyReport(forms.Form): + department = forms.CharField(label="Abteilung", max_length=150) + company_text = forms.CharField(label="Betriebliche Tätigkeiten", max_length=300) + week_topic = forms.CharField(label="Thema der Woche", max_length=600) + school_text = forms.CharField(label="Berufsschule", max_length=300) + + def content_values(self) -> dict: + if self.is_valid(): + return { + "company_text": self.cleaned_data["company_text"], + "week_topic": self.cleaned_data["week_topic"], + "school_text": self.cleaned_data["school_text"], + } + + +class DailyReport: + department = forms.CharField(label="Abteilung", max_length=150) + week_topic = forms.CharField(label="Thema der Woche", max_length=600) + monday_text = forms.CharField(label="Berufsschule", max_length=300) + tuesday_text = forms.CharField(label="Dienstag", max_length=300) + wednesday_text = forms.CharField(label="Mittwoch", max_length=300) + thursday_text = forms.CharField(label="Donnerstag", max_length=300) + friday_text = forms.CharField(label="Freitag", max_length=300) diff --git a/core/styles.py b/core/styles.py index 44d1780..a996d57 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": "a4-page bg-white shadow-lg p-6 mx-auto my-10", + "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", } diff --git a/core/templates/index.html b/core/templates/index.html index fdf059f..e3d44e5 100755 --- a/core/templates/index.html +++ b/core/templates/index.html @@ -1,33 +1,44 @@ - - Azube - - - - {{ user.display_name }} + + Azube + + + + {{ user.display_name }} -

Deine Berichtshefte

+
+

Deine Berichtshefte

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

wau, such empty!

+ + + {% else %} + +
{% for report in reports %} - - {% if forloop.last %} - {% if report.week != week_now %} + + {% if approved %} +
+ ✓ +
+ {% endif %} {{ report }} +
+ + {% if forloop.last %} {% if report.week != week_now %} + + {% endif %} {% endif %} {% endfor %} {% endif %} +
- - {% endif %} - {% endif %} - - {% endfor %} -
- - \ No newline at end of file + + diff --git a/core/templates/report.html b/core/templates/report.html index d8e9ecc..f9feea1 100755 --- a/core/templates/report.html +++ b/core/templates/report.html @@ -15,4 +15,4 @@ {% endfor %} - \ No newline at end of file + diff --git a/core/templates/test.html b/core/templates/test.html deleted file mode 100755 index 31ea0fc..0000000 --- a/core/templates/test.html +++ /dev/null @@ -1,384 +0,0 @@ - - - Test - - - - - - - -

Deine Berichtshefte

- - - - - - \ No newline at end of file diff --git a/core/templates/write.html b/core/templates/write.html index 7d3ccbe..22bcc7c 100755 --- a/core/templates/write.html +++ b/core/templates/write.html @@ -1,50 +1,20 @@ - - Neues Berichtsheft - - -

Neues Berichtsheft

+ + Neues Berichtsheft + + +

Neues Berichtsheft

-

Berichtsheft {{ year }} / {{ week }}

+

{{ user.display_name }}

+

Berichtsheft {{ year }} / {{ week }}

+

Von: {{ start_date|date:"d.m.Y" }} bis: {{ end_date|date:"d.m.Y" }}

-
- {% for var in definition.vars %} -
- - + {{ form }} - {% if var.name == "year" %} - value = "{{ year }}" - {% endif %} + - {% if var.name == "start_date" %} - value = "{{ start_date|date:"d.m.Y" }}" - {% endif %} - - {% if var.name == "end_date" %} - value = "{{ end_date|date:"d.m.Y" }}" - {% endif %} - - {% if var.name == "num_doc" %} - value = "{{ current_num }}" - {% endif %} - - {% if var.name == "name" %} - value = "{{ user.display_name }}" - {% endif %} - - > -
- {% endfor %} - - - - {% csrf_token %} + {% csrf_token %}
- - + diff --git a/core/views.py b/core/views.py index 3df9ed1..d2e0ab7 100755 --- a/core/views.py +++ b/core/views.py @@ -1,8 +1,6 @@ from django.http import HttpResponse from django.shortcuts import get_object_or_404, redirect, render -from core.forms import extract_post_values -from core.report_templates import ReportTemplates from core.util import get_week_range, next_date from .azure_auth import AzureUser from .models import Berichtsheft @@ -22,38 +20,30 @@ def write_new_report(request): def write_new_report_post(request): user = AzureUser(request) - definition = user.get_report_template() + report_form = user.get_report_kind_form(request.POST) - values = extract_post_values(request.POST, definition["vars"]) + # TODO : Implement values - # TODO : Input Validation + current_year, current_week, _, _, current_num = get_current_report_values( + user.reports().order_by("-year", "-week").first() + ) report = Berichtsheft( user=user.id, - num=int(values.pop("num_doc", "")), - year=int(values.pop("year", "")), - week=int(values.pop("week", "")), - content=values, + kind=user.get_report_kind(), + num=int(current_num), + year=int(current_year), + week=int(current_week), + department=report_form.cleaned_data["department"], + content=report_form.content_values(), ) report.save() return redirect("/") -def write_new_report_get(request): - user = AzureUser(request) - - definition = user.get_report_template() - - # Get the latest year and week - latest = user.reports().order_by("-year", "-week").first() - +def get_current_report_values(latest): year_now, week_now, _ = datetime.datetime.today().isocalendar() - # Report for this week already exists - if latest is not None: - if latest.year == year_now and latest.week == week_now: - return redirect(f"/report/{latest.id}") - if latest is not None: current_year, current_week = next_date(latest.year, latest.week) else: @@ -66,8 +56,32 @@ def write_new_report_get(request): else: current_num = 1 + return current_year, current_week, start_date, end_date, current_num + + +def write_new_report_get(request): + user = AzureUser(request) + + report_kind = user.get_report_kind() + + # Get the latest year and week + latest = user.reports().order_by("-year", "-week").first() + + year_now, week_now, _ = datetime.datetime.today().isocalendar() + + # Report for this week already exists + if latest is not None: + if latest.year == year_now and latest.week == week_now: + return redirect(f"/report/{latest.id}") + + current_year, current_week, start_date, end_date, current_num = ( + get_current_report_values(latest) + ) + # TODO : Cookies for persistent saves + form = user.get_report_kind_form() + return render( request, "write.html", @@ -78,7 +92,8 @@ def write_new_report_get(request): "start_date": start_date, "end_date": end_date, "current_num": current_num, - "definition": definition, + "report_kind": report_kind, + "form": form, }, ) @@ -109,7 +124,3 @@ def report_detail_page(request, report_id): return render( request, "report.html", {"report": report, "approved": report.is_approved()} ) - - -def test(request): - return render(request, "test.html", {})