Dynamický update stavu
This commit is contained in:
parent
341ae7ce45
commit
dd86fc1fcb
4 changed files with 64 additions and 2 deletions
7
korektury/api/urls.py
Normal file
7
korektury/api/urls.py
Normal file
|
@ -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
Normal file
20
korektury/api/views.py
Normal file
|
@ -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})
|
|
@ -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>
|
||||
|
||||
|
|
|
@ -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…
Reference in a new issue