.
This commit is contained in:
@@ -12,6 +12,8 @@ urlpatterns = [
|
|||||||
path("rota/<int:rota_id>/shift/<int:idx>/edit/", views.shift_edit, name="shift_edit"),
|
path("rota/<int:rota_id>/shift/<int:idx>/edit/", views.shift_edit, name="shift_edit"),
|
||||||
path("rota/<int:rota_id>/shift/<int:idx>/delete/", views.shift_delete, name="shift_delete"),
|
path("rota/<int:rota_id>/shift/<int:idx>/delete/", views.shift_delete, name="shift_delete"),
|
||||||
path("worker/add/", views.worker_create, name="worker_add"),
|
path("worker/add/", views.worker_create, name="worker_add"),
|
||||||
|
path("worker/<int:rota_id>/<int:worker_id>/edit/", views.worker_edit, name="worker_edit"),
|
||||||
|
path("worker/<int:rota_id>/<int:worker_id>/delete/", views.worker_delete, name="worker_delete"),
|
||||||
path("worker/<int:worker_id>/", views.worker_detail, name="worker_detail"),
|
path("worker/<int:worker_id>/", views.worker_detail, name="worker_detail"),
|
||||||
path("run/<int:run_id>/", views.rota_run_detail, name="rota_run_detail"),
|
path("run/<int:run_id>/", views.rota_run_detail, name="rota_run_detail"),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -64,6 +64,79 @@ def worker_detail(request, worker_id):
|
|||||||
return render(request, "rota/worker_detail.html", {"worker": worker, "form": form})
|
return render(request, "rota/worker_detail.html", {"worker": worker, "form": form})
|
||||||
|
|
||||||
|
|
||||||
|
@require_http_methods(["GET", "POST"])
|
||||||
|
def worker_edit(request, rota_id, worker_id):
|
||||||
|
rota = get_object_or_404(RotaSchedule, pk=rota_id)
|
||||||
|
worker = get_object_or_404(Worker, pk=worker_id)
|
||||||
|
|
||||||
|
if request.method == "POST":
|
||||||
|
form = WorkerForm(request.POST, instance=worker)
|
||||||
|
if form.is_valid():
|
||||||
|
form.save()
|
||||||
|
# Return updated worker list OOB and close modal
|
||||||
|
worker_list_html = render_to_string("rota/partials/worker_list.html", {"rota": rota}, request=request)
|
||||||
|
resp = (
|
||||||
|
f'<div id="worker-list" hx-swap-oob="true">{worker_list_html}</div>'
|
||||||
|
+ '<div id="modal" hx-swap-oob="true"></div>'
|
||||||
|
)
|
||||||
|
response = HttpResponse(resp)
|
||||||
|
response["HX-Trigger"] = "closeModal"
|
||||||
|
return response
|
||||||
|
|
||||||
|
# invalid: return form modal with errors
|
||||||
|
modal_html = render_to_string(
|
||||||
|
"rota/partials/worker_form_modal.html",
|
||||||
|
{"form": form, "form_action": request.path, "rota": rota},
|
||||||
|
request=request,
|
||||||
|
)
|
||||||
|
return HttpResponse(modal_html)
|
||||||
|
|
||||||
|
# GET: populate form and return modal
|
||||||
|
form = WorkerForm(instance=worker)
|
||||||
|
modal_html = render_to_string(
|
||||||
|
"rota/partials/worker_form_modal.html",
|
||||||
|
{"form": form, "form_action": request.path, "rota": rota},
|
||||||
|
request=request,
|
||||||
|
)
|
||||||
|
return HttpResponse(modal_html)
|
||||||
|
|
||||||
|
|
||||||
|
@require_http_methods(["GET", "POST"])
|
||||||
|
def worker_delete(request, rota_id, worker_id):
|
||||||
|
rota = get_object_or_404(RotaSchedule, pk=rota_id)
|
||||||
|
worker = get_object_or_404(Worker, pk=worker_id)
|
||||||
|
|
||||||
|
if request.method == "POST":
|
||||||
|
# remove assignment between worker and rota
|
||||||
|
try:
|
||||||
|
from .models import Assignment
|
||||||
|
|
||||||
|
Assignment.objects.filter(worker=worker, rota=rota).delete()
|
||||||
|
except Exception:
|
||||||
|
try:
|
||||||
|
rota.workers.remove(worker)
|
||||||
|
except Exception:
|
||||||
|
return HttpResponseBadRequest("Unable to remove worker from rota")
|
||||||
|
|
||||||
|
rota = get_object_or_404(RotaSchedule, pk=rota_id)
|
||||||
|
worker_list_html = render_to_string("rota/partials/worker_list.html", {"rota": rota}, request=request)
|
||||||
|
resp = (
|
||||||
|
f'<div id="worker-list" hx-swap-oob="true">{worker_list_html}</div>'
|
||||||
|
+ '<div id="modal" hx-swap-oob="true"></div>'
|
||||||
|
)
|
||||||
|
response = HttpResponse(resp)
|
||||||
|
response["HX-Trigger"] = "closeModal"
|
||||||
|
return response
|
||||||
|
|
||||||
|
# GET: confirmation modal
|
||||||
|
modal_html = render_to_string(
|
||||||
|
"rota/partials/worker_confirm_delete.html",
|
||||||
|
{"rota": rota, "worker": worker},
|
||||||
|
request=request,
|
||||||
|
)
|
||||||
|
return HttpResponse(modal_html)
|
||||||
|
|
||||||
|
|
||||||
def rota_run_detail(request, run_id):
|
def rota_run_detail(request, run_id):
|
||||||
from .models import RotaRun
|
from .models import RotaRun
|
||||||
|
|
||||||
@@ -96,12 +169,15 @@ def shift_add(request, rota_id):
|
|||||||
|
|
||||||
# Return out-of-band swap: update shift-list and clear modal
|
# Return out-of-band swap: update shift-list and clear modal
|
||||||
shift_list_html = render_to_string("rota/partials/shift_list.html", {"rota": rota}, request=request)
|
shift_list_html = render_to_string("rota/partials/shift_list.html", {"rota": rota}, request=request)
|
||||||
# wrap shift list for OOB swap
|
# wrap shift list for OOB swap and clear form/modal containers
|
||||||
resp = (
|
resp = (
|
||||||
f'<div id="shift-list" hx-swap-oob="true">{shift_list_html}</div>'
|
f'<div id="shift-list" hx-swap-oob="true">{shift_list_html}</div>'
|
||||||
|
+ '<div id="shift-form" hx-swap-oob="true"></div>'
|
||||||
+ '<div id="modal" hx-swap-oob="true"></div>'
|
+ '<div id="modal" hx-swap-oob="true"></div>'
|
||||||
)
|
)
|
||||||
return HttpResponse(resp)
|
response = HttpResponse(resp)
|
||||||
|
response["HX-Trigger"] = "closeModal"
|
||||||
|
return response
|
||||||
|
|
||||||
# invalid: return modal with form and errors
|
# invalid: return modal with form and errors
|
||||||
modal_html = render_to_string("rota/partials/shift_form_modal.html", {"form": form, "rota": rota, "form_action": request.path}, request=request)
|
modal_html = render_to_string("rota/partials/shift_form_modal.html", {"form": form, "rota": rota, "form_action": request.path}, request=request)
|
||||||
@@ -141,9 +217,12 @@ def shift_edit(request, rota_id, idx):
|
|||||||
shift_list_html = render_to_string("rota/partials/shift_list.html", {"rota": rota}, request=request)
|
shift_list_html = render_to_string("rota/partials/shift_list.html", {"rota": rota}, request=request)
|
||||||
resp = (
|
resp = (
|
||||||
f'<div id="shift-list" hx-swap-oob="true">{shift_list_html}</div>'
|
f'<div id="shift-list" hx-swap-oob="true">{shift_list_html}</div>'
|
||||||
|
+ '<div id="shift-form" hx-swap-oob="true"></div>'
|
||||||
+ '<div id="modal" hx-swap-oob="true"></div>'
|
+ '<div id="modal" hx-swap-oob="true"></div>'
|
||||||
)
|
)
|
||||||
return HttpResponse(resp)
|
response = HttpResponse(resp)
|
||||||
|
response["HX-Trigger"] = "closeModal"
|
||||||
|
return response
|
||||||
|
|
||||||
modal_html = render_to_string("rota/partials/shift_form_modal.html", {"form": form, "rota": rota, "form_action": request.path}, request=request)
|
modal_html = render_to_string("rota/partials/shift_form_modal.html", {"form": form, "rota": rota, "form_action": request.path}, request=request)
|
||||||
return HttpResponse(modal_html)
|
return HttpResponse(modal_html)
|
||||||
@@ -181,9 +260,12 @@ def shift_delete(request, rota_id, idx):
|
|||||||
shift_list_html = render_to_string("rota/partials/shift_list.html", {"rota": rota}, request=request)
|
shift_list_html = render_to_string("rota/partials/shift_list.html", {"rota": rota}, request=request)
|
||||||
resp = (
|
resp = (
|
||||||
f'<div id="shift-list" hx-swap-oob="true">{shift_list_html}</div>'
|
f'<div id="shift-list" hx-swap-oob="true">{shift_list_html}</div>'
|
||||||
|
+ '<div id="shift-form" hx-swap-oob="true"></div>'
|
||||||
+ '<div id="modal" hx-swap-oob="true"></div>'
|
+ '<div id="modal" hx-swap-oob="true"></div>'
|
||||||
)
|
)
|
||||||
return HttpResponse(resp)
|
response = HttpResponse(resp)
|
||||||
|
response["HX-Trigger"] = "closeModal"
|
||||||
|
return response
|
||||||
|
|
||||||
# GET: confirmation modal
|
# GET: confirmation modal
|
||||||
modal_html = render_to_string("rota/partials/shift_confirm_delete.html", {"rota": rota, "idx": idx, "shift": shift}, request=request)
|
modal_html = render_to_string("rota/partials/shift_confirm_delete.html", {"rota": rota, "idx": idx, "shift": shift}, request=request)
|
||||||
|
|||||||
Reference in New Issue
Block a user