Web M&M
https://mam.matfyz.cz
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
2.1 KiB
56 lines
2.1 KiB
# Generated by Django 2.2.13 on 2020-06-17 19:06
|
|
|
|
from django.db import migrations, models
|
|
import django.db.models.deletion
|
|
from treenode.treelib import get_parent
|
|
|
|
import logging
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def najdi_cislo(apps, schema_editor):
|
|
Clanek = apps.get_model('seminar', 'Clanek')
|
|
Hodnoceni = apps.get_model('seminar', 'Hodnoceni')
|
|
Reseni = apps.get_model('seminar', 'Reseni')
|
|
ReseniNode = apps.get_model('seminar', 'ReseniNode')
|
|
CisloNode = apps.get_model('seminar', 'CisloNode')
|
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
|
|
|
for c in Clanek.objects.all():
|
|
reseni = c.reseni_set
|
|
if (reseni.count() != 1): # Pozor, reseni_set je Manager, takže se na něj musí trošku jinak
|
|
logger.warn(f"Více než jedno řešení pro článek {c}")
|
|
c.cislo = None
|
|
c.save()
|
|
continue
|
|
r = reseni.first()
|
|
aktualniNode = r.text_cely # Hlavní ReseniNode pro řešení
|
|
while aktualniNode is not None:
|
|
#if isinstance(aktualniNode, CisloNode): # Nejde, protože aktualniNode se tváří jako obecný TreeNode...
|
|
# Ale v dokumentaci k django-polymorphic se píše, že tam jsou nastavené nějaké fieldy na známé hodnoty :-)
|
|
# https://django-polymorphic.readthedocs.io/en/stable/migrating.html
|
|
cislonode_ct = ContentType.objects.get_for_model(CisloNode)
|
|
akt_ct = aktualniNode.polymorphic_ctype
|
|
if akt_ct == cislonode_ct:
|
|
# Zneužíváme tu opačnou vazbu k treenode_ptr, protože
|
|
# aktualniNode je "jen" TreeNode a ne CisloNode
|
|
c.cislo = aktualniNode.cislonode.cislo
|
|
c.save()
|
|
break
|
|
aktualniNode = get_parent(aktualniNode)
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('seminar', '0083_auto_20200506_1952'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.AddField(
|
|
model_name='clanek',
|
|
name='cislo',
|
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='vydane_clanky', to='seminar.Cislo', verbose_name='číslo vydání'),
|
|
),
|
|
migrations.RunPython(najdi_cislo, migrations.RunPython.noop),
|
|
]
|
|
|