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 django.template.loader import render_to_string from core.styles import STYLE from .util import is_htmx_request, title, htmx_request from .reports import choose_report_kind import datetime import json # 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, request.FILES) 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() ) upload = report_form.cleaned_data["img"] img = upload.read() if upload is not None else None 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(), image=img, ) report.save() # Clear draft BerichtsheftDraft.objects.filter(user=user.id).delete() 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) draft = BerichtsheftDraft.objects.filter(user=user.id).first() form = user.get_report_kind_form() return htmx_request( 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, "draft": draft, }, "Neues Berichtsheft", ) 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 htmx_request( request, "index.html", { "user": user, "reports": reports, "week_now": week_now, "late_reports": user.late_reports(), "style": STYLE, }, "Berichtshefte", ) 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) form = choose_report_kind(report.kind) if report.user != user.id: return HttpResponse("Nah", status=401) return htmx_request( request, "report.html", {"report": report, "form": form}, f"Berichtsheft {report.num}", ) 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) def settings_page(request): user = AzureUser(request) return htmx_request( request, "settings.html", {"user": user}, "Einstellungen", )