|
|
@ -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) |
|
|
|