From 417932bf9b6e1cb5a2d1b57516163dc3052b348c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Mon, 9 Jan 2023 15:11:50 +0100 Subject: [PATCH] data: add a data migration for old card cells --- combo/data/migrations/0063_old_card_cells.py | 36 ++++++ combo/data/models.py | 10 -- tests/test_migrations.py | 124 +++++++++++++++++++ tests/wcs/test_card.py | 86 +------------ 4 files changed, 161 insertions(+), 95 deletions(-) create mode 100644 combo/data/migrations/0063_old_card_cells.py create mode 100644 tests/test_migrations.py diff --git a/combo/data/migrations/0063_old_card_cells.py b/combo/data/migrations/0063_old_card_cells.py new file mode 100644 index 00000000..00706a6d --- /dev/null +++ b/combo/data/migrations/0063_old_card_cells.py @@ -0,0 +1,36 @@ +from django.db import migrations + + +def forward(apps, schema_editor): + PageSnapshot = apps.get_model('data', 'PageSnapshot') + for snapshot in PageSnapshot.objects.all(): + changed = False + for cell in snapshot.serialization.get('cells') or []: + if cell.get('model') not in ['wcs.wcscardinfoscell', 'wcs.wcscardscell']: + continue + if cell['model'] == 'wcs.wcscardinfoscell': + cell['model'] = 'wcs.wcscardcell' + elif cell['model'] == 'wcs.wcscardscell': + cell['model'] = 'wcs.wcscardcell' + cell['fields']['card_ids'] = '' + cell['fields']['related_card_path'] = '__all__' + cell['fields']['display_mode'] = 'table' + cell['fields']['title_type'] = 'auto' + cell['fields']['custom_schema'] = {} + if cell['fields'].get('custom_title'): + cell['fields']['title_type'] = 'manual' + changed = True + if changed: + snapshot.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('data', '0062_page_uuid'), + ('wcs', '0053_new_card_cell'), + ] + + operations = [ + migrations.RunPython(forward, reverse_code=migrations.RunPython.noop), + ] diff --git a/combo/data/models.py b/combo/data/models.py index 964aa2ee..d77cda43 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -627,16 +627,6 @@ class Page(models.Model): def load_serialized_cells(cls, cells): # load new cells for cell_data in cells: - # legacy card cells, for snapshots - if cell_data['model'] == 'wcs.wcscardinfoscell': - cell_data['model'] = 'wcs.wcscardcell' - elif cell_data['model'] == 'wcs.wcscardscell': - cell_data['model'] = 'wcs.wcscardcell' - cell_data['fields']['related_card_path'] = '__all__' - cell_data['fields']['display_mode'] = 'table' - cell_data['fields']['title_type'] = 'auto' - if cell_data['fields'].get('custom_title'): - cell_data['fields']['title_type'] = 'manual' model = apps.get_model(cell_data['model']) cell_data = model.prepare_serialized_data(cell_data) cell = list(serializers.deserialize('json', json.dumps([cell_data]), ignorenonexistent=True))[0] diff --git a/tests/test_migrations.py b/tests/test_migrations.py new file mode 100644 index 00000000..2bad3a1a --- /dev/null +++ b/tests/test_migrations.py @@ -0,0 +1,124 @@ +from django.db import connection +from django.db.migrations.executor import MigrationExecutor + + +def test_page_snapshot_with_old_card_cells_migration(transactional_db): + migrate_from = [('data', '0062_page_uuid')] + migrate_to = [('data', '0063_old_card_cells')] + + executor = MigrationExecutor(connection) + old_apps = executor.loader.project_state(migrate_from).apps + executor.migrate(migrate_from) + + pagesnapshot_class = old_apps.get_model('data', 'PageSnapshot') + + snapshot = pagesnapshot_class.objects.create( + serialization={ + 'cells': [ + { + 'model': 'wcs.wcscardinfoscell', + 'fields': { + 'card_ids': '42,35', + 'title_type': 'manual', + 'custom_title': 'my-title', + 'display_mode': 'card', + 'without_user': True, + 'custom_schema': {}, + 'only_for_user': True, + 'carddef_reference': 'default:card_model_1', + 'related_card_path': '', + 'limit': 42, + 'slug': 'my-card', + 'order': 1, + 'groups': [], + 'public': True, + 'condition': 'my-condition', + 'placeholder': 'content', + 'template_name': None, + 'extra_css_class': '', + 'last_update_timestamp': '2022-08-11T13:57:43.362Z', + 'restricted_to_unlogged': False, + }, + }, + { + 'model': 'wcs.wcscardscell', + 'fields': { + 'custom_title': 'my-other-title', + 'without_user': False, + 'only_for_user': False, + 'carddef_reference': 'default:card_model_1', + 'limit': 35, + 'slug': 'my-other-card', + 'order': 2, + 'groups': [], + 'public': True, + 'condition': '', + 'placeholder': 'content', + 'template_name': None, + 'extra_css_class': '', + 'last_update_timestamp': '2022-08-12T07:19:18.541Z', + 'restricted_to_unlogged': False, + }, + }, + ], + } + ) + + executor = MigrationExecutor(connection) + executor.migrate(migrate_to) + executor.loader.build_graph() + + apps = executor.loader.project_state(migrate_to).apps + pagesnapshot_class = apps.get_model('data', 'PageSnapshot') + + snapshot = pagesnapshot_class.objects.get() + assert snapshot.serialization['cells'][0] == { + 'model': 'wcs.wcscardcell', + 'fields': { + 'card_ids': '42,35', + 'title_type': 'manual', + 'custom_title': 'my-title', + 'display_mode': 'card', + 'without_user': True, + 'custom_schema': {}, + 'only_for_user': True, + 'carddef_reference': 'default:card_model_1', + 'related_card_path': '', + 'limit': 42, + 'slug': 'my-card', + 'order': 1, + 'groups': [], + 'public': True, + 'condition': 'my-condition', + 'placeholder': 'content', + 'template_name': None, + 'extra_css_class': '', + 'last_update_timestamp': '2022-08-11T13:57:43.362Z', + 'restricted_to_unlogged': False, + }, + } + assert snapshot.serialization['cells'][1] == { + 'model': 'wcs.wcscardcell', + 'fields': { + 'card_ids': '', + 'title_type': 'manual', + 'custom_title': 'my-other-title', + 'display_mode': 'table', + 'without_user': False, + 'custom_schema': {}, + 'only_for_user': False, + 'carddef_reference': 'default:card_model_1', + 'related_card_path': '__all__', + 'limit': 35, + 'slug': 'my-other-card', + 'order': 2, + 'groups': [], + 'public': True, + 'condition': '', + 'placeholder': 'content', + 'template_name': None, + 'extra_css_class': '', + 'last_update_timestamp': '2022-08-12T07:19:18.541Z', + 'restricted_to_unlogged': False, + }, + } diff --git a/tests/wcs/test_card.py b/tests/wcs/test_card.py index 10de7a0a..19041f47 100644 --- a/tests/wcs/test_card.py +++ b/tests/wcs/test_card.py @@ -17,7 +17,7 @@ from requests.models import Response from combo.apps.wcs.forms import WcsCardCellDisplayForm from combo.apps.wcs.models import WcsCardCell -from combo.data.models import Page, PageSnapshot, TextCell, ValidityInfo +from combo.data.models import Page, TextCell, ValidityInfo from combo.data.utils import import_site from tests.test_manager import login from tests.utils import manager_submit_cell @@ -3264,90 +3264,6 @@ def test_card_cell_assets(mock_send, settings, app, admin_user): assert 'Logo — %s' % cell2.get_label_for_asset() in resp.text -def test_page_snapshot_with_old_card_cells(): - page = Page.objects.create(title='xxx', slug='test_snapshots', template_name='standard') - PageSnapshot.take(page) - snapshot = page.pagesnapshot_set.first() - snapshot.serialization['cells'].append( - { - 'model': 'wcs.wcscardinfoscell', - 'fields': { - 'slug': 'my-card', - 'limit': 42, - 'order': 1, - 'groups': [], - 'public': True, - 'card_ids': '42,35', - 'condition': 'my-condition', - 'title_type': 'manual', - 'placeholder': 'content', - 'custom_title': 'my-title', - 'display_mode': 'card', - 'without_user': True, - 'custom_schema': {}, - 'only_for_user': True, - 'template_name': None, - 'extra_css_class': '', - 'carddef_reference': 'default:card_model_1', - 'related_card_path': '', - 'last_update_timestamp': '2022-08-11T13:57:43.362Z', - 'restricted_to_unlogged': False, - 'page': page.pk, - }, - } - ) - snapshot.serialization['cells'].append( - { - 'model': 'wcs.wcscardscell', - 'fields': { - 'slug': 'my-other-card', - 'limit': 35, - 'order': 2, - 'groups': [], - 'public': True, - 'condition': '', - 'placeholder': 'content', - 'custom_title': 'my-other-title', - 'without_user': False, - 'only_for_user': False, - 'template_name': None, - 'extra_css_class': '', - 'carddef_reference': 'default:card_model_1', - 'last_update_timestamp': '2022-08-12T07:19:18.541Z', - 'restricted_to_unlogged': False, - 'page': page.pk, - }, - } - ) - old_page = snapshot.get_page() - cell1 = old_page.get_cells()[0] - assert isinstance(cell1, WcsCardCell) - assert cell1.slug == 'my-card' - assert cell1.limit == 42 - assert cell1.card_ids == '42,35' - assert cell1.title_type == 'manual' - assert cell1.custom_title == 'my-title' - assert cell1.display_mode == 'card' - assert cell1.without_user is True - assert cell1.custom_schema == {} - assert cell1.only_for_user is True - assert cell1.carddef_reference == 'default:card_model_1' - assert cell1.related_card_path == '' - cell2 = old_page.get_cells()[1] - assert isinstance(cell1, WcsCardCell) - assert cell2.slug == 'my-other-card' - assert cell2.limit == 35 - assert cell2.card_ids == '' - assert cell2.title_type == 'manual' - assert cell2.custom_title == 'my-other-title' - assert cell2.display_mode == 'table' - assert cell2.without_user is False - assert cell2.custom_schema == {} - assert cell2.only_for_user is False - assert cell2.carddef_reference == 'default:card_model_1' - assert cell2.related_card_path == '__all__' - - def get_output_of_command(command, *args, **kwargs): old_stdout = sys.stdout output = sys.stdout = StringIO()