Pre-fetching the contentreference content may have been premature optimization. Switc...
[philo.git] / philo / templatetags / containers.py
index c9ef2a0..fdcd82c 100644 (file)
@@ -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,16 +57,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 +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: