Pavel "LEdoian" Turinsky
4 years ago
3 changed files with 53 additions and 4 deletions
@ -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…
Reference in new issue