Projet

Général

Profil

0001-misc-get-cells-from-private-placeholders-in-parent-a.patch

Frédéric Péters, 03 avril 2020 15:10

Télécharger (5,22 ko)

Voir les différences:

Subject: [PATCH] misc: get cells from private placeholders in parent
 acquisition (#41276)

 combo/data/models.py  | 11 +++++++++--
 combo/public/views.py |  8 +++++++-
 tests/test_public.py  | 36 +++++++++++++++++++++++++++++++++++-
 3 files changed, 51 insertions(+), 4 deletions(-)
combo/data/models.py
39 39
from django.db import models, transaction
40 40
from django.db.models.base import ModelBase
41 41
from django.db.models.signals import pre_save, post_save, post_delete
42
from django.db.models import Max
42
from django.db.models import Max, Q
43 43
from django.dispatch import receiver
44 44
from django.forms import models as model_forms
45 45
from django import forms
......
715 715
        if load_contenttypes:
716 716
            # populate ContentType cache
717 717
            ContentType.objects.get_for_models(*cell_classes)
718
        extra_filter = kwargs.pop('extra_filter', None)
718 719
        for klass in cell_classes:
719 720
            if klass is None:
720 721
                continue
721 722
            if cell_filter and not cell_filter(klass):
722 723
                continue
723 724
            cells_queryset = klass.objects.filter(**kwargs)
725
            if extra_filter:
726
                cells_queryset = cells_queryset.filter(extra_filter)
724 727
            if select_related:
725 728
                cells_queryset = cells_queryset.select_related(
726 729
                    *select_related.get('__all__', []),
......
1381 1384
        cells_by_page = {}
1382 1385
        for page in pages:
1383 1386
            cells_by_page[page.id] = []
1384
        for cell in list(CellBase.get_cells(placeholder=self.placeholder, page__in=pages)):
1387
        seen = {}
1388
        # get cells from placeholder + cells in private placeholders that may
1389
        # be used by actual cells.
1390
        for cell in CellBase.get_cells(page__in=pages, extra_filter=Q(placeholder=self.placeholder) | Q(placeholder__startswith='_')):
1391
            seen[(cell.__class__, cell.id)] = True
1385 1392
            cells_by_page[cell.page_id].append(cell)
1386 1393

  
1387 1394
        cells = cells_by_page[pages[-1].id]
combo/public/views.py
174 174
    if len(hierarchy) == 1 and hierarchy[0].slug == 'index':
175 175
        # home page cannot contain parent cells
176 176
        return
177
    seen = {}
177 178
    for cell in cells[:]:
178 179
        if not isinstance(cell, ParentContentCell):
179 180
            continue
180 181
        idx = cells.index(cell)
181
        cells[idx:idx+1] = cell.get_parents_cells(hierarchy=hierarchy[:-1])
182
        parent_cells = cell.get_parents_cells(hierarchy=hierarchy[:-1])
183
        # keep cells that were not already seen and mark cells as seen,
184
        # simultaneously.
185
        parent_cells = [seen.setdefault((x.__class__, x.id), True) and x
186
                for x in parent_cells if (x.__class__, x.id) not in seen]
187
        cells[idx:idx + 1] = parent_cells
182 188

  
183 189

  
184 190
def should_check_badges():
tests/test_public.py
25 25

  
26 26
from combo.wsgi import application
27 27
from combo.data.models import (Page, CellBase, TextCell, ParentContentCell,
28
        FeedCell, LinkCell, ConfigJsonCell, Redirect, JsonCell)
28
        FeedCell, LinkCell, LinkListCell, ConfigJsonCell, Redirect, JsonCell)
29 29
from combo.apps.family.models import FamilyInfosCell
30 30

  
31 31
pytestmark = pytest.mark.django_db
......
199 199
    resp = app.get('/sixth/', status=200)
200 200
    assert resp.text.count('BARFOO') == 1
201 201

  
202

  
203
def test_list_of_links_acquisition(app):
204
    Page.objects.all().delete()
205
    index_page = Page(title='Home', slug='index', template_name='standard')
206
    index_page.save()
207

  
208
    cell = LinkListCell.objects.create(order=0, page=index_page, placeholder='footer')
209
    LinkCell.objects.create(
210
        page=index_page,
211
        placeholder=cell.link_placeholder,
212
        title='Example Site',
213
        url='http://example.net/',
214
        order=0,
215
    )
216
    LinkCell.objects.create(
217
        page=index_page,
218
        placeholder=cell.link_placeholder,
219
        title='Example2 Site',
220
        url='http://example.org/',
221
        order=1,
222
    )
223

  
224
    resp = app.get('/', status=200)
225
    assert resp.text.count('Example Site') == 1
226
    assert resp.text.count('Example2 Site') == 1
227

  
228
    page = Page(title='Second', slug='second', template_name='standard')
229
    page.save()
230
    ParentContentCell(page=page, placeholder='footer', order=0).save()
231
    resp = app.get('/second/', status=200)
232
    assert resp.text.count('Example Site') == 1
233
    assert resp.text.count('Example2 Site') == 1
234

  
235

  
202 236
def test_page_redirect(app):
203 237
    Page.objects.all().delete()
204 238
    page = Page(title='Elsewhere', slug='elsewhere', template_name='standard',
205
-