@ -221,9 +221,8 @@ class DetailReseniView(DetailView):
def get_context_data ( self , * * kw ) :
def get_context_data ( self , * * kw ) :
reseni = get_object_or_404 ( m . Reseni , id = self . kwargs [ ' pk ' ] )
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 = 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 )
ctx [ ' poznamka_form ' ] = f . PoznamkaReseniForm ( instance = reseni )
return ctx
return ctx
@ -231,34 +230,18 @@ class DetailReseniView(DetailView):
def hodnoceniReseniView ( request , pk , * args , * * kwargs ) :
def hodnoceniReseniView ( request , pk , * args , * * kwargs ) :
reseni = get_object_or_404 ( m . Reseni , pk = pk )
reseni = get_object_or_404 ( m . Reseni , pk = pk )
template_name = ' odevzdavatko/detail.html '
template_name = ' odevzdavatko/detail.html '
form_class = f . OhodnoceniRes eniFormSet
form_class = f . Hodnoc eniFormSet
success_url = reverse ( ' odevzdavatko_detail_reseni ' , kwargs = { ' pk ' : pk } )
success_url = reverse ( ' odevzdavatko_detail_reseni ' , kwargs = { ' pk ' : pk } )
# FIXME: Použit initial i tady a nebastlit hodnocení tak nízkoúrovňově
hodnoceni_formset = f . HodnoceniFormSet ( request . POST , queryset = m . Hodnoceni . objects . filter ( reseni = reseni ) )
# Also: https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/#django.forms.ModelForm
formset = f . OhodnoceniReseniFormSet ( request . POST )
poznamka_form = f . PoznamkaReseniForm ( request . POST , instance = reseni )
poznamka_form = f . PoznamkaReseniForm ( request . POST , instance = reseni )
# TODO: Napsat validaci formuláře a formsetu
# TODO: Napsat validaci formuláře a formsetu
if not ( formset . is_valid ( ) and poznamka_form . is_valid ( ) ) :
if not ( hodnoceni_ formset. is_valid ( ) and poznamka_form . is_valid ( ) ) :
raise ValueError ( formset . errors , poznamka_form . errors )
raise ValueError ( hodnoceni_ formset. errors , poznamka_form . errors )
with transaction . atomic ( ) :
with transaction . atomic ( ) :
# Poznámka je jednoduchá na zpracování:
if poznamka_form . has_changed ( ) : poznamka_form . save ( )
poznamka_form . save ( )
if hodnoceni_formset . has_changed ( ) : hodnoceni_formset . 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 ( )
return redirect ( success_url )
return redirect ( success_url )