Fixing a bug in BlogView's tag_view where it would provide a QuerySet as `tag` instea...
[philo.git] / templatetags / containers.py
1 from django import template
2 from django.conf import settings
3 from django.utils.safestring import SafeUnicode, mark_safe
4 from django.core.exceptions import ObjectDoesNotExist
5 from django.contrib.contenttypes.models import ContentType
6
7
8 register = template.Library()
9
10
11 class ContainerNode(template.Node):
12         child_nodelists = ('nodelist_main', 'nodelist_empty',)
13         
14         def __init__(self, name, references=None, as_var=None, nodelist_main=None, nodelist_empty=None):
15                 self.name = name
16                 self.as_var = as_var
17                 self.references = references
18                 
19                 if nodelist_main is None:
20                         self.nodelist_main = template.NodeList()
21                 else:
22                         self.nodelist_main = nodelist_main
23                 
24                 if nodelist_empty is None:
25                         self.nodelist_empty = template.NodeList()
26                 else:
27                         self.nodelist_empty = nodelist_empty
28         
29         def render(self, context):
30                 content = settings.TEMPLATE_STRING_IF_INVALID
31                 if 'page' in context:
32                         container_content = self.get_container_content(context)
33                 
34                 if not self.nodelist_main:
35                         if container_content and self.as_var:
36                                 context[self.as_var] = container_content
37                                 return ''
38                         return container_content
39                 
40                 if container_content:
41                         if self.as_var is None:
42                                 self.as_var = self.name
43                         
44                         #make a new context 
45                         context.push()
46                         context[self.as_var] = container_content
47                         nodelist = template.NodeList()
48                         for node in self.nodelist_main:
49                                 nodelist.append(node.render(context))
50                         context.pop()
51                         return nodelist.render(context)
52                 
53                 if self.nodelist_empty is not None:
54                         return self.nodelist_empty.render(context)
55                 
56                 return ''
57         
58         def get_container_content(self, context):
59                 page = context['page']
60                 if self.references:
61                         try:
62                                 contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references)
63                                 content = contentreference.content
64                         except ObjectDoesNotExist:
65                                 content = ''
66                 else:
67                         try:
68                                 contentlet = page.contentlets.get(name__exact=self.name)
69                                 if contentlet.dynamic:
70                                         try:
71                                                 content = mark_safe(template.Template(contentlet.content, name=contentlet.name).render(context))
72                                         except template.TemplateSyntaxError, error:
73                                                 if settings.DEBUG:
74                                                         content = ('[Error parsing contentlet \'%s\': %s]' % (self.name, error))
75                                 else:
76                                         content = contentlet.content
77                         except ObjectDoesNotExist:
78                                 content = ''
79                 return content
80
81
82 def do_container(parser, token):
83         """
84         {% container <name> [[references <type>] as <variable>] %} 
85         {% blockcontainer <name> [[references <type>] as <variable>] %} [ {% empty %} ] {% endblockcontainer %}
86         """
87         params = token.split_contents()
88         if len(params) >= 2:
89                 tag = params[0]
90                 name = params[1].strip('"')
91                 references = None
92                 as_var = None
93                 if len(params) > 2:
94                         remaining_tokens = params[2:]
95                         while remaining_tokens:
96                                 option_token = remaining_tokens.pop(0)
97                                 if option_token == 'references':
98                                         try:
99                                                 app_label, model = remaining_tokens.pop(0).strip('"').split('.')
100                                                 references = ContentType.objects.get(app_label=app_label, model=model)
101                                         except IndexError:
102                                                 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument specifying a content type' % tag)
103                                         except ValueError:
104                                                 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model (see django.contrib.contenttypes)' % tag)
105                                         except ObjectDoesNotExist:
106                                                 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)
107                                 elif option_token == 'as':
108                                         try:
109                                                 as_var = remaining_tokens.pop(0)
110                                         except IndexError:
111                                                 raise template.TemplateSyntaxError('"%s" template tag option "as" requires an argument specifying a variable name' % tag)
112                         if references and not as_var:
113                                 raise template.TemplateSyntaxError('"%s" template tags using "references" option require additional use of the "as" option specifying a variable name' % tag)
114                 if tag == 'container':
115                         return ContainerNode(name, references, as_var)
116                 
117                 nodelist_main = parser.parse(('empty','endblockcontainer',))
118                 token = parser.next_token()
119                 
120                 if token.contents == 'empty':
121                         nodelist_empty = parser.parse(('endblockcontainer',))
122                         parser.delete_first_token()
123                 else:
124                         nodelist_empty = None
125                 return ContainerNode(name, references, as_var, nodelist_main, nodelist_empty)
126                 
127         else: # error
128                 raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag)
129
130
131 register.tag('container', do_container)
132 register.tag('blockcontainer', do_container)