diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/azube/__init__.py b/azube/__init__.py old mode 100755 new mode 100644 diff --git a/azube/asgi.py b/azube/asgi.py old mode 100755 new mode 100644 diff --git a/azube/settings.py b/azube/settings.py old mode 100755 new mode 100644 diff --git a/azube/urls.py b/azube/urls.py old mode 100755 new mode 100644 diff --git a/azube/wsgi.py b/azube/wsgi.py old mode 100755 new mode 100644 diff --git a/core/__init__.py b/core/__init__.py old mode 100755 new mode 100644 diff --git a/core/admin.py b/core/admin.py old mode 100755 new mode 100644 diff --git a/core/apps.py b/core/apps.py old mode 100755 new mode 100644 diff --git a/core/azure_auth.py b/core/azure_auth.py old mode 100755 new mode 100644 diff --git a/core/doc_gen.py b/core/doc_gen.py old mode 100755 new mode 100644 diff --git a/core/migrations/0001_initial.py b/core/migrations/0001_initial.py old mode 100755 new mode 100644 index eaacbb1..7be4550 --- a/core/migrations/0001_initial.py +++ b/core/migrations/0001_initial.py @@ -6,7 +6,6 @@ import django.db.models.deletion class Migration(migrations.Migration): - initial = True dependencies = [ diff --git a/core/migrations/0002_alter_berichtsheft_id.py b/core/migrations/0002_alter_berichtsheft_id.py old mode 100755 new mode 100644 index 21edbf1..2ccae28 --- a/core/migrations/0002_alter_berichtsheft_id.py +++ b/core/migrations/0002_alter_berichtsheft_id.py @@ -4,7 +4,6 @@ from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [ ("core", "0001_initial"), ] diff --git a/core/migrations/0003_group_azureuser_alter_berichtsheft_user.py b/core/migrations/0003_group_azureuser_alter_berichtsheft_user.py old mode 100755 new mode 100644 index 6050d9c..8cc8a8e --- a/core/migrations/0003_group_azureuser_alter_berichtsheft_user.py +++ b/core/migrations/0003_group_azureuser_alter_berichtsheft_user.py @@ -8,7 +8,6 @@ import django.utils.timezone class Migration(migrations.Migration): - dependencies = [ ("auth", "0012_alter_user_first_name_max_length"), ("core", "0002_alter_berichtsheft_id"), diff --git a/core/migrations/0004_remove_berichtsheft_approved_by.py b/core/migrations/0004_remove_berichtsheft_approved_by.py old mode 100755 new mode 100644 index aa1f29b..78b4b3f --- a/core/migrations/0004_remove_berichtsheft_approved_by.py +++ b/core/migrations/0004_remove_berichtsheft_approved_by.py @@ -4,7 +4,6 @@ from django.db import migrations class Migration(migrations.Migration): - dependencies = [ ("core", "0003_group_azureuser_alter_berichtsheft_user"), ] diff --git a/core/migrations/0005_user_alter_berichtsheft_user_delete_azureuser.py b/core/migrations/0005_user_alter_berichtsheft_user_delete_azureuser.py old mode 100755 new mode 100644 index cf368b3..f52a13b --- a/core/migrations/0005_user_alter_berichtsheft_user_delete_azureuser.py +++ b/core/migrations/0005_user_alter_berichtsheft_user_delete_azureuser.py @@ -7,7 +7,6 @@ import django.utils.timezone class Migration(migrations.Migration): - dependencies = [ ("auth", "0012_alter_user_first_name_max_length"), ("core", "0004_remove_berichtsheft_approved_by"), diff --git a/core/migrations/0006_berichtsheft_num.py b/core/migrations/0006_berichtsheft_num.py old mode 100755 new mode 100644 index 985fc22..4ab5e4d --- a/core/migrations/0006_berichtsheft_num.py +++ b/core/migrations/0006_berichtsheft_num.py @@ -4,7 +4,6 @@ from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [ ("core", "0005_user_alter_berichtsheft_user_delete_azureuser"), ] diff --git a/core/migrations/0007_approval.py b/core/migrations/0007_approval.py old mode 100755 new mode 100644 index 6893d66..1cb35ba --- a/core/migrations/0007_approval.py +++ b/core/migrations/0007_approval.py @@ -5,7 +5,6 @@ import django.db.models.deletion class Migration(migrations.Migration): - dependencies = [ ("core", "0006_berichtsheft_num"), ] diff --git a/core/migrations/0008_berichtsheft_needs_rewrite.py b/core/migrations/0008_berichtsheft_needs_rewrite.py old mode 100755 new mode 100644 index 8c5ea61..a67dc54 --- a/core/migrations/0008_berichtsheft_needs_rewrite.py +++ b/core/migrations/0008_berichtsheft_needs_rewrite.py @@ -4,7 +4,6 @@ from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [ ("core", "0007_approval"), ] diff --git a/core/migrations/0009_berichtsheft_kind.py b/core/migrations/0009_berichtsheft_kind.py old mode 100755 new mode 100644 index 004495f..016887d --- a/core/migrations/0009_berichtsheft_kind.py +++ b/core/migrations/0009_berichtsheft_kind.py @@ -4,7 +4,6 @@ from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [ ("core", "0008_berichtsheft_needs_rewrite"), ] diff --git a/core/migrations/0010_berichtsheft_department.py b/core/migrations/0010_berichtsheft_department.py old mode 100755 new mode 100644 index d330c89..57c931f --- a/core/migrations/0010_berichtsheft_department.py +++ b/core/migrations/0010_berichtsheft_department.py @@ -4,7 +4,6 @@ from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [ ("core", "0009_berichtsheft_kind"), ] diff --git a/core/migrations/0011_berichtsheftdraft.py b/core/migrations/0011_berichtsheftdraft.py new file mode 100644 index 0000000..9f73ecf --- /dev/null +++ b/core/migrations/0011_berichtsheftdraft.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2.17 on 2024-12-05 10:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("core", "0010_berichtsheft_department"), + ] + + operations = [ + migrations.CreateModel( + name="BerichtsheftDraft", + fields=[ + ("user", models.TextField(primary_key=True, serialize=False)), + ("department", models.CharField(default="", max_length=160)), + ("content", models.JSONField()), + ], + ), + ] diff --git a/core/migrations/__init__.py b/core/migrations/__init__.py old mode 100755 new mode 100644 diff --git a/core/models.py b/core/models.py old mode 100755 new mode 100644 index 08bd28a..67b831d --- a/core/models.py +++ b/core/models.py @@ -55,3 +55,9 @@ class Approval(models.Model): report = models.ForeignKey( Berichtsheft, on_delete=models.CASCADE, related_name="report" ) + + +class BerichtsheftDraft(models.Model): + user = models.TextField(primary_key=True) + department = models.CharField(max_length=160, default="") + content = models.JSONField() diff --git a/core/reports.py b/core/reports.py old mode 100755 new mode 100644 index 3dfbffa..4c9328d --- a/core/reports.py +++ b/core/reports.py @@ -20,7 +20,7 @@ class WeeklyReport(forms.Form): widget=forms.Textarea( attrs={ "class": "w-full p-2 border border-gray-300 rounded-lg focus:ring focus:ring-blue-400 mb-5", - "rows": 10, + "rows": 5, "placeholder": "Betriebliche Tätigkeiten", } ), @@ -31,7 +31,7 @@ class WeeklyReport(forms.Form): widget=forms.Textarea( attrs={ "class": "w-full p-2 border border-gray-300 rounded-lg focus:ring focus:ring-blue-400 mb-5", - "rows": 8, + "rows": 10, "placeholder": "Thema der Woche", } ), diff --git a/core/styles.py b/core/styles.py old mode 100755 new mode 100644 diff --git a/core/templates/component/report.html b/core/templates/component/report.html old mode 100755 new mode 100644 diff --git a/core/templates/head.html b/core/templates/head.html old mode 100755 new mode 100644 diff --git a/core/templates/header.html b/core/templates/header.html old mode 100755 new mode 100644 index 1a2dd41..567861a --- a/core/templates/header.html +++ b/core/templates/header.html @@ -1,4 +1,4 @@ -
+

{{ title }}

diff --git a/core/templates/htmx/reports.html b/core/templates/htmx/reports.html old mode 100755 new mode 100644 diff --git a/core/templates/index.html b/core/templates/index.html old mode 100755 new mode 100644 diff --git a/core/templates/report.html b/core/templates/report.html old mode 100755 new mode 100644 diff --git a/core/templates/write.html b/core/templates/write.html old mode 100755 new mode 100644 index 746d24f..3bff28f --- a/core/templates/write.html +++ b/core/templates/write.html @@ -19,13 +19,25 @@
- + + {% load set_content %} + {% load access %} + {% for field in form %}
- {{ field }} + + {% if field.id_for_label == "id_department" %} + + {{ field|set_content:draft.department}} + + {%else%} + {% with content=draft.content|access:field.id_for_label %} + {{ field|set_content:content }} + {% endwith %} +{%endif%} {% if field.errors %}

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

{% endif %} @@ -39,6 +51,81 @@ Submit + + + diff --git a/core/templatetags/access.py b/core/templatetags/access.py new file mode 100644 index 0000000..1cd9369 --- /dev/null +++ b/core/templatetags/access.py @@ -0,0 +1,13 @@ +from django import template + +register = template.Library() + + +@register.filter(name="access") +def access(value, arg): + if value == None: + return "" + try: + return value[arg] + except: + return "" diff --git a/core/templatetags/add_attr.py b/core/templatetags/add_attr.py new file mode 100644 index 0000000..51e77b7 --- /dev/null +++ b/core/templatetags/add_attr.py @@ -0,0 +1,25 @@ +from django import template + +register = template.Library() + + +@register.filter(name="add_attr") +def add_attr(field, css): + """Adds custom attributes (like class) to form fields without overwriting existing attributes.""" + # Retrieve existing attributes (if any) + attrs = field.field.widget.attrs.copy() + + # Split the CSS string into key-value pairs + definition = css.split(",") + + for d in definition: + if ":" not in d: + # If no key-value pair is provided, default to 'class' + attrs["class"] = f"{attrs.get('class', '')} {d}".strip() + else: + # If a key-value pair is provided (e.g., 'data-test:123'), add it + key, val = d.split(":") + attrs[key.strip()] = val.strip() + + # Return the widget with the updated attributes + return field.as_widget(attrs=attrs) diff --git a/core/templatetags/set_content.py b/core/templatetags/set_content.py new file mode 100644 index 0000000..0c5dc22 --- /dev/null +++ b/core/templatetags/set_content.py @@ -0,0 +1,27 @@ +from django import template +from django import forms + +register = template.Library() + + +@register.filter(name="set_content") +def set_content(field, content): + """Sets the content of input and textarea fields, and custom attributes like class, without overwriting existing attributes.""" + + # Retrieve existing attributes (if any) + attrs = field.field.widget.attrs.copy() + + # Check if the field is a text input or textarea + is_input_or_textarea = isinstance( + field.field.widget, (forms.widgets.TextInput, forms.widgets.Textarea) + ) + + # If the field is an input or textarea, set the content as value or text + if is_input_or_textarea: + attrs["value"] = content.strip() # Set the value to the provided content + + if isinstance(field.field.widget, forms.widgets.Textarea): + field.initial = content.strip() + + # Return the widget with the updated attributes + return field.as_widget(attrs=attrs) diff --git a/core/tests.py b/core/tests.py old mode 100755 new mode 100644 diff --git a/core/urls.py b/core/urls.py old mode 100755 new mode 100644 index 0253d91..6c33d88 --- a/core/urls.py +++ b/core/urls.py @@ -6,4 +6,5 @@ urlpatterns = [ 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"), + path("draft", views.report_draft_update, name="report_draft_update"), ] diff --git a/core/util.py b/core/util.py old mode 100755 new mode 100644 diff --git a/core/views.py b/core/views.py old mode 100755 new mode 100644 index c95bfe2..980b5fc --- a/core/views.py +++ b/core/views.py @@ -7,6 +7,7 @@ from .models import Berichtsheft from django.core.paginator import Paginator from core.styles import STYLE import datetime +import json # Create your views here. @@ -78,11 +79,15 @@ def write_new_report_get(request): 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) - ) + ( + current_year, + current_week, + start_date, + end_date, + current_num, + ) = get_current_report_values(latest) - # TODO : Cookies for persistent saves + draft = BerichtsheftDraft.objects.filter(user=user.id).first() form = user.get_report_kind_form() @@ -98,6 +103,7 @@ def write_new_report_get(request): "current_num": current_num, "report_kind": report_kind, "form": form, + "draft": draft, }, ) @@ -154,3 +160,37 @@ def report_detail_page(request, report_id): return HttpResponse("Nah", status=401) return render(request, "report.html", {"report": report}) + + +from django.shortcuts import get_object_or_404 +from django.http import JsonResponse +from .models import BerichtsheftDraft + + +def report_draft_update(request): + if request.method == "POST": + user = AzureUser(request) + + data = json.loads(request.body) + + department = "" + content = {} + + for key, val in data.items(): + if key == "id_department": + department = val + continue + content[key] = val + + draft, created = BerichtsheftDraft.objects.update_or_create( + user=user.id, defaults={"department": department, "content": content} + ) + + return JsonResponse( + { + "status": "success", + "message": "Draft updated" if not created else "Draft created", + } + ) + + return JsonResponse({"error": "Invalid request method"}, status=400) diff --git a/manage.py b/manage.py old mode 100755 new mode 100644 diff --git a/requirements.txt b/requirements.txt old mode 100755 new mode 100644