2022-10-01 11:39:59 +02:00
# 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 ( ) :
2022-10-12 14:29:35 +02:00
if h . cislo_body is not None and h . cislo_body . rocnik . rocnik < 26 :
2022-10-11 11:00:53 +02:00
# Deadline připravený v minulé migraci
h . deadline_body = h . cislo_body . deadline_v_cisle . get ( )
h . save ( )
continue
2022-10-01 14:01:29 +02:00
p = h . problem
2022-10-12 14:27:44 +02:00
if p . polymorphic_ctype . model == ' tema ' :
2022-10-01 14:11:29 +02:00
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 ( )
2022-10-01 14:22:12 +02:00
if d is not None :
h . deadline_body = d
h . save ( )
continue
2022-10-01 14:01:29 +02:00
2022-10-01 14:11:29 +02:00
cislo = None
2022-10-12 14:27:44 +02:00
if p . polymorphic_ctype . model == ' uloha ' :
2022-10-01 14:11:29 +02:00
u = p . uloha
cislo = u . cislo_zadani
2022-10-12 14:27:44 +02:00
if p . polymorphic_ctype . model == ' clanek ' :
2022-10-01 14:01:29 +02:00
c = p . clanek
if c . cislo is not None :
2022-10-01 14:11:29 +02:00
cislo = c . cislo
2022-10-01 14:01:29 +02:00
2022-10-01 14:11:29 +02:00
if cislo is None :
2022-10-13 16:45:44 +02:00
log . warning ( f " Číslo hodnocení { h . id } se nepodařilo určit exaktním způsobem. Dané hodnocení házím do jeho cislo_body: { h . cislo_body } . " )
2022-10-01 14:11:29 +02:00
cislo = h . cislo_body
2022-10-01 14:01:29 +02:00
2022-10-01 14:11:29 +02:00
if cislo is not None :
d = Deadline . objects . filter ( cislo = cislo , deadline__gte = h . reseni . cas_doruceni ) . first ( )
2022-10-01 14:01:29 +02:00
if d is None :
2022-10-01 14:11:29 +02:00
d = Deadline . objects . filter ( cislo = cislo ) . last ( )
2022-10-01 14:22:12 +02:00
if d is not None :
h . deadline_body = d
h . save ( )
continue
2022-10-01 14:01:29 +02:00
2022-10-01 11:39:59 +02:00
d = Deadline . objects . filter ( deadline__gte = h . reseni . cas_doruceni ) . first ( )
h . deadline_body = d
h . save ( )
2022-10-13 15:58:16 +02:00
log . warning ( f " Deadline hodnocení { h . id } se nepodařil určit exaktnějším způsobem. Zkouším další. Přiřazen { h . deadline_body } . Původní cislo_body: { h . cislo_body } . " )
2022-10-12 14:27:44 +02:00
# Zběžná kontrola. Předpokládá, že M&M má méně než 10 čísel v ročníku
# a že první znak pořadí je int určující dané pořadí (schroustání 7-8).
2022-10-13 15:56:07 +02:00
if h . cislo_body and h . deadline_body and (
2022-10-12 03:09:12 +02:00
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 ] )
) :
2022-10-01 11:39:59 +02:00
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 ) ,
]