azube/core/views.py

227 lines
5.6 KiB
Python
Raw Normal View History

2024-12-03 15:15:45 +01:00
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
2024-12-03 11:38:13 +01:00
from core.util import get_week_range, next_date
2024-12-02 16:49:45 +01:00
from .azure_auth import AzureUser
from .models import Berichtsheft
2024-12-04 11:41:57 +01:00
from django.core.paginator import Paginator
2024-12-05 17:03:42 +01:00
from django.template.loader import render_to_string
2024-12-03 15:15:45 +01:00
from core.styles import STYLE
2024-12-05 17:03:42 +01:00
from .util import is_htmx_request, title, htmx_request
2024-12-06 12:10:30 +01:00
from .reports import choose_report_kind
2024-12-02 16:49:45 +01:00
import datetime
2024-12-05 13:45:51 +01:00
import json
2024-12-02 16:49:45 +01:00
# Create your views here.
def write_new_report(request):
2024-12-03 13:13:50 +01:00
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)
2024-12-06 12:10:30 +01:00
report_form = user.get_report_kind_form(request.POST, request.FILES)
2024-12-03 13:13:50 +01:00
2024-12-04 11:41:57 +01:00
if not report_form.is_valid():
return HttpResponse("Bad Request", status=400)
2024-12-04 09:37:01 +01:00
# TODO : Implement values
2024-12-03 13:13:50 +01:00
2024-12-04 09:37:01 +01:00
current_year, current_week, _, _, current_num = get_current_report_values(
user.reports().order_by("-year", "-week").first()
)
2024-12-03 13:13:50 +01:00
2024-12-06 12:10:30 +01:00
upload = report_form.cleaned_data["img"]
img = upload.read() if upload is not None else None
2024-12-03 13:13:50 +01:00
report = Berichtsheft(
user=user.id,
2024-12-04 09:37:01 +01:00
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(),
2024-12-06 12:10:30 +01:00
image=img,
2024-12-03 13:13:50 +01:00
)
report.save()
2024-12-06 12:10:30 +01:00
# Clear draft
BerichtsheftDraft.objects.filter(user=user.id).delete()
2024-12-03 13:13:50 +01:00
return redirect("/")
2024-12-04 09:37:01 +01:00
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
2024-12-03 13:13:50 +01:00
def write_new_report_get(request):
2024-12-02 16:49:45 +01:00
user = AzureUser(request)
2024-12-04 09:37:01 +01:00
report_kind = user.get_report_kind()
2024-12-02 16:49:45 +01:00
# Get the latest year and week
2024-12-04 11:41:57 +01:00
latest = user.latest_report()
2024-12-03 15:15:45 +01:00
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}")
2024-12-05 13:45:51 +01:00
(
current_year,
current_week,
start_date,
end_date,
current_num,
) = get_current_report_values(latest)
2024-12-03 11:38:13 +01:00
2024-12-05 13:45:51 +01:00
draft = BerichtsheftDraft.objects.filter(user=user.id).first()
2024-12-03 11:38:13 +01:00
2024-12-04 09:37:01 +01:00
form = user.get_report_kind_form()
2024-12-05 17:03:42 +01:00
return htmx_request(
2024-12-03 11:38:13 +01:00
request,
"write.html",
{
"user": user,
"year": current_year,
"week": current_week,
"start_date": start_date,
"end_date": end_date,
"current_num": current_num,
2024-12-04 09:37:01 +01:00
"report_kind": report_kind,
"form": form,
2024-12-05 13:45:51 +01:00
"draft": draft,
2024-12-03 11:38:13 +01:00
},
2024-12-05 17:03:42 +01:00
"Neues Berichtsheft",
2024-12-03 11:38:13 +01:00
)
2024-12-02 16:49:45 +01:00
def index(request):
user = AzureUser(request)
# Get all berichtshefte
2024-12-04 11:41:57 +01:00
reports = Paginator(user.reports(), 30).get_page(1)
2024-12-02 16:49:45 +01:00
2024-12-03 15:15:45 +01:00
year_now, week_now, _ = datetime.datetime.today().isocalendar()
2024-12-05 17:03:42 +01:00
return htmx_request(
2024-12-03 15:15:45 +01:00
request,
"index.html",
2024-12-04 11:41:57 +01:00
{
"user": user,
"reports": reports,
"week_now": week_now,
"late_reports": user.late_reports(),
"style": STYLE,
},
2024-12-05 17:03:42 +01:00
"Berichtshefte",
2024-12-04 11:41:57 +01:00
)
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},
2024-12-03 15:15:45 +01:00
)
def report_detail_page(request, report_id):
user = AzureUser(request)
report = get_object_or_404(Berichtsheft, id=report_id)
2024-12-06 12:10:30 +01:00
form = choose_report_kind(report.kind)
2024-12-03 15:15:45 +01:00
if report.user != user.id:
return HttpResponse("Nah", status=401)
2024-12-05 17:03:42 +01:00
return htmx_request(
2024-12-06 12:10:30 +01:00
request,
"report.html",
{"report": report, "form": form},
f"Berichtsheft {report.num}",
2024-12-05 17:03:42 +01:00
)
2024-12-05 13:45:51 +01:00
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)
2024-12-06 14:04:17 +01:00
def settings_page(request):
user = AzureUser(request)
return htmx_request(
request,
"settings.html",
{"user": user},
"Einstellungen",
)