X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/8d5b1f789ed643ac9582398b370e6cf5137b8a6b..829ebbd416e7b5c4a963635fca4a19e205af98c5:/philo/templatetags/containers.py diff --git a/philo/templatetags/containers.py b/philo/templatetags/containers.py index 11ccd88..fdcd82c 100644 --- a/philo/templatetags/containers.py +++ b/philo/templatetags/containers.py @@ -18,9 +18,19 @@ 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 = dict((((c.name, ContentType.objects.get_for_id(c.content_type_id)), c) for c in references)) + return self._references class ContainerNode(template.Node): @@ -47,20 +57,13 @@ class ContainerNode(template.Node): except KeyError: return settings.TEMPLATE_STRING_IF_INVALID - contentlet_specs, contentreference_specs = page.template.containers - contentlets = page.contentlets.filter(name__in=contentlet_specs) - q = Q() - for name, ct in contentreference_specs.items(): - q |= Q(name=name, content_type=ct) - references = page.contentreferences.filter(q) - - 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: @@ -68,7 +71,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: