Browse Source

Dynamický update stavu

korekturovatko
Jonas Havelka 2 weeks ago
parent
commit
dd86fc1fcb
  1. 7
      korektury/api/urls.py
  2. 20
      korektury/api/views.py
  3. 35
      korektury/templates/korektury/korekturovatko/_zmena_stavu.html
  4. 4
      korektury/urls.py

7
korektury/api/urls.py

@ -0,0 +1,7 @@
from django.urls import path
from personalni.utils import org_required
from . import views
urlpatterns = [
path('korektury/api/<int:pdf_id>/stav', org_required(views.korektury_stav_view), name='korektury_api_pdf_stav'),
]

20
korektury/api/views.py

@ -0,0 +1,20 @@
from django.http import HttpResponseForbidden, JsonResponse
from django.shortcuts import get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from rest_framework import serializers
from korektury.utils import send_email_notification_komentar
from korektury.models import Oprava, KorekturovanePDF, Komentar
from personalni.models import Organizator, Osoba
def korektury_stav_view(request, pdf_id: int, **kwargs):
q = request.POST
pdf = get_object_or_404(KorekturovanePDF, id=pdf_id)
status = q.get('state')
if status is not None:
assert status in KorekturovanePDF.STATUS.values
pdf.status = status
pdf.save()
return JsonResponse({'status': pdf.status})

35
korektury/templates/korektury/korekturovatko/_zmena_stavu.html

@ -1,9 +1,8 @@
<h4>Změnit stav PDF:</h4>
<i>Aktuální: {{pdf.status}}</i>
<br>
<form method="post">
<form method="post" id="PDFSTAV_FORM">
{% csrf_token %}
<input type='hidden' name='action' value='set-state'/>
<input type="radio" name="state" value="{{ pdf.STATUS.PRIDAVANI }}" {% if pdf.status == pdf.STATUS.PRIDAVANI %} checked {% endif %}>Přidávání korektur
<br>
<input type="radio" name="state" value="{{ pdf.STATUS.ZANASENI }}" {% if pdf.status == pdf.STATUS.ZANASENI %} checked {% endif %}>Zanášení korektur
@ -12,3 +11,35 @@
<br>
<input type='submit' value='Změnit stav PDF'/>
</form>
<script>
const pdfstav_form = document.getElementById('PDFSTAV_FORM');
const csrf_form = document.getElementById('CSRF_FORM');
/**
*
* @param {RequestInit} data
* @param {Boolean} catchError
*/
function fetchStav(data, catchError=true) {
fetch("{% url 'korektury_api_pdf_stav' pdf.id %}", data
)
.then(response => {
if (!response.ok) { if (catchError) alert("Něco se nepovedlo:" + response.statusText);}
else response.json().then(data => document.body.dataset.status = data["status"]);
})
.catch(error => {if (catchError) alert("Něco se nepovedlo:" + error);});
}
pdfstav_form.addEventListener('submit', async event => {
event.preventDefault();
const data = new FormData(pdfstav_form);
fetchStav({method: "POST", body: data});
});
// FIXME není mi jasné, zda v {} nemá být `cache: "no-store"`, aby prohlížeč necachoval get.
setInterval(() => fetchStav({}, false), 30000); // Každou půl minutu fetchni stav
</script>

4
korektury/urls.py

@ -1,4 +1,6 @@
from django.urls import path
from django.urls import include
from personalni.utils import org_required
from . import views
@ -7,4 +9,6 @@ urlpatterns = [
path('korektury/neseskupene/', org_required(views.KorekturyAktualniListView.as_view()), name='korektury_neseskupene_list'),
path('korektury/zastarale/', org_required(views.KorekturyZastaraleListView.as_view()), name='korektury_stare_list'),
path('korektury/<int:pdf>/', org_required(views.KorekturyView.as_view()), name='korektury'),
path('api/', include('korektury.api.urls')),
]

Loading…
Cancel
Save