azube/core/views.py
Angelo Rodriguez 34f3367a08 work
2024-12-04 09:37:01 +01:00

126 lines
3.2 KiB
Python
Executable file

from django.http import HttpResponse
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 core.styles import STYLE
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)
report_form = user.get_report_kind_form(request.POST)
# TODO : Implement values
current_year, current_week, _, _, current_num = get_current_report_values(
user.reports().order_by("-year", "-week").first()
)
report = Berichtsheft(
user=user.id,
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 get_current_report_values(latest):
year_now, week_now, _ = datetime.datetime.today().isocalendar()
if latest is not None:
current_year, current_week = next_date(latest.year, latest.week)
else:
current_year, current_week = year_now, week_now
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
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",
{
"user": user,
"year": current_year,
"week": current_week,
"start_date": start_date,
"end_date": end_date,
"current_num": current_num,
"report_kind": report_kind,
"form": form,
},
)
def index(request):
user = AzureUser(request)
# Get all berichtshefte
all_reports = user.reports()
year_now, week_now, _ = datetime.datetime.today().isocalendar()
return render(
request,
"index.html",
{"user": user, "reports": all_reports, "week_now": week_now, "style": STYLE},
)
def report_detail_page(request, report_id):
user = AzureUser(request)
report = get_object_or_404(Berichtsheft, id=report_id)
if report.user != user.id:
return HttpResponse("Nah", status=401)
return render(
request, "report.html", {"report": report, "approved": report.is_approved()}
)