Merge branch 'develop' into fast_containers
authorStephen Burrows <stephen.r.burrows@gmail.com>
Mon, 27 Jun 2011 14:11:50 +0000 (10:11 -0400)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Mon, 27 Jun 2011 14:11:50 +0000 (10:11 -0400)
Conflicts:
philo/templatetags/containers.py

philo/templatetags/containers.py

index be94a3d..2c55034 100644 (file)
@@ -7,12 +7,30 @@ from django import template
 from django.conf import settings
 from django.contrib.contenttypes.models import ContentType
 from django.core.exceptions import ObjectDoesNotExist
+from django.db.models import Q
 from django.utils.safestring import SafeUnicode, mark_safe
 
 
 register = template.Library()
 
 
+CONTAINER_CONTEXT_KEY = 'philo_container_context'
+
+
+class ContainerContext(object):
+       def __init__(self, page):
+               contentlet_specs, contentreference_specs = page.template.containers
+               
+               contentlets = page.contentlets.filter(name__in=contentlet_specs)
+               self.contentlets = dict(((c.name, c) for c in contentlets))
+               
+               q = Q()
+               for name, ct in contentreference_specs.items():
+                       q |= Q(name=name, content_type=ct)
+               references = page.contentreferences.filter(q)
+               self.references = dict(((c.name, c) for c in references))
+
+
 class ContainerNode(template.Node):
        def __init__(self, name, references=None, as_var=None):
                self.name = name
@@ -36,21 +54,28 @@ class ContainerNode(template.Node):
                return container_content
        
        def get_container_content(self, context):
-               page = context['page']
+               try:
+                       container_context = context.render_context[CONTAINER_CONTEXT_KEY]
+               except KeyError:
+                       container_context = ContainerContext(context['page'])
+                       context.render_context[CONTAINER_CONTEXT_KEY] = container_context
+               
                if self.references:
                        # Then it's a content reference.
                        try:
-                               contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references)
-                               content = contentreference.content
-                       except ObjectDoesNotExist:
+                               contentreference = container_context.references[(self.name, self.references)]
+                       except KeyError:
                                content = ''
+                       else:
+                               content = contentreference.content
                else:
                        # Otherwise it's a contentlet.
                        try:
-                               contentlet = page.contentlets.get(name__exact=self.name)
-                               content = contentlet.content
-                       except ObjectDoesNotExist:
+                               contentlet = container_context.contentlets[self.name]
+                       except KeyError:
                                content = ''
+                       else:
+                               content = contentlet.content
                return content