Projet

Général

Profil

0003-data-don-t-load-all-cells-for-cache-build-51472.patch

Lauréline Guérin, 01 mars 2021 11:22

Télécharger (4,19 ko)

Voir les différences:

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(-)
combo/data/models.py
212 212
        'PageSnapshot', on_delete=models.CASCADE, null=True, related_name='temporary_page'
213 213
    )
214 214

  
215
    # keep a cached list of cell types that are used in the pages.
215
    # keep a cached list of cell types that are used in the page.
216 216
    related_cells = JSONField(blank=True)
217 217

  
218 218
    _level = None
......
427 427
        return CellBase.get_cells(page=self)
428 428

  
429 429
    def build_cell_cache(self):
430
        cells = CellBase.get_cells(page=self, skip_cell_cache=True)
430
        cell_classes = get_cell_classes()
431 431
        cell_types = set()
432
        for cell in cells:
433
            cell_types.add(cell.get_cell_type_str())
432
        for klass in cell_classes:
433
            if klass is None:
434
                continue
435
            if klass.objects.filter(page=self).exists():
436
                cell_types.add(klass.get_cell_type_str())
434 437
        if cell_types != set(self.related_cells.get('cell_types', [])):
435 438
            self.related_cells['cell_types'] = list(cell_types)
436
        self.save()
439
        self.save(update_fields=['related_cells', 'last_update_timestamp'])
437 440

  
438 441
    def get_serialized_page(self):
439 442
        cells = [x for x in self.get_cells() if x.placeholder and not x.placeholder.startswith('_')]
......
496 499
                cell['fields']['page'] = page.object.natural_key()
497 500

  
498 501
        # if there were cells, remove them
499
        for cell in CellBase.get_cells(page_id=page.object.id):
500
            cell.delete()
502
        # if page was created, do nothing
503
        if created is False:
504
            for cell in CellBase.get_cells(page_id=page.object.id):
505
                cell.delete()
501 506
        return page.object  # get page out of deserialization object
502 507

  
503 508
    @classmethod
tests/test_manager.py
832 832
    resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json')
833 833
    with CaptureQueriesContext(connection) as ctx:
834 834
        resp = resp.form.submit()
835
        assert len(ctx.captured_queries) in [1162, 1163]
835
        assert len(ctx.captured_queries) in [1142, 1143]
836 836

  
837 837
    Page.objects.all().delete()
838 838
    assert LinkCell.objects.count() == 0
......
841 841
    resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json')
842 842
    with CaptureQueriesContext(connection) as ctx:
843 843
        resp = resp.form.submit()
844
        assert len(ctx.captured_queries) == 849
844
        assert len(ctx.captured_queries) == 667
845 845
    assert set(Page.objects.get(slug='one').related_cells['cell_types']) == set(
846 846
        ['data_textcell', 'data_linkcell']
847 847
    )
......
2128 2128

  
2129 2129
    with CaptureQueriesContext(connection) as ctx:
2130 2130
        resp2 = resp.click('view', index=1)
2131
        assert len(ctx.captured_queries) == 365
2131
        assert len(ctx.captured_queries) == 319
2132 2132
    assert Page.snapshots.latest('pk').related_cells == {'cell_types': ['data_textcell']}
2133 2133
    assert resp2.text.index('Hello world') < resp2.text.index('Foobar3')
2134 2134

  
......
2189 2189
    resp = resp.click('restore', index=6)
2190 2190
    with CaptureQueriesContext(connection) as ctx:
2191 2191
        resp = resp.form.submit().follow()
2192
        assert len(ctx.captured_queries) == 587
2192
        assert len(ctx.captured_queries) == 575
2193 2193

  
2194 2194
    resp2 = resp.click('See online')
2195 2195
    assert resp2.text.index('Foobar1') < resp2.text.index('Foobar2') < resp2.text.index('Foobar3')
2196
-