103 lines
3.7 KiB
Python
103 lines
3.7 KiB
Python
import uuid
|
|
|
|
from django.db import migrations
|
|
|
|
|
|
def get_page_uuid(page_uuids_by_slugs, slug, default_index=False):
|
|
try:
|
|
uuid.UUID(slug)
|
|
except (ValueError, AttributeError):
|
|
pass
|
|
else:
|
|
# it's a uuid, don't change it
|
|
if slug in page_uuids_by_slugs.values():
|
|
return slug
|
|
|
|
slug = str(slug).strip('/').rsplit('/', maxsplit=1)[-1]
|
|
if not slug and default_index:
|
|
slug = 'index'
|
|
return page_uuids_by_slugs.get(slug)
|
|
|
|
|
|
def forward(apps, schema_editor):
|
|
PageSnapshot = apps.get_model('data', 'PageSnapshot')
|
|
Page = apps.get_model('data', 'Page')
|
|
page_uuids_by_slugs = {page.slug: str(page.uuid) for page in Page.objects.only('uuid', 'slug')}
|
|
for snapshot in PageSnapshot.objects.all():
|
|
changed = False
|
|
|
|
if (snapshot.serialization.get('fields') or {}).get('parent'):
|
|
new_uuid = get_page_uuid(
|
|
page_uuids_by_slugs, snapshot.serialization['fields']['parent'][0], default_index=True
|
|
)
|
|
if new_uuid:
|
|
new_uuid = [new_uuid]
|
|
snapshot.serialization['fields']['parent'] = new_uuid
|
|
changed = True
|
|
|
|
for cell in snapshot.serialization.get('cells') or []:
|
|
if cell.get('model') not in [
|
|
'data.linkcell',
|
|
'data.linklistcell',
|
|
'search.searchcell',
|
|
'wcs.wcscardcell',
|
|
]:
|
|
continue
|
|
|
|
if cell['model'] == 'data.linkcell':
|
|
if not cell['fields'].get('link_page'):
|
|
continue
|
|
new_uuid = get_page_uuid(page_uuids_by_slugs, cell['fields']['link_page'][0])
|
|
if new_uuid:
|
|
new_uuid = [new_uuid]
|
|
cell['fields']['link_page'] = new_uuid
|
|
changed = True
|
|
|
|
elif cell['model'] == 'data.linklistcell':
|
|
for link in cell['fields'].get('links') or []:
|
|
if link.get('model') != 'data.linkcell':
|
|
continue
|
|
if not link['fields'].get('link_page'):
|
|
continue
|
|
new_uuid = get_page_uuid(page_uuids_by_slugs, link['fields']['link_page'][0])
|
|
if new_uuid:
|
|
new_uuid = [new_uuid]
|
|
link['fields']['link_page'] = new_uuid
|
|
changed = True
|
|
|
|
elif cell['model'] == 'search.searchcell':
|
|
if not cell['fields'].get('_search_services'):
|
|
continue
|
|
if not cell['fields']['_search_services'].get('options'):
|
|
continue
|
|
for option in cell['fields']['_search_services']['options'].values():
|
|
if not option:
|
|
continue
|
|
if not option.get('target_page'):
|
|
continue
|
|
option['target_page'] = get_page_uuid(page_uuids_by_slugs, option['target_page'])
|
|
changed = True
|
|
|
|
elif cell['model'] == 'wcs.wcscardcell':
|
|
if not cell['fields'].get('custom_schema'):
|
|
continue
|
|
for custom_cell in cell['fields']['custom_schema'].get('cells') or []:
|
|
if not custom_cell.get('page'):
|
|
continue
|
|
custom_cell['page'] = get_page_uuid(page_uuids_by_slugs, custom_cell['page'])
|
|
changed = True
|
|
|
|
if changed:
|
|
snapshot.save()
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('data', '0063_old_card_cells'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(forward, reverse_code=migrations.RunPython.noop),
|
|
]
|