Dynamický update stavu opravy

This commit is contained in:
Jonas Havelka 2025-01-20 23:40:06 +01:00
parent 9b0fe3d32f
commit d2e199e509
3 changed files with 37 additions and 0 deletions

View file

@ -4,4 +4,5 @@ from . import views
urlpatterns = [
path('korektury/api/<int:pdf_id>/stav', org_required(views.korektury_stav_view), name='korektury_api_pdf_stav'),
path('korektury/api/oprava/stav', org_required(views.oprava_stav_view), name='korektury_api_oprava_stav'),
]

View file

@ -18,3 +18,17 @@ def korektury_stav_view(request, pdf_id: int, **kwargs):
pdf.status = status
pdf.save()
return JsonResponse({'status': pdf.status})
def oprava_stav_view(request, **kwargs):
q = request.POST
op_id_str = q.get('id')
assert op_id_str is not None
op_id = int(op_id_str)
op = get_object_or_404(Oprava, id=op_id)
status = q.get('action')
if status is not None:
assert status in Oprava.STATUS.values
op.status = status
op.save()
return JsonResponse({'status': op.status})

View file

@ -42,3 +42,25 @@
<hr>
</body>
</html>
<script>
function zmenStavKorektury(event) {
event.preventDefault();
oprava = event.target.parentElement.parentElement.parentElement.parentElement;
const data = new FormData(event.target);
data.append("action", event.submitter.value);
fetch("{% url 'korektury_api_oprava_stav' %}", {method: "POST", body: data})
.then(response => {
if (!response.ok) {alert("Něco se nepovedlo:" + response.statusText);}
else response.json().then(data => {oprava.dataset.opravastatus = data["status"]; oprava.previousElementSibling.dataset.opravastatus = data["status"]});
})
.catch(error => {alert("Něco se nepovedlo:" + error);});
}
for (const form of document.querySelectorAll("[action='']")) form.addEventListener('submit', async event => { zmenStavKorektury(event); });
</script>