Made referencing containers optional on pages. Fixes a bug.
[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 self.nodelist_main is None:
35                         self.nodelist_main
36                         if container_content and self.as_var:
37                                 context[self.as_var] = container_content
38                                 return ''
39                         return container_content
40                 
41                 if container_content:
42                         if self.as_var is None:
43                                 self.as_var = self.name
44                         
45                         #make a new context 
46                         context.push()
47                         context[self.as_var] = container_content
48                         nodelist = template.NodeList()
49                         for node in self.nodelist_main:
50                                 nodelist.append(node.render(context))
51                         context.pop()
52                         return nodelist.render(context)
53                 
54                 if self.nodelist_empty is not None:
55                         return self.nodelist_empty.render(context)
56                 
57                 return ''
58         
59         def get_container_content(self, context):
60                 page = context['page']
61                 if self.references:
62                         try:
63                                 contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references)
64                                 content = contentreference.content
65                         except ObjectDoesNotExist:
66                                 content = ''
67                 else:
68                         try:
69                                 contentlet = page.contentlets.get(name__exact=self.name)
70                                 if contentlet.dynamic:
71                                         try:
72                                                 content = mark_safe(template.Template(contentlet.content, name=contentlet.name).render(context))
73                                         except template.TemplateSyntaxError, error:
74                                                 if settings.DEBUG:
75                                                         content = ('[Error parsing contentlet \'%s\': %s]' % (self.name, error))
76                                 else:
77                                         content = contentlet.content
78                         except ObjectDoesNotExist:
79                                 content = ''
80                 return content
81
82
83 def do_container(parser, token):
84         """
85         {% container <name> [[references <type>] as <variable>] %} 
86         {% blockcontainer <name> [[references <type>] as <variable>] %} [ {% empty %} ] {% endblockcontainer %}
87         """
88         params = token.split_contents()
89         if len(params) >= 2:
90                 tag = params[0]
91                 name = params[1].strip('"')
92                 references = None
93                 as_var = None
94                 if len(params) > 2:
95                         remaining_tokens = params[2:]
96                         while remaining_tokens:
97                                 option_token = remaining_tokens.pop(0)
98                                 if option_token == 'references':
99                                         try:
100                                                 app_label, model = remaining_tokens.pop(0).strip('"').split('.')
101                                                 references = ContentType.objects.get(app_label=app_label, model=model)
102                                         except IndexError:
103                                                 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument specifying a content type' % tag)
104                                         except ValueError:
105                                                 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model (see django.contrib.contenttypes)' % tag)
106                                         except ObjectDoesNotExist:
107                                                 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)
108                                 elif option_token == 'as':
109                                         try:
110                                                 as_var = remaining_tokens.pop(0)
111                                         except IndexError:
112                                                 raise template.TemplateSyntaxError('"%s" template tag option "as" requires an argument specifying a variable name' % tag)
113                         if references and not as_var:
114                                 raise template.TemplateSyntaxError('"%s" template tags using "references" option require additional use of the "as" option specifying a variable name' % tag)
115                 if tag == 'container':
116                         return ContainerNode(name, references, as_var)
117                 
118                 nodelist_main = parser.parse(('empty','endblockcontainer',))
119                 token = parser.next_token()
120                 
121                 if token.contents == 'empty':
122                         nodelist_empty = parser.parse(('endblockcontainer',))
123                         parser.delete_first_token()
124                 else:
125                         nodelist_empty = None
126                 return ContainerNode(name, references, as_var, nodelist_main, nodelist_empty)
127                 
128         else: # error
129                 raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag)
130
131
132 register.tag('container', do_container)
133 register.tag('blockcontainer', do_container)