Removed automatic interpretation of contentlets as templates as per issue #168.
[philo.git] / philo / templatetags / containers.py
1 """
2 The container template tags are automatically included as builtins if :mod:`philo` is an installed app.
3
4 """
5
6 from django import template
7 from django.conf import settings
8 from django.contrib.contenttypes.models import ContentType
9 from django.core.exceptions import ObjectDoesNotExist
10 from django.utils.safestring import SafeUnicode, mark_safe
11
12
13 register = template.Library()
14
15
16 class ContainerNode(template.Node):
17         def __init__(self, name, references=None, as_var=None):
18                 self.name = name
19                 self.as_var = as_var
20                 self.references = references
21         
22         def render(self, context):
23                 content = settings.TEMPLATE_STRING_IF_INVALID
24                 if 'page' in context:
25                         container_content = self.get_container_content(context)
26                 else:
27                         container_content = None
28                 
29                 if self.as_var:
30                         context[self.as_var] = container_content
31                         return ''
32                 
33                 if not container_content:
34                         return ''
35                 
36                 return container_content
37         
38         def get_container_content(self, context):
39                 page = context['page']
40                 if self.references:
41                         # Then it's a content reference.
42                         try:
43                                 contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references)
44                                 content = contentreference.content
45                         except ObjectDoesNotExist:
46                                 content = ''
47                 else:
48                         # Otherwise it's a contentlet.
49                         try:
50                                 contentlet = page.contentlets.get(name__exact=self.name)
51                                 content = contentlet.content
52                         except ObjectDoesNotExist:
53                                 content = ''
54                 return content
55
56
57 @register.tag
58 def container(parser, token):
59         """
60         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.
61         
62         Usage::
63         
64                 {% container <name> [[references <app_label>.<model_name>] as <variable>] %}
65         
66         """
67         params = token.split_contents()
68         if len(params) >= 2:
69                 tag = params[0]
70                 name = params[1].strip('"')
71                 references = None
72                 as_var = None
73                 if len(params) > 2:
74                         remaining_tokens = params[2:]
75                         while remaining_tokens:
76                                 option_token = remaining_tokens.pop(0)
77                                 if option_token == 'references':
78                                         try:
79                                                 app_label, model = remaining_tokens.pop(0).strip('"').split('.')
80                                                 references = ContentType.objects.get_by_natural_key(app_label, model)
81                                         except IndexError:
82                                                 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument specifying a content type' % tag)
83                                         except ValueError:
84                                                 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model (see django.contrib.contenttypes)' % tag)
85                                         except ObjectDoesNotExist:
86                                                 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model which refers to an installed content type (see django.contrib.contenttypes)' % tag)
87                                 elif option_token == 'as':
88                                         try:
89                                                 as_var = remaining_tokens.pop(0)
90                                         except IndexError:
91                                                 raise template.TemplateSyntaxError('"%s" template tag option "as" requires an argument specifying a variable name' % tag)
92                         if references and not as_var:
93                                 raise template.TemplateSyntaxError('"%s" template tags using "references" option require additional use of the "as" option specifying a variable name' % tag)
94                 return ContainerNode(name, references, as_var)
95                 
96         else: # error
97                 raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag)