Added philo 404 handling via a relationship named Http404 on nodes. Uses the passthro...
[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         def __init__(self, name, references=None, as_var=None):
13                 self.name = name
14                 self.as_var = as_var
15                 self.references = references
16         
17         def render(self, context):
18                 content = settings.TEMPLATE_STRING_IF_INVALID
19                 if 'page' in context:
20                         container_content = self.get_container_content(context)
21                 
22                 if self.as_var:
23                         context[self.as_var] = container_content
24                         return ''
25                 
26                 if not container_content:
27                         return ''
28                 
29                 return container_content
30         
31         def get_container_content(self, context):
32                 page = context['page']
33                 if self.references:
34                         try:
35                                 contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references)
36                                 content = contentreference.content
37                         except ObjectDoesNotExist:
38                                 content = ''
39                 else:
40                         try:
41                                 contentlet = page.contentlets.get(name__exact=self.name)
42                                 if contentlet.dynamic:
43                                         try:
44                                                 content = mark_safe(template.Template(contentlet.content, name=contentlet.name).render(context))
45                                         except template.TemplateSyntaxError, error:
46                                                 if settings.DEBUG:
47                                                         content = ('[Error parsing contentlet \'%s\': %s]' % (self.name, error))
48                                 else:
49                                         content = contentlet.content
50                         except ObjectDoesNotExist:
51                                 content = ''
52                 return content
53
54
55 def do_container(parser, token):
56         """
57         {% container <name> [[references <type>] as <variable>] %}
58         """
59         params = token.split_contents()
60         if len(params) >= 2:
61                 tag = params[0]
62                 name = params[1].strip('"')
63                 references = None
64                 as_var = None
65                 if len(params) > 2:
66                         remaining_tokens = params[2:]
67                         while remaining_tokens:
68                                 option_token = remaining_tokens.pop(0)
69                                 if option_token == 'references':
70                                         try:
71                                                 app_label, model = remaining_tokens.pop(0).strip('"').split('.')
72                                                 references = ContentType.objects.get(app_label=app_label, model=model)
73                                         except IndexError:
74                                                 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument specifying a content type' % tag)
75                                         except ValueError:
76                                                 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model (see django.contrib.contenttypes)' % tag)
77                                         except ObjectDoesNotExist:
78                                                 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)
79                                 elif option_token == 'as':
80                                         try:
81                                                 as_var = remaining_tokens.pop(0)
82                                         except IndexError:
83                                                 raise template.TemplateSyntaxError('"%s" template tag option "as" requires an argument specifying a variable name' % tag)
84                         if references and not as_var:
85                                 raise template.TemplateSyntaxError('"%s" template tags using "references" option require additional use of the "as" option specifying a variable name' % tag)
86                 return ContainerNode(name, references, as_var)
87                 
88         else: # error
89                 raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag)
90
91
92 register.tag('container', do_container)