86 lines
2.1 KiB
Python
Executable file
86 lines
2.1 KiB
Python
Executable file
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
|
|
from .models import Berichtsheft
|
|
import datetime
|
|
|
|
# Create your views here.
|
|
|
|
|
|
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)
|
|
|
|
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()
|
|
if latest is not None:
|
|
current_year, current_week = next_date(latest.year, latest.week)
|
|
else:
|
|
current_year, current_week, _ = datetime.datetime.today().isocalendar()
|
|
|
|
start_date, end_date = get_week_range(current_year, current_week)
|
|
|
|
if latest is not None:
|
|
current_num = latest.num + 1
|
|
else:
|
|
current_num = 1
|
|
|
|
# TODO : Cookies for persistent saves
|
|
|
|
return render(
|
|
request,
|
|
"write.html",
|
|
{
|
|
"user": user,
|
|
"year": current_year,
|
|
"week": current_week,
|
|
"start_date": start_date,
|
|
"end_date": end_date,
|
|
"current_num": current_num,
|
|
"definition": definition,
|
|
},
|
|
)
|
|
|
|
|
|
def index(request):
|
|
user = AzureUser(request)
|
|
|
|
# Get all berichtshefte
|
|
all_reports = user.reports()
|
|
|
|
return render(request, "index.html", {"user": user, "reports": all_reports})
|
|
|
|
|
|
def test(request):
|
|
return render(request, "test.html", {})
|