From 153e9cb11cc4c50441aa9e72e11487e544eefcb4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 26 Feb 2021 14:49:38 +0100
Subject: [PATCH 3/7] data: don't load all cells for cache build (#51472)

---
 combo/data/models.py  | 19 ++++++++++++-------
 tests/test_manager.py |  8 ++++----
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/combo/data/models.py b/combo/data/models.py
index 1059b237..6b1f3bb2 100644
--- a/combo/data/models.py
+++ b/combo/data/models.py
@@ -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
diff --git a/tests/test_manager.py b/tests/test_manager.py
index a7f87686..a3af2c7e 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -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')
-- 
2.30.0

