diff --git a/seminar/templates/generic_form.html b/seminar/templates/generic_form.html new file mode 100644 index 00000000..33e2d276 --- /dev/null +++ b/seminar/templates/generic_form.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} +{% load static %} + + +{% block content %} +

+ {% block nadpis1a %} + {{ nadpis }} + {% endblock %} +

+
+ {% csrf_token %} + + {{ form.as_table }} +
+ +
+ +{% endblock %} diff --git a/seminar/urls.py b/seminar/urls.py index 58fb0335..d825b9b8 100644 --- a/seminar/urls.py +++ b/seminar/urls.py @@ -105,4 +105,11 @@ urlpatterns = [ path('', views.TitulniStranaView.as_view(), name='titulni_strana'), path('jak-resit/', views.JakResitView.as_view(), name='jak_resit'), path('stare-novinky/', views.StareNovinkyView.as_view(), name='stare_novinky'), + + # Dočasné & neodladěné: + path( + 'hidden/hromadne_pridani', + org_required(views.HromadnePridaniView.as_view()), + name="hromadne_pridani" + ), ] diff --git a/seminar/views/docasne.py b/seminar/views/docasne.py index da34d2e6..d13c0016 100644 --- a/seminar/views/docasne.py +++ b/seminar/views/docasne.py @@ -1,4 +1,9 @@ from http import HTTPStatus + +from django.db import transaction +from django.forms import Form, CharField, IntegerField +from django.views.generic import FormView + import seminar.models as m from django.shortcuts import render, get_object_or_404 @@ -14,3 +19,49 @@ def problemView(request, pk): status_code = HTTPStatus.NOT_FOUND return render(request, template, context=ctx, status=status_code) + +# FIXME Tohle možná patří do forms.py +# FIXME Tohle není odladěné, selhává to nekontrolovaně +# a obecně je to fujky nekomentovaný kód +class HromadnePridaniForm(Form): + tema = CharField(label="Název tématu:") + dil = IntegerField(label="Díl:", min_value=1) + body = CharField(label="Počty bodů (0 pro problém) oddělené čárkami:") + + +# FIXME Tohle není odladěné, selhává to nekontrolovaně +# a obecně je to fujky nekomentovaný kód +class HromadnePridaniView(FormView): + form_class = HromadnePridaniForm + template_name = "generic_form.html" + success_url = 'hromadne_pridani' + + def get_context_data(self, **kwargs): + context = super().get_context_data() + context["nadpis"] = "Hromadné přidání úloh" + context["form_url"] = "hromadne_pridani" + return context + + def form_valid(self, form): + cd = form.cleaned_data + tema = cd["tema"] + dil = cd["dil"] + body = list(map(int, cd["body"].split(","))) + + t = m.Problem.objects.get(nazev__exact=tema, nadproblem=None) + with transaction.atomic(): + pfx = f"{t.nazev}, díl {dil}, " + + for k, b in enumerate(body): + u = m.Uloha.objects.create( + nadproblem=t, + nazev=pfx + f"{'úloha' if b > 0 else 'problém'} {k + 1}", + autor=t.autor, + garant=t.garant, + max_body=b, + cislo_zadani=m.Cislo.get(t.rocnik.rocnik, dil), + kod=k, + stav=m.Problem.STAV_ZADANY, + ) + u.opravovatele.set(t.opravovatele.all()) + return super().form_valid(form)