# Generated by Django 3.2.15 on 2022-10-01 09:28
from django . db import migrations , models
import django . db . models . deletion
from logging import getLogger
log = getLogger ( __name__ )
def prirad_deadliny ( apps , schema_editor ) :
Hodnoceni = apps . get_model ( ' seminar ' , ' Hodnoceni ' )
Deadline = apps . get_model ( ' seminar ' , ' Deadline ' )
for h in Hodnoceni . objects . all ( ) :
if h . cislo_body . rocnik . rocnik < 26 :
# Deadline připravený v minulé migraci
h . deadline_body = h . cislo_body . deadline_v_cisle . get ( )
h . save ( )
continue
p = h . problem
try :
t = p . tema
d = Deadline . objects . filter ( cislo__rocnik = t . rocnik , deadline__gte = h . reseni . cas_doruceni ) . first ( )
if d is None :
d = Deadline . objects . filter ( cislo__rocnik = t . rocnik ) . last ( )
if d is not None :
h . deadline_body = d
h . save ( )
continue
except Exception :
pass
cislo = None
try :
u = p . uloha
cislo = u . cislo_zadani
except Exception :
pass
try :
c = p . clanek
if c . cislo is not None :
cislo = c . cislo
except Exception :
pass
if cislo is None :
cislo = h . cislo_body
if cislo is not None :
d = Deadline . objects . filter ( cislo = cislo , deadline__gte = h . reseni . cas_doruceni ) . first ( )
if d is None :
d = Deadline . objects . filter ( cislo = cislo ) . last ( )
if d is not None :
h . deadline_body = d
h . save ( )
continue
log . warning ( f " Deadline hodnocení { h . id } se nepodařil určit exaktnějším způsobem. Zkouším další. " )
d = Deadline . objects . filter ( deadline__gte = h . reseni . cas_doruceni ) . first ( )
h . deadline_body = d
h . save ( )
if h . cislo_body and ( int ( h . deadline_body . cislo . poradi [ 0 ] ) + 2 < int ( h . cislo_body . poradi [ 0 ] ) or int ( h . deadline_body . cislo . poradi [ 0 ] ) > int ( h . cislo_body . poradi [ 0 ] ) ) :
log . error ( f " Hodnocení { h . id } se špatně změnilo číslo z { h . cislo_body } na { h . deadline_body . cislo } " )
class Migration ( migrations . Migration ) :
dependencies = [
( ' seminar ' , ' 0103_deadline ' ) ,
]
operations = [
migrations . AddField (
model_name = ' hodnoceni ' ,
name = ' deadline_body ' ,
field = models . ForeignKey ( blank = True , null = True , on_delete = django . db . models . deletion . PROTECT , related_name = ' hodnoceni ' , to = ' seminar.deadline ' , verbose_name = ' deadline pro body ' ) ,
) ,
migrations . RunPython ( prirad_deadliny , migrations . RunPython . noop ) ,
]