Browse Source

Oprava vytvoření polymorfických contenttypes

middleware_test
Pavel "LEdoian" Turinsky 4 years ago
parent
commit
303c8e4b4b
  1. 4
      seminar/migrations/0065_treenode_polymorphic_ctype.py
  2. 4
      seminar/migrations/0066_problem_polymorphic_ctype.py
  3. 49
      seminar/migrations/0087_fix_polymorphism.py

4
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),
]

4
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),
]

49
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),
]
Loading…
Cancel
Save