From: Stephen Burrows Date: Mon, 27 Jun 2011 17:52:44 +0000 (-0400) Subject: Moved caching of contentlets/contentreferences so that (for example) a page with... X-Git-Tag: philo-0.9.1^2~5^2~8^2 X-Git-Url: http://git.ithinksw.org/philo.git/commitdiff_plain/85bb4af20c882f754c41afaee7573c5b9ff6222c Moved caching of contentlets/contentreferences so that (for example) a page with only contentlets will not fetch content references. Set up content references to pre-fetch all related pieces of content to save queries. --- diff --git a/philo/templatetags/containers.py b/philo/templatetags/containers.py index c9ef2a0..3956f1d 100644 --- a/philo/templatetags/containers.py +++ b/philo/templatetags/containers.py @@ -18,9 +18,29 @@ CONTAINER_CONTEXT_KEY = 'philo_container_context' class ContainerContext(object): - def __init__(self, contentlets, references): - self.contentlets = dict(((c.name, c) for c in contentlets)) - self.references = dict((((c.name, ContentType.objects.get_for_id(c.content_type_id)), c) for c in references)) + def __init__(self, page): + self.page = page + + def get_contentlets(self): + if not hasattr(self, '_contentlets'): + self._contentlets = dict(((c.name, c) for c in self.page.contentlets.all())) + return self._contentlets + + def get_references(self): + if not hasattr(self, '_references'): + references = self.page.contentreferences.all() + self._references = {} + contents = {} + for c in references: + ct = ContentType.objects.get_for_id(c.content_type_id) + self.references[(c.name, ct)] = c + contents.setdefault(ct, {})[c.content_id] = c + + for ct in contents: + objs = ct.model_class().objects.filter(pk__in=contents[ct]) + for obj in objs: + contents[ct][obj.pk].content = obj + return self._references class ContainerNode(template.Node): @@ -47,16 +67,13 @@ class ContainerNode(template.Node): except KeyError: return settings.TEMPLATE_STRING_IF_INVALID - contentlets = page.contentlets.all() - references = page.contentreferences.all() - - container_context = ContainerContext(contentlets, references) + container_context = ContainerContext(page) context.render_context[CONTAINER_CONTEXT_KEY] = container_context if self.references: # Then it's a content reference. try: - contentreference = container_context.references[(self.name, self.references)] + contentreference = container_context.get_references()[(self.name, self.references)] except KeyError: content = '' else: @@ -64,7 +81,7 @@ class ContainerNode(template.Node): else: # Otherwise it's a contentlet. try: - contentlet = container_context.contentlets[self.name] + contentlet = container_context.get_contentlets()[self.name] except KeyError: content = '' else: