work+htmx

This commit is contained in:
Angelo Rodriguez 2024-12-04 11:41:57 +01:00
parent 34f3367a08
commit d248c99242
11 changed files with 111 additions and 29 deletions

View file

@ -1,8 +1,10 @@
import datetime
import json
import base64
from core.models import Berichtsheft
from core.reports import DailyReport, WeeklyReport
import core.util
class AzureUser:
@ -33,7 +35,7 @@ class AzureUser:
self.id = "anon"
def reports(self):
return Berichtsheft.objects.filter(user=self.id)
return Berichtsheft.objects.filter(user=self.id).order_by("-year", "-week")
def get_report_kind(self):
# TODO : Implement
@ -45,3 +47,19 @@ class AzureUser:
return WeeklyReport(request)
case "daily":
return DailyReport(request)
def latest_report(self):
return self.reports().order_by("-year", "-week").first()
def late_reports(self) -> int:
year_now, week_now, _ = datetime.datetime.today().isocalendar()
count = 0
latest = self.latest_report()
new_year, new_week = (latest.year, latest.week)
while week_now != new_week or (week_now == new_week and year_now != new_year):
count += 1
new_year, new_week = core.util.next_date(new_year, new_week)
return count

View file

@ -43,6 +43,7 @@ class Berichtsheft(models.Model):
def __str__(self):
return f"Berichtsheft: {self.user}, Year: {self.year}, Week: {self.week}"
@property
def is_approved(self):
approvals = Approval.objects.filter(report=self.id)
return len(approvals) >= 2

View file

@ -2,5 +2,5 @@
STYLE = {
"red_btn": "text-white bg-red-700 hover:bg-red-800 focus:outline-none focus:ring-4 focus:ring-red-300 font-medium rounded-full text-sm px-5 py-2.5 text-center me-2 mb-2 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-900",
"card": "bg-white drop-shadow-md p-6 mx-auto my-10 aspect-[2/3] hover:drop-shadow-xl hover:scale-[0.95] scale-[0.9] transition-all duration-50 transform ease-in-out",
"card": "bg-white drop-shadow-md p-6 mx-auto my-10 aspect-[2/3] hover:drop-shadow-xl hover:scale-[0.95] scale-[0.9] transition-all duration-50 transform ease-in-out w-80",
}

View file

@ -0,0 +1,9 @@
<a href="/report/{{ report.id }}" class="{{ style.card }}">
{% if report.is_approved %}
<div
class="absolute top-0 right-0 translate-y-3 -translate-x-3 bg-green-500 text-white rounded-full w-6 h-6 flex items-center justify-center scale-[1.5]"
>
</div>
{% endif %} {{ report }}
</a>

2
core/templates/head.html Executable file
View file

@ -0,0 +1,2 @@
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/htmx.org@2.0.3" integrity="sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq" crossorigin="anonymous"></script>

View file

@ -0,0 +1,12 @@
{% if reports|length != 0 %}
{% for report in reports %}
{% include 'component/report.html' with report=report %}
{% endfor %}
<div hx-get="/reports?page={{ next }}"
hx-trigger="revealed"
hx-swap="outerHTML">
{% endif %}

View file

@ -1,43 +1,50 @@
<html>
<head>
<title>Azube</title>
<script src="https://cdn.tailwindcss.com"></script>
{% include 'head.html' %}
</head>
<body>
{{ user.display_name }}
<div class="bg-gray-100">
<div class="bg-gray-100 min-h-screen">
<h1 class="text-2xl justify-center flex pt-5">Deine Berichtshefte</h1>
{% if late_reports > 1 %}
<div class="bg-yellow-100 border-l-4 border-yellow-500 text-yellow-700 p-4 mr-10 ml-10 mt-6" role="alert">
<p class="font-bold"> Du bist nicht aktuell! </p>
<p> Du hast noch {{ late_reports }} Berichtshefte nachzuschreiben. </p>
</div>
{% endif %}
{% if reports|length == 0 %}
<h3>wau, such empty!</h3>
<h3 class="text-xl justify-center flex m-10 p-5">wau, such empty!</h3>
<button onclick="location = '/write'" class="{{ style.red_btn }}">
<button onclick="location = '/write'" class="{{ style.red_btn }} justify-center">
Füge dein erstes Berichtsheft hinzu!
</button>
{% else %}
<div
class="min-h-screen flex items-center justify-center grid grid-cols-5"
class="mb-5 flex items-center justify-center grid lg:grid-cols-6 md:grid-cols-3"
>
{% for report in reports %}
<a href="/report/{{ report.id }}" class="{{ style.card }}">
{% if approved %}
<div
class="absolute top-0 right-0 translate-y-3 -translate-x-3 bg-green-500 text-white rounded-full w-6 h-6 flex items-center justify-center scale-[1.5]"
>
</div>
{% endif %} {{ report }}
{% if forloop.first %} {% if report.week != week_now %}
<a href="/write" class="{{ style.card }} flex items-center justify-center" title="Neues Berichtsheft">
<span class="text-9xl font-bold"> + </span>
</a>
{% endif %} {% endif %}
{% include 'component/report.html' with report=report %}
{% endfor %}
<div hx-get="/reports?page=2"
hx-trigger="revealed"
hx-swap="outerHTML">
{% endif %}
{% if forloop.last %} {% if report.week != week_now %}
<button onclick="location = '/write'" class="{{ style.red_btn }}">
Berichtsheft schreiben
</button>
{% endif %} {% endif %} {% endfor %} {% endif %}
</div>
</div>
</body>

View file

@ -1,13 +1,14 @@
<html>
<head>
<title> Berichtsheft {{ report.num }} </title>
{% include 'head.html' %}
</head>
<body>
<h1> Berichtsheft {{ report.num }} </h1>
<p> Berichtsheft {{ report.year }} / {{ report.week }} </p>
<p> Is Approved ? {{ approved }} </p>
<p> Is Approved ? {{ report.is_approved }} </p>
{% for key, value in report.content.items %}

View file

@ -1,6 +1,7 @@
<html>
<head>
<title>Neues Berichtsheft</title>
{% include 'head.html' %}
</head>
<body>
<h1>Neues Berichtsheft</h1>

View file

@ -5,4 +5,5 @@ urlpatterns = [
path("", views.index, name="index"),
path("write", views.write_new_report, name="write"),
path("report/<int:report_id>", views.report_detail_page, name="report_detail"),
path("reports", views.reports_list, name="reports_list"),
]

View file

@ -4,6 +4,7 @@ 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
@ -22,6 +23,9 @@ def write_new_report_post(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(
@ -65,7 +69,7 @@ def write_new_report_get(request):
report_kind = user.get_report_kind()
# Get the latest year and week
latest = user.reports().order_by("-year", "-week").first()
latest = user.latest_report()
year_now, week_now, _ = datetime.datetime.today().isocalendar()
@ -102,14 +106,42 @@ def index(request):
user = AzureUser(request)
# Get all berichtshefte
all_reports = user.reports()
reports = Paginator(user.reports(), 30).get_page(1)
year_now, week_now, _ = datetime.datetime.today().isocalendar()
return render(
request,
"index.html",
{"user": user, "reports": all_reports, "week_now": week_now, "style": STYLE},
{
"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},
)
@ -121,6 +153,4 @@ def report_detail_page(request, report_id):
if report.user != user.id:
return HttpResponse("Nah", status=401)
return render(
request, "report.html", {"report": report, "approved": report.is_approved()}
)
return render(request, "report.html", {"report": report})