Projet

Général

Profil

0001-data-inject-global-context-in-context-for-cell-condi.patch

Lauréline Guérin, 05 juillet 2022 09:48

Télécharger (5,52 ko)

Voir les différences:

Subject: [PATCH] data: inject global context in context for cell conditions
 (#66953)

 combo/data/models.py  | 15 ++++++++-------
 combo/public/views.py | 27 ++++++++++++++++-----------
 tests/test_public.py  | 22 ++++++++++++++++++----
 3 files changed, 42 insertions(+), 22 deletions(-)
combo/data/models.py
1327 1327
            and validity_info.invalid_datetime <= now()
1328 1328
        )
1329 1329

  
1330
    def compute_condition(self, request):
1330
    def compute_condition(self, request, original_context):
1331 1331
        condition = self.condition
1332 1332
        if not condition:
1333 1333
            return True
1334
        context = self.page.get_extra_variables(request, {})
1335
        context = RequestContext(request, context)
1334
        context = RequestContext(request)
1335
        context.push(original_context)
1336 1336
        try:
1337 1337
            return Template('{%% if %s %%}OK{%% endif %%}' % condition).render(context) == 'OK'
1338 1338
        except (TemplateSyntaxError, VariableDoesNotExist):
1339 1339
            return False
1340 1340

  
1341
    def is_visible(self, request, check_validity_info=True):
1342
        condition = self.compute_condition(request=request)
1343
        if not condition:
1344
            return False
1341
    def is_visible(self, request, context=None, check_validity_info=True):
1342
        if context:
1343
            condition = self.compute_condition(request=request, original_context=context)
1344
            if not condition:
1345
                return False
1345 1346
        if check_validity_info and self.is_hidden_because_invalid():
1346 1347
            return False
1347 1348
        return element_is_visible(self, user=getattr(request, 'user', None))
combo/public/views.py
547 547
        if redirect_url:
548 548
            return HttpResponseRedirect(redirect_url)
549 549

  
550
    ctx = {
551
        'check_badges': should_check_badges(),
552
        'page': page,
553
        'pages': pages,
554
        'request': request,
555
    }
556
    ctx.update(getattr(request, 'extra_context_data', {}))
557
    modify_global_context(request, ctx)
558

  
550 559
    cells = CellBase.get_cells(
551 560
        page=page,
552 561
        select_related={'data_linkcell': ['link_page']},
......
554 563
        cells_exclude=Q(placeholder__in=['_auto_tile', '_dashboard', '_suggested_tile']),
555 564
    )
556 565
    extend_with_parent_cells(cells, hierarchy=pages)
557
    cells = [x for x in cells if x.is_visible(request)]
566
    cells = [x for x in cells if x.is_visible(request, context=ctx)]
558 567
    mark_duplicated_slugs(cells)
559 568

  
560 569
    # load assets
......
565 574
    for cell in cells:
566 575
        cell._assets = {a.key: a for a in assets if a.key in cell._asset_keys.keys()}
567 576

  
568
    ctx = {
569
        'check_badges': should_check_badges(),
570
        'page': page,
571
        'page_cells': cells,
572
        'pages': pages,
573
        'request': request,
574
        'media': sum((cell.media for cell in cells), Media()),
575
    }
576
    ctx.update(getattr(request, 'extra_context_data', {}))
577
    modify_global_context(request, ctx)
577
    ctx.update(
578
        {
579
            'page_cells': cells,
580
            'media': sum((cell.media for cell in cells), Media()),
581
        }
582
    )
578 583
    if getattr(settings, 'COMBO_TEST_ALWAYS_RENDER_CELLS_SYNCHRONOUSLY', False):
579 584
        ctx['synchronous'] = True
580 585

  
tests/test_public.py
139 139

  
140 140

  
141 141
def test_page_contents_condition(app, normal_user):
142
    page = Page.objects.create(title='Home', slug='index', template_name='standard')
143
    TextCell.objects.create(page=page, placeholder='content', text='Foobar', order=0, condition='True')
144
    TextCell.objects.create(page=page, placeholder='content', text='Foobaz', order=1, condition='False')
142
    page = Page.objects.create(title='Page', slug='page', template_name='standard')
143
    cell1 = TextCell.objects.create(
144
        page=page, placeholder='content', text='Foobar', order=0, condition='True'
145
    )
146
    cell2 = TextCell.objects.create(
147
        page=page, placeholder='content', text='Foobaz', order=1, condition='False'
148
    )
145 149
    cell3 = LinkListCell.objects.create(order=1, page=page, placeholder='content')
146 150
    LinkCell.objects.create(
147 151
        page=page,
......
161 165
    )
162 166

  
163 167
    app = login(app, username='normal-user', password='normal-user')
164
    resp = app.get('/', status=200)
168
    resp = app.get(page.get_online_url())
165 169
    assert 'Foobar' in resp.text
166 170
    assert 'Foobaz' not in resp.text
167 171
    assert 'Example Site' in resp.text
168 172
    assert 'Other Site' not in resp.text
169 173

  
174
    page.sub_slug = 'foo_id'
175
    page.save()
176
    cell1.condition = 'foo_id'
177
    cell1.save()
178
    cell2.condition = 'not foo_id'
179
    cell2.save()
180
    resp = app.get(page.get_online_url() + '11/')
181
    assert 'Foobar' in resp.text
182
    assert 'Foobaz' not in resp.text
183

  
170 184

  
171 185
def test_page_footer_acquisition(app):
172 186
    Page.objects.all().delete()
173
-