perfs: limit cells considered for cell.modify_global_context (#21052)

This commit is contained in:
Frédéric Péters 2018-01-09 09:58:02 +01:00
parent a049d749e6
commit 85b3ddc697
2 changed files with 11 additions and 7 deletions

View File

@ -356,6 +356,10 @@ class CellBase(models.Model):
# get_badge(self, context); set to None so cell types can be skipped easily
get_badge = None
# modify_global_context(self, context, request=None)
# Apply changes to the template context that must visible to all cells in the page
modify_global_context = None
class Meta:
abstract = True
@ -526,10 +530,6 @@ class CellBase(models.Model):
tmpl = template.loader.select_template(template_names)
return tmpl.render(context, context.get('request'))
def modify_global_context(self, context, request=None):
'''Apply changes to the template context that must visible to all cells in the page'''
return context
def render_for_search(self):
if not self.is_enabled():
return ''

View File

@ -112,12 +112,15 @@ def render_cell(request, cell):
}
if cell.page_id:
other_cells = CellBase.get_cells(page_id=cell.page_id)
other_cells = []
for klass in CellBase.get_cell_classes(lambda x: bool(x.modify_global_context)):
other_cells.extend(klass.objects.filter(page_id=cell.page_id))
other_cells = [x for x in other_cells if x.is_visible(user=request.user)]
other_cells.sort(key=lambda x: x.order)
for other_cell in other_cells:
if other_cell.get_reference() != cell.get_reference():
other_cell.modify_global_context(context, request)
else:
elif cell.modify_global_context:
# Cell can pass data through its own __dict__
cell.modify_global_context(context, request)
@ -370,7 +373,8 @@ def publish_page(request, page, status=200, template_name=None):
}
for cell in cells:
cell.modify_global_context(ctx, request)
if cell.modify_global_context:
cell.modify_global_context(ctx, request)
if not template_name:
combo_template = settings.COMBO_PUBLIC_TEMPLATES[page.template_name]