@ -1,7 +1,7 @@
from django . core . exceptions import PermissionDenied
from django . core . exceptions import PermissionDenied
from django . views . generic import ListView , DetailView , FormView
from django . views . generic import ListView , DetailView , FormView
from django . contrib . auth . mixins import LoginRequiredMixin
from django . contrib . auth . mixins import LoginRequiredMixin
from django . core . mail import send_mail
from django . core . mail import send_mail , EmailMessage
from django . utils import timezone
from django . utils import timezone
from django . views . generic import ListView , DetailView , FormView , CreateView
from django . views . generic import ListView , DetailView , FormView , CreateView
from django . views . generic . list import MultipleObjectTemplateResponseMixin , MultipleObjectMixin
from django . views . generic . list import MultipleObjectTemplateResponseMixin , MultipleObjectMixin
@ -219,58 +219,46 @@ class DetailReseniView(DetailView):
model = m . Reseni
model = m . Reseni
template_name = ' odevzdavatko/detail.html '
template_name = ' odevzdavatko/detail.html '
def aktualni_hodnoceni ( self ) :
self . reseni = get_object_or_404 ( m . Reseni , id = self . kwargs [ ' pk ' ] )
result = [ ] # Slovníky s klíči problem, body, deadline_body -- initial data pro f.OhodnoceniReseniFormSet
for hodn in m . Hodnoceni . objects . filter ( reseni = self . reseni ) :
result . append ( {
" problem " : hodn . problem ,
" body " : hodn . body ,
" deadline_body " : hodn . deadline_body ,
" feedback " : hodn . feedback ,
} )
return result
def get_context_data ( self , * * kw ) :
def get_context_data ( self , * * kw ) :
reseni = get_object_or_404 ( m . Reseni , id = self . kwargs [ ' pk ' ] )
ctx = super ( ) . get_context_data ( * * kw )
ctx = super ( ) . get_context_data ( * * kw )
ctx [ ' form ' ] = f . OhodnoceniReseniFormSet (
ctx [ ' form ' ] = f . HodnoceniFormSet ( queryset = m . Hodnoceni . objects . filter ( reseni = reseni ) )
initial = self . aktualni_hodnoceni ( )
ctx [ ' poznamka_form ' ] = f . PoznamkaReseniForm ( instance = reseni )
)
ctx [ ' poznamka_form ' ] = f . PoznamkaReseniForm ( instance = self . reseni )
return ctx
return ctx
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 . HodnoceniFormSet
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í
zmeny = [ ]
qs = m . Hodnoceni . objects . filter ( reseni = reseni )
for hodnoceni_form in hodnoceni_formset :
logger . info ( f " Will delete { qs . count ( ) } objects: { qs } " )
if ' feedback ' in hodnoceni_form . changed_data :
qs . delete ( )
zmeny . append ( hodnoceni_form . instance )
# Vyrobíme nová podle formsetu
if zmeny :
for form in formset :
adresati = reseni . resitele . filter ( upozorneni = True ) . values_list ( ' osoba__email ' , flat = True )
hodnoceni = m . Hodnoceni (
if adresati :
reseni = reseni ,
email = EmailMessage (
* * form . cleaned_data ,
subject = ' Změna zpětné vazby k Tvému řešení ' ,
)
body = ' \n \n ' . join ( [ f ' { hodnoceni . problem } \n { hodnoceni . feedback } ' for hodnoceni in zmeny ] ) + f ' \n \n Nechceš-li toto upozornění dostávat, můžeš jej vypnout v nastavení svého účtu. \n \n Tvoji organizátoři M&M ' ,
logger . info ( f " Creating Hodnoceni: { hodnoceni } " )
from_email = ' odevzdavatko@mam.mff.cuni.cz ' ,
hodnoceni . save ( )
to = adresati ,
reply_to = [ request . user . email ] or None ,
)
email . send ( )
return redirect ( success_url )
return redirect ( success_url )