wcs: import/export with link to page with sub_slug (#68534)
This commit is contained in:
parent
04165ad13c
commit
d051059544
|
@ -925,6 +925,30 @@ class WcsCardCell(CardMixin, CellBase):
|
|||
return 'combo/wcs/cards.html'
|
||||
return 'combo/wcs/card.html'
|
||||
|
||||
def get_serialized_cell(self):
|
||||
serialized_cell = super().get_serialized_cell()
|
||||
custom_schema = serialized_cell['fields']['custom_schema']
|
||||
for cell in custom_schema.get('cells') or []:
|
||||
if cell.get('page') not in ['', None]:
|
||||
try:
|
||||
page = Page.objects.get(pk=cell['page'])
|
||||
cell['page'] = page.natural_key()[0]
|
||||
except Page.DoesNotExist:
|
||||
cell['page'] = ''
|
||||
return serialized_cell
|
||||
|
||||
@classmethod
|
||||
def prepare_serialized_data(cls, cell_data):
|
||||
if not cell_data['fields'].get('custom_schema'):
|
||||
return cell_data
|
||||
custom_schema = cell_data['fields']['custom_schema']
|
||||
for cell in custom_schema.get('cells') or []:
|
||||
if cell.get('page'):
|
||||
page_slug = cell['page'].strip('/').split('/')[-1]
|
||||
cell['page'] = Page.get_page_ids_by_slugs().get(page_slug) or ''
|
||||
|
||||
return cell_data
|
||||
|
||||
@property
|
||||
def global_context_key(self):
|
||||
return '%s-card-ids' % self.get_reference()
|
||||
|
|
|
@ -263,6 +263,11 @@ class Page(models.Model):
|
|||
def natural_key(self):
|
||||
return (self.get_online_url(follow_redirection=False).strip('/'),)
|
||||
|
||||
@classmethod
|
||||
@utils.cache_during_request
|
||||
def get_page_ids_by_slugs(cls):
|
||||
return {page.slug: page.pk for page in cls.objects.only('pk', 'slug')}
|
||||
|
||||
def picture_extension(self):
|
||||
if not self.picture:
|
||||
return None
|
||||
|
@ -548,11 +553,7 @@ class Page(models.Model):
|
|||
del serialized_page['snapshot']
|
||||
if 'related_cells' in serialized_page['fields']:
|
||||
del serialized_page['fields']['related_cells']
|
||||
serialized_page['cells'] = json.loads(
|
||||
serializers.serialize('json', cells, use_natural_foreign_keys=True, use_natural_primary_keys=True)
|
||||
)
|
||||
for index, cell in enumerate(cells):
|
||||
serialized_page['cells'][index].update(cell.export_subobjects())
|
||||
serialized_page['cells'] = CellBase.get_serialized_cells(cells)
|
||||
serialized_page['fields']['groups'] = [x[0] for x in serialized_page['fields']['groups']]
|
||||
for cell in serialized_page['cells']:
|
||||
del cell['pk']
|
||||
|
@ -1437,6 +1438,19 @@ class CellBase(models.Model, metaclass=CellMeta):
|
|||
def get_external_links_data(self):
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
def get_serialized_cells(cls, cells):
|
||||
return [c.get_serialized_cell() for c in cells]
|
||||
|
||||
def get_serialized_cell(self):
|
||||
serialized_cell = json.loads(
|
||||
serializers.serialize(
|
||||
'json', [self], use_natural_foreign_keys=True, use_natural_primary_keys=True
|
||||
)
|
||||
)[0]
|
||||
serialized_cell.update(self.export_subobjects())
|
||||
return serialized_cell
|
||||
|
||||
@classmethod
|
||||
def prepare_serialized_data(cls, cell_data):
|
||||
return cell_data
|
||||
|
@ -1664,16 +1678,11 @@ class LinkCell(CellBase):
|
|||
self.check_validity()
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
@utils.cache_during_request
|
||||
def get_page_slugs():
|
||||
return {page.slug for page in Page.objects.all()}
|
||||
|
||||
@classmethod
|
||||
def prepare_serialized_data(cls, cell_data):
|
||||
if cell_data['fields'].get('link_page'):
|
||||
link_page_slug = cell_data['fields']['link_page'][0].strip('/').split('/')[-1]
|
||||
if link_page_slug not in cls.get_page_slugs():
|
||||
if link_page_slug not in Page.get_page_ids_by_slugs():
|
||||
del cell_data['fields']['link_page']
|
||||
return cell_data
|
||||
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
import json
|
||||
import re
|
||||
import sys
|
||||
from io import StringIO
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
from django.apps import apps
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.core.cache import cache
|
||||
from django.core.management import call_command
|
||||
from django.test.client import RequestFactory
|
||||
from django.urls import reverse
|
||||
from pyquery import PyQuery
|
||||
|
@ -15,6 +18,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.utils import import_site
|
||||
from tests.test_manager import login
|
||||
from tests.utils import manager_submit_cell
|
||||
|
||||
|
@ -2755,3 +2759,65 @@ def test_page_snapshot_with_old_card_cells():
|
|||
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()
|
||||
call_command(command, format_json=True, *args, **kwargs)
|
||||
sys.stdout = old_stdout
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def test_export_import_card_cell_with_page_link():
|
||||
root_page = Page.objects.create(title='Root', slug='root', template_name='standard')
|
||||
card_page = Page.objects.create(
|
||||
title='Card',
|
||||
slug='card',
|
||||
template_name='standard',
|
||||
sub_slug='card_model_1_id',
|
||||
parent=root_page,
|
||||
)
|
||||
|
||||
page = Page.objects.create(title='xxx', template_name='standard')
|
||||
WcsCardCell.objects.create(
|
||||
page=page,
|
||||
placeholder='content',
|
||||
order=0,
|
||||
carddef_reference='default:card_model_1',
|
||||
custom_schema={
|
||||
'cells': [
|
||||
{
|
||||
'varname': '@link@',
|
||||
'page': card_page.pk,
|
||||
'template': 'Foo',
|
||||
'display_mode': 'link',
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
site_export = get_output_of_command('export_site')
|
||||
site_data = json.loads(site_export)
|
||||
assert len(site_data['pages']) == 3
|
||||
assert site_data['pages'][-1]['cells'][0]['fields']['custom_schema']['cells'][0]['page'] == 'root/card'
|
||||
import_site(data={}, clean=True)
|
||||
assert Page.objects.all().count() == 0
|
||||
|
||||
import_site(data=site_data, clean=True)
|
||||
new_card_page = Page.objects.get(slug='card')
|
||||
new_cell = WcsCardCell.objects.get()
|
||||
assert new_cell.custom_schema['cells'][0]['page'] == new_card_page.pk
|
||||
|
||||
# unknown target page
|
||||
new_cell.custom_schema['cells'][0]['page'] = 0
|
||||
new_cell.save()
|
||||
site_export = get_output_of_command('export_site')
|
||||
site_data = json.loads(site_export)
|
||||
assert site_data['pages'][-1]['cells'][0]['fields']['custom_schema']['cells'][0]['page'] == ''
|
||||
|
||||
site_data['pages'][-1]['cells'][0]['fields']['custom_schema']['cells'][0]['page'] = 'unknown'
|
||||
import_site(data=site_data, clean=True)
|
||||
new_card_page = Page.objects.get(slug='card')
|
||||
new_cell = WcsCardCell.objects.get()
|
||||
assert new_cell.custom_schema['cells'][0]['page'] == ''
|
||||
|
|
Loading…
Reference in New Issue