data: don't load all cells for cache build (#51472)

This commit is contained in:
Lauréline Guérin 2021-02-26 14:49:38 +01:00
parent 0f6f0178a5
commit d1fe947574
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 16 additions and 11 deletions

View File

@ -212,7 +212,7 @@ class Page(models.Model):
'PageSnapshot', on_delete=models.CASCADE, null=True, related_name='temporary_page'
)
# keep a cached list of cell types that are used in the pages.
# keep a cached list of cell types that are used in the page.
related_cells = JSONField(blank=True)
_level = None
@ -427,13 +427,16 @@ class Page(models.Model):
return CellBase.get_cells(page=self)
def build_cell_cache(self):
cells = CellBase.get_cells(page=self, skip_cell_cache=True)
cell_classes = get_cell_classes()
cell_types = set()
for cell in cells:
cell_types.add(cell.get_cell_type_str())
for klass in cell_classes:
if klass is None:
continue
if klass.objects.filter(page=self).exists():
cell_types.add(klass.get_cell_type_str())
if cell_types != set(self.related_cells.get('cell_types', [])):
self.related_cells['cell_types'] = list(cell_types)
self.save()
self.save(update_fields=['related_cells', 'last_update_timestamp'])
def get_serialized_page(self):
cells = [x for x in self.get_cells() if x.placeholder and not x.placeholder.startswith('_')]
@ -496,8 +499,10 @@ class Page(models.Model):
cell['fields']['page'] = page.object.natural_key()
# if there were cells, remove them
for cell in CellBase.get_cells(page_id=page.object.id):
cell.delete()
# if page was created, do nothing
if created is False:
for cell in CellBase.get_cells(page_id=page.object.id):
cell.delete()
return page.object # get page out of deserialization object
@classmethod

View File

@ -832,7 +832,7 @@ def test_site_export_import_json(app, admin_user):
resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json')
with CaptureQueriesContext(connection) as ctx:
resp = resp.form.submit()
assert len(ctx.captured_queries) in [1162, 1163]
assert len(ctx.captured_queries) in [1142, 1143]
Page.objects.all().delete()
assert LinkCell.objects.count() == 0
@ -841,7 +841,7 @@ def test_site_export_import_json(app, admin_user):
resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json')
with CaptureQueriesContext(connection) as ctx:
resp = resp.form.submit()
assert len(ctx.captured_queries) == 849
assert len(ctx.captured_queries) == 667
assert set(Page.objects.get(slug='one').related_cells['cell_types']) == set(
['data_textcell', 'data_linkcell']
)
@ -2128,7 +2128,7 @@ def test_page_versionning(app, admin_user):
with CaptureQueriesContext(connection) as ctx:
resp2 = resp.click('view', index=1)
assert len(ctx.captured_queries) == 365
assert len(ctx.captured_queries) == 319
assert Page.snapshots.latest('pk').related_cells == {'cell_types': ['data_textcell']}
assert resp2.text.index('Hello world') < resp2.text.index('Foobar3')
@ -2189,7 +2189,7 @@ def test_page_versionning(app, admin_user):
resp = resp.click('restore', index=6)
with CaptureQueriesContext(connection) as ctx:
resp = resp.form.submit().follow()
assert len(ctx.captured_queries) == 587
assert len(ctx.captured_queries) == 575
resp2 = resp.click('See online')
assert resp2.text.index('Foobar1') < resp2.text.index('Foobar2') < resp2.text.index('Foobar3')