Projet

Général

Profil

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

Frédéric Péters, 04 avril 2020 09:07

Télécharger (6,24 ko)

Voir les différences:

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

 combo/data/models.py  | 16 +++++++++++++---
 combo/public/views.py |  8 +++++++-
 tests/test_public.py  | 36 +++++++++++++++++++++++++++++++++++-
 3 files changed, 55 insertions(+), 5 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
......
569 569
    default_form_class = None
570 570
    manager_form_factory_kwargs = {}
571 571
    manager_form_template = 'combo/cell_form.html'
572
    children_placeholder_prefix = None
572 573

  
573 574
    visible = True
574 575
    user_dependant = False
......
715 716
        if load_contenttypes:
716 717
            # populate ContentType cache
717 718
            ContentType.objects.get_for_models(*cell_classes)
719
        extra_filter = kwargs.pop('extra_filter', None)
718 720
        for klass in cell_classes:
719 721
            if klass is None:
720 722
                continue
721 723
            if cell_filter and not cell_filter(klass):
722 724
                continue
723 725
            cells_queryset = klass.objects.filter(**kwargs)
726
            if extra_filter:
727
                cells_queryset = cells_queryset.filter(extra_filter)
724 728
            if select_related:
725 729
                cells_queryset = cells_queryset.select_related(
726 730
                    *select_related.get('__all__', []),
......
1210 1214

  
1211 1215
    template_name = 'combo/link-list-cell.html'
1212 1216
    manager_form_template = 'combo/manager/link-list-cell-form.html'
1217
    children_placeholder_prefix = '_linkslist:'
1213 1218

  
1214 1219
    invalid_reason_codes = {
1215 1220
        'data_link_invalid': _('Invalid link'),
......
1220 1225

  
1221 1226
    @property
1222 1227
    def link_placeholder(self):
1223
        return '_linkslist:{}'.format(self.pk)
1228
        return self.children_placeholder_prefix + str(self.pk)
1224 1229

  
1225 1230
    def get_items(self, prefetch_validity_info=False):
1226 1231
        return CellBase.get_cells(
......
1381 1386
        cells_by_page = {}
1382 1387
        for page in pages:
1383 1388
            cells_by_page[page.id] = []
1384
        for cell in list(CellBase.get_cells(placeholder=self.placeholder, page__in=pages)):
1389
        # get cells from placeholder + cells in private placeholders that may
1390
        # be used by actual cells.
1391
        placeholder_filter = Q(placeholder=self.placeholder)
1392
        for klass in CellBase.get_cell_classes(lambda x: bool(x.children_placeholder_prefix)):
1393
            placeholder_filter |= Q(placeholder__startswith=klass.children_placeholder_prefix)
1394
        for cell in CellBase.get_cells(page__in=pages, extra_filter=placeholder_filter):
1385 1395
            cells_by_page[cell.page_id].append(cell)
1386 1396

  
1387 1397
        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
-