data: inject global context in context for cell conditions (#66953)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Lauréline Guérin 2022-07-05 09:31:52 +02:00
parent 10b127edd3
commit 1f94e32a17
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 42 additions and 22 deletions

View File

@ -1327,21 +1327,22 @@ class CellBase(models.Model, metaclass=CellMeta):
and validity_info.invalid_datetime <= now() and validity_info.invalid_datetime <= now()
) )
def compute_condition(self, request): def compute_condition(self, request, original_context):
condition = self.condition condition = self.condition
if not condition: if not condition:
return True return True
context = self.page.get_extra_variables(request, {}) context = RequestContext(request)
context = RequestContext(request, context) context.push(original_context)
try: try:
return Template('{%% if %s %%}OK{%% endif %%}' % condition).render(context) == 'OK' return Template('{%% if %s %%}OK{%% endif %%}' % condition).render(context) == 'OK'
except (TemplateSyntaxError, VariableDoesNotExist): except (TemplateSyntaxError, VariableDoesNotExist):
return False return False
def is_visible(self, request, check_validity_info=True): def is_visible(self, request, context=None, check_validity_info=True):
condition = self.compute_condition(request=request) if context:
if not condition: condition = self.compute_condition(request=request, original_context=context)
return False if not condition:
return False
if check_validity_info and self.is_hidden_because_invalid(): if check_validity_info and self.is_hidden_because_invalid():
return False return False
return element_is_visible(self, user=getattr(request, 'user', None)) return element_is_visible(self, user=getattr(request, 'user', None))

View File

@ -547,6 +547,15 @@ def publish_page(request, page, status=200, template_name=None):
if redirect_url: if redirect_url:
return HttpResponseRedirect(redirect_url) return HttpResponseRedirect(redirect_url)
ctx = {
'check_badges': should_check_badges(),
'page': page,
'pages': pages,
'request': request,
}
ctx.update(getattr(request, 'extra_context_data', {}))
modify_global_context(request, ctx)
cells = CellBase.get_cells( cells = CellBase.get_cells(
page=page, page=page,
select_related={'data_linkcell': ['link_page']}, select_related={'data_linkcell': ['link_page']},
@ -554,7 +563,7 @@ def publish_page(request, page, status=200, template_name=None):
cells_exclude=Q(placeholder__in=['_auto_tile', '_dashboard', '_suggested_tile']), cells_exclude=Q(placeholder__in=['_auto_tile', '_dashboard', '_suggested_tile']),
) )
extend_with_parent_cells(cells, hierarchy=pages) extend_with_parent_cells(cells, hierarchy=pages)
cells = [x for x in cells if x.is_visible(request)] cells = [x for x in cells if x.is_visible(request, context=ctx)]
mark_duplicated_slugs(cells) mark_duplicated_slugs(cells)
# load assets # load assets
@ -565,16 +574,12 @@ def publish_page(request, page, status=200, template_name=None):
for cell in cells: for cell in cells:
cell._assets = {a.key: a for a in assets if a.key in cell._asset_keys.keys()} cell._assets = {a.key: a for a in assets if a.key in cell._asset_keys.keys()}
ctx = { ctx.update(
'check_badges': should_check_badges(), {
'page': page, 'page_cells': cells,
'page_cells': cells, 'media': sum((cell.media for cell in cells), Media()),
'pages': pages, }
'request': request, )
'media': sum((cell.media for cell in cells), Media()),
}
ctx.update(getattr(request, 'extra_context_data', {}))
modify_global_context(request, ctx)
if getattr(settings, 'COMBO_TEST_ALWAYS_RENDER_CELLS_SYNCHRONOUSLY', False): if getattr(settings, 'COMBO_TEST_ALWAYS_RENDER_CELLS_SYNCHRONOUSLY', False):
ctx['synchronous'] = True ctx['synchronous'] = True

View File

@ -139,9 +139,13 @@ def test_page_contents_group_absence(app, normal_user):
def test_page_contents_condition(app, normal_user): def test_page_contents_condition(app, normal_user):
page = Page.objects.create(title='Home', slug='index', template_name='standard') page = Page.objects.create(title='Page', slug='page', template_name='standard')
TextCell.objects.create(page=page, placeholder='content', text='Foobar', order=0, condition='True') cell1 = TextCell.objects.create(
TextCell.objects.create(page=page, placeholder='content', text='Foobaz', order=1, condition='False') page=page, placeholder='content', text='Foobar', order=0, condition='True'
)
cell2 = TextCell.objects.create(
page=page, placeholder='content', text='Foobaz', order=1, condition='False'
)
cell3 = LinkListCell.objects.create(order=1, page=page, placeholder='content') cell3 = LinkListCell.objects.create(order=1, page=page, placeholder='content')
LinkCell.objects.create( LinkCell.objects.create(
page=page, page=page,
@ -161,12 +165,22 @@ def test_page_contents_condition(app, normal_user):
) )
app = login(app, username='normal-user', password='normal-user') app = login(app, username='normal-user', password='normal-user')
resp = app.get('/', status=200) resp = app.get(page.get_online_url())
assert 'Foobar' in resp.text assert 'Foobar' in resp.text
assert 'Foobaz' not in resp.text assert 'Foobaz' not in resp.text
assert 'Example Site' in resp.text assert 'Example Site' in resp.text
assert 'Other Site' not in resp.text assert 'Other Site' not in resp.text
page.sub_slug = 'foo_id'
page.save()
cell1.condition = 'foo_id'
cell1.save()
cell2.condition = 'not foo_id'
cell2.save()
resp = app.get(page.get_online_url() + '11/')
assert 'Foobar' in resp.text
assert 'Foobaz' not in resp.text
def test_page_footer_acquisition(app): def test_page_footer_acquisition(app):
Page.objects.all().delete() Page.objects.all().delete()