diff --git a/core/azure_auth.py b/core/azure_auth.py index 2636fb9..3797d8a 100755 --- a/core/azure_auth.py +++ b/core/azure_auth.py @@ -2,6 +2,7 @@ import json import base64 from core.models import Berichtsheft +from core.report_templates import ReportTemplates class AzureUser: @@ -33,3 +34,7 @@ class AzureUser: def reports(self): return Berichtsheft.objects.filter(user=self.id) + + def get_report_template(self): + # TODO : Implement + return ReportTemplates.get_template("weekly") diff --git a/core/forms.py b/core/forms.py new file mode 100755 index 0000000..be9c917 --- /dev/null +++ b/core/forms.py @@ -0,0 +1,5 @@ +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/models.py b/core/models.py index 0f00920..8a57865 100755 --- a/core/models.py +++ b/core/models.py @@ -31,9 +31,7 @@ class Berichtsheft(models.Model): created = models.DateTimeField(auto_now_add=True) def __str__(self): - return ( - f"Berichtsheft: {self.user.username}, Year: {self.year}, Week: {self.week}" - ) + return f"Berichtsheft: {self.user}, Year: {self.year}, Week: {self.week}" class Approval: diff --git a/core/templates/write.html b/core/templates/write.html index 51cd0ed..7d3ccbe 100755 --- a/core/templates/write.html +++ b/core/templates/write.html @@ -37,7 +37,7 @@ value = "{{ user.display_name }}" {% endif %} - > + > {% endfor %} diff --git a/core/views.py b/core/views.py index 33e1180..7f760ec 100755 --- a/core/views.py +++ b/core/views.py @@ -1,5 +1,6 @@ -from django.shortcuts import render +from django.shortcuts import 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 @@ -10,10 +11,36 @@ import datetime def write_new_report(request): + if request.method == "POST": + return write_new_report_post(request) + else: + return write_new_report_get(request) + + +def write_new_report_post(request): user = AzureUser(request) - # TODO : Get template for user - definition = ReportTemplates.get_template("weekly") + definition = user.get_report_template() + + values = extract_post_values(request.POST, definition["vars"]) + + # TODO : Input Validation + + report = Berichtsheft( + user=user.id, + num=int(values.pop("num_doc", "")), + year=int(values.pop("year", "")), + week=int(values.pop("week", "")), + 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()