perf: optimize extend_with_parent_cells

This commit is contained in:
Frédéric Péters 2018-01-08 22:33:18 +01:00
parent 247f2e0dfb
commit 4df4d04736
2 changed files with 19 additions and 20 deletions

View File

@ -834,22 +834,21 @@ class ParentContentCell(CellBase):
class Meta:
verbose_name = _('Same as parent')
def get_cells(self):
if self.page.parent:
parent_page = self.page.parent
elif self.page.slug != 'index':
try:
parent_page = Page.objects.get(slug='index', parent=None)
except Page.DoesNotExist:
return []
else:
return []
cells = list(CellBase.get_cells(placeholder=self.placeholder, page=parent_page))
for i, cell in enumerate(cells):
if not isinstance(cell, ParentContentCell):
continue
cells[i:i+1] = cell.get_cells()
break
def get_cells(self, hierarchy):
page_ids = [Page.objects.get(slug='index', parent=None).id] + [x.id for x in hierarchy[:-1]]
cells_by_page = {}
for page_id in page_ids:
cells_by_page[page_id] = []
for cell in list(CellBase.get_cells(placeholder=self.placeholder, page__in=page_ids)):
cells_by_page[cell.page_id].append(cell)
cells = cells_by_page[page_ids[-1]]
for page_id in reversed(page_ids[:-1]):
for i, cell in enumerate(cells):
if not isinstance(cell, ParentContentCell):
continue
cells[i:i+1] = cells_by_page[page_id]
break
return cells
def render(self, context):

View File

@ -125,12 +125,12 @@ def render_cell(request, cell):
return HttpResponse(template.render(context, request), content_type='text/html')
def extend_with_parent_cells(cells):
def extend_with_parent_cells(cells, hierarchy):
for cell in cells[:]:
if not isinstance(cell, ParentContentCell):
continue
idx = cells.index(cell)
cells[idx:idx+1] = cell.get_cells()
cells[idx:idx+1] = cell.get_cells(hierarchy=hierarchy[:-1])
def skeleton(request):
@ -216,7 +216,7 @@ def skeleton(request):
pages = selected_page.get_parents_and_self()
combo_template = settings.COMBO_PUBLIC_TEMPLATES[selected_page.template_name]
cells = list(cells)
extend_with_parent_cells(cells)
extend_with_parent_cells(cells, hierarchy=pages)
ctx = {
'page': selected_page,
@ -357,7 +357,7 @@ def publish_page(request, page, status=200, template_name=None):
return HttpResponseRedirect(page.get_redirect_url())
cells = list(CellBase.get_cells(page_id=page.id))
extend_with_parent_cells(cells)
extend_with_parent_cells(cells, hierarchy=pages)
cells = [x for x in cells if x.is_visible(user=request.user)]
ctx = {