From 303c8e4b4b10c23991ee1c53096ce3bd405d3e93 Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Fri, 4 Sep 2020 14:27:35 +0200 Subject: [PATCH] =?UTF-8?q?Oprava=20vytvo=C5=99en=C3=AD=20polymorfick?= =?UTF-8?q?=C3=BDch=20contenttypes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0065_treenode_polymorphic_ctype.py | 4 +- .../0066_problem_polymorphic_ctype.py | 4 +- seminar/migrations/0087_fix_polymorphism.py | 49 +++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 seminar/migrations/0087_fix_polymorphism.py diff --git a/seminar/migrations/0065_treenode_polymorphic_ctype.py b/seminar/migrations/0065_treenode_polymorphic_ctype.py index 71eef262..cb65d8f1 100644 --- a/seminar/migrations/0065_treenode_polymorphic_ctype.py +++ b/seminar/migrations/0065_treenode_polymorphic_ctype.py @@ -5,7 +5,7 @@ import django.db.models.deletion def vyrob_treenodum_ctypes(apps, schema_editor): # Kód zkopírovaný z dokumentace: https://django-polymorphic.readthedocs.io/en/stable/migrating.html - # XXX: Nevím, jestli se tohle náhodou nemělo spustit na všech childech (jen/i) + # NOTE: Tahle migrace je špatně, 0087 ji opravuje. Možno squashnout pryč. TreeNode = apps.get_model('seminar', 'TreeNode') ContentType = apps.get_model('contenttypes', 'ContentType') @@ -27,5 +27,5 @@ class Migration(migrations.Migration): name='polymorphic_ctype', field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='polymorphic_seminar.treenode_set+', to='contenttypes.ContentType'), ), - migrations.RunPython(vyrob_treenodum_ctypes, migrations.RunPython.noop), + migrations.RunPython(vyrob_treenodum_ctypes, migrations.RunPython.noop, elidable=True), ] diff --git a/seminar/migrations/0066_problem_polymorphic_ctype.py b/seminar/migrations/0066_problem_polymorphic_ctype.py index f956217e..3aab4605 100644 --- a/seminar/migrations/0066_problem_polymorphic_ctype.py +++ b/seminar/migrations/0066_problem_polymorphic_ctype.py @@ -5,7 +5,7 @@ import django.db.models.deletion def vyrob_problemum_ctypes(apps, schema_editor): # Kód zkopírovaný z dokumentace: https://django-polymorphic.readthedocs.io/en/stable/migrating.html - # XXX: Nevím, jestli se tohle náhodou nemělo spustit na všech childech (jen/i) + # NOTE: Tahle migrace je špatně, 0087 ji opravuje. Možno squashnout pryč. Problem = apps.get_model('seminar', 'Problem') ContentType = apps.get_model('contenttypes', 'ContentType') @@ -25,5 +25,5 @@ class Migration(migrations.Migration): name='polymorphic_ctype', field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='polymorphic_seminar.problem_set+', to='contenttypes.ContentType'), ), - migrations.RunPython(vyrob_problemum_ctypes, migrations.RunPython.noop), + migrations.RunPython(vyrob_problemum_ctypes, migrations.RunPython.noop, elidable=True), ] diff --git a/seminar/migrations/0087_fix_polymorphism.py b/seminar/migrations/0087_fix_polymorphism.py new file mode 100644 index 00000000..d38877ba --- /dev/null +++ b/seminar/migrations/0087_fix_polymorphism.py @@ -0,0 +1,49 @@ +# Generated by Django 2.2.16 on 2020-09-04 12:06 + +from django.db import migrations +from logging import getLogger + +log = getLogger(__name__) + +# Oprava migrací 0065 a 0066, kde jsem špatně pochopil django-polymorphic + +# Pomocná funkce -- děláme to samé pro obě polymorfní hierarchie +def fix_ctypes(parent: str, children, apps, schema_editor): + Parent = apps.get_model('seminar', parent) + ContentType = apps.get_model('contenttypes', 'ContentType') + + # Nejdřív všechno smažeme: + Parent.objects.update(polymorphic_ctype=None) + + # Opravíme děti + for clsname in children: + Model = apps.get_model('seminar', clsname) + ct = ContentType.objects.get_for_model(Model) + Model.objects.update(polymorphic_ctype=ct) + + + # Ostatní instance mají mít explicitně content type pro rodiče + new_ct = ContentType.objects.get_for_model(Parent) + for obj in Parent.objects.filter(polymorphic_ctype__isnull=True): + log.warn(f"{parent} {obj} neměl content type -- nejspíš to je instance přímo {parent}!") + obj.polymorphic_ctype=new_ct + obj.save() + +def fix_treenode(apps, schema_editor): + children = ['RocnikNode', 'CisloNode', 'MezicisloNode', 'TemaVCisleNode', + 'OrgTextNode', 'UlohaZadaniNode', 'UlohaVzorakNode', 'PohadkaNode', + 'TextNode', 'CastNode', 'ReseniNode'] + fix_ctypes("TreeNode", children, apps, schema_editor) + +def fix_problem(apps, schema_editor): + children = ['Tema', 'Clanek', 'Uloha', 'Konfera'] + fix_ctypes("Problem", children, apps, schema_editor) + +class Migration(migrations.Migration): + dependencies = [ + ('seminar', '0086_auto_20200819_0959'), + ] + operations = [ + migrations.RunPython(fix_treenode, migrations.RunPython.noop), + migrations.RunPython(fix_problem, migrations.RunPython.noop), + ]