156 lines
3.9 KiB
Python
Executable file
156 lines
3.9 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 django.core.paginator import Paginator
|
|
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)
|
|
|
|
if not report_form.is_valid():
|
|
return HttpResponse("Bad Request", status=400)
|
|
|
|
# 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.latest_report()
|
|
|
|
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
|
|
reports = Paginator(user.reports(), 30).get_page(1)
|
|
|
|
year_now, week_now, _ = datetime.datetime.today().isocalendar()
|
|
|
|
return render(
|
|
request,
|
|
"index.html",
|
|
{
|
|
"user": user,
|
|
"reports": reports,
|
|
"week_now": week_now,
|
|
"late_reports": user.late_reports(),
|
|
"style": STYLE,
|
|
},
|
|
)
|
|
|
|
|
|
def reports_list(request):
|
|
user = AzureUser(request)
|
|
|
|
p = Paginator(user.reports(), 30)
|
|
|
|
try:
|
|
page_num = int(request.GET.get("page"))
|
|
except:
|
|
return HttpResponse("Page should be a number", 400)
|
|
|
|
if p.num_pages >= page_num:
|
|
objs = p.get_page(page_num)
|
|
else:
|
|
objs = []
|
|
|
|
return render(
|
|
request,
|
|
"htmx/reports.html",
|
|
{"reports": objs, "next": page_num + 1, "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})
|