Eliminated unnecessary ContentType queries that used applabel/model-based lookups.
[philo.git] / philo / templatetags / containers.py
index c5fd445..bc04b9f 100644 (file)
@@ -1,8 +1,13 @@
+"""
+The container template tags are automatically included as builtins if :mod:`philo` is an installed app.
+
+"""
+
 from django import template
 from django.conf import settings
-from django.utils.safestring import SafeUnicode, mark_safe
-from django.core.exceptions import ObjectDoesNotExist
 from django.contrib.contenttypes.models import ContentType
+from django.core.exceptions import ObjectDoesNotExist
+from django.utils.safestring import SafeUnicode, mark_safe
 
 
 register = template.Library()
@@ -59,9 +64,15 @@ class ContainerNode(template.Node):
                return content
 
 
-def do_container(parser, token):
+@register.tag
+def container(parser, token):
        """
-       {% container <name> [[references <type>] as <variable>] %}
+       If a template using this tag is used to render a :class:`.Page`, that :class:`.Page` will have associated content which can be set in the admin interface. If a content type is referenced, then a :class:`.ContentReference` object will be created; otherwise, a :class:`.Contentlet` object will be created.
+       
+       Usage::
+       
+               {% container <name> [[references <app_label>.<model_name>] as <variable>] %}
+       
        """
        params = token.split_contents()
        if len(params) >= 2:
@@ -76,7 +87,7 @@ def do_container(parser, token):
                                if option_token == 'references':
                                        try:
                                                app_label, model = remaining_tokens.pop(0).strip('"').split('.')
-                                               references = ContentType.objects.get(app_label=app_label, model=model)
+                                               references = ContentType.objects.get_by_natural_key(app_label, model)
                                        except IndexError:
                                                raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument specifying a content type' % tag)
                                        except ValueError:
@@ -94,6 +105,3 @@ def do_container(parser, token):
                
        else: # error
                raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag)
-
-
-register.tag('container', do_container)