@ -221,9 +221,8 @@ class DetailReseniView(DetailView):
def get_context_data ( self , * * kw ) :
reseni = get_object_or_404 ( m . Reseni , id = self . kwargs [ ' pk ' ] )
initial = m . Hodnoceni . objects . filter ( reseni = reseni ) . values ( ' problem ' , ' body ' , ' deadline_body ' , ' feedback ' , )
ctx = super ( ) . get_context_data ( * * kw )
ctx [ ' form ' ] = f . OhodnoceniReseniFormSet ( initial = initial )
ctx [ ' form ' ] = f . HodnoceniFormSet ( queryset = m . Hodnoceni . objects . filter ( reseni = reseni ) )
ctx [ ' poznamka_form ' ] = f . PoznamkaReseniForm ( instance = reseni )
return ctx
@ -231,34 +230,18 @@ class DetailReseniView(DetailView):
def hodnoceniReseniView ( request , pk , * args , * * kwargs ) :
reseni = get_object_or_404 ( m . Reseni , pk = pk )
template_name = ' odevzdavatko/detail.html '
form_class = f . OhodnoceniRes eniFormSet
form_class = f . Hodnoc eniFormSet
success_url = reverse ( ' odevzdavatko_detail_reseni ' , kwargs = { ' pk ' : pk } )
# FIXME: Použit initial i tady a nebastlit hodnocení tak nízkoúrovňově
# Also: https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/#django.forms.ModelForm
formset = f . OhodnoceniReseniFormSet ( request . POST )
hodnoceni_formset = f . HodnoceniFormSet ( request . POST , queryset = m . Hodnoceni . objects . filter ( reseni = reseni ) )
poznamka_form = f . PoznamkaReseniForm ( request . POST , instance = reseni )
# TODO: Napsat validaci formuláře a formsetu
if not ( formset . is_valid ( ) and poznamka_form . is_valid ( ) ) :
raise ValueError ( formset . errors , poznamka_form . errors )
if not ( hodnoceni_ formset. is_valid ( ) and poznamka_form . is_valid ( ) ) :
raise ValueError ( hodnoceni_ formset. errors , poznamka_form . errors )
with transaction . atomic ( ) :
# Poznámka je jednoduchá na zpracování:
poznamka_form . save ( )
# Smažeme všechna dosavadní hodnocení tohoto řešení
qs = m . Hodnoceni . objects . filter ( reseni = reseni )
logger . info ( f " Will delete { qs . count ( ) } objects: { qs } " )
qs . delete ( )
# Vyrobíme nová podle formsetu
for form in formset :
hodnoceni = m . Hodnoceni (
reseni = reseni ,
* * form . cleaned_data ,
)
logger . info ( f " Creating Hodnoceni: { hodnoceni } " )
hodnoceni . save ( )
if poznamka_form . has_changed ( ) : poznamka_form . save ( )
if hodnoceni_formset . has_changed ( ) : hodnoceni_formset . save ( )
return redirect ( success_url )