2 from django.db import models
3 from django.contrib.contenttypes.models import ContentType
4 from django.contrib.contenttypes import generic
5 from django.conf import settings
6 from django.template import add_to_builtins as register_templatetags
7 from django.template import Template as DjangoTemplate
8 from django.template import TemplateDoesNotExist
9 from django.template import Context, RequestContext
10 from django.template.loader import get_template
11 from django.template.loader_tags import ExtendsNode, ConstantIncludeNode, IncludeNode
12 from django.http import HttpResponse
13 from philo.models.base import TreeModel, register_value_model
14 from philo.models.nodes import View
15 from philo.utils import fattr
16 from philo.templatetags.containers import ContainerNode
17 from philo.signals import page_about_to_render_to_string, page_finished_rendering_to_string
20 class Template(TreeModel):
21 name = models.CharField(max_length=255)
22 documentation = models.TextField(null=True, blank=True)
23 mimetype = models.CharField(max_length=255, null=True, blank=True, help_text='Default: %s' % settings.DEFAULT_CONTENT_TYPE)
24 code = models.TextField(verbose_name='django template code')
28 return 'philo.models.Template: ' + self.path
31 def django_template(self):
32 return DjangoTemplate(self.code)
37 Returns a tuple where the first item is a list of names of contentlets referenced by containers,
38 and the second item is a list of tuples of names and contenttypes of contentreferences referenced by containers.
39 This will break if there is a recursive extends or includes in the template code.
40 Due to the use of an empty Context, any extends or include tags with dynamic arguments probably won't work.
42 def container_nodes(template):
43 def nodelist_container_nodes(nodelist):
47 if hasattr(node, 'child_nodelists'):
48 for nodelist_name in node.child_nodelists:
49 if hasattr(node, nodelist_name):
50 nodes.extend(nodelist_container_nodes(getattr(node, nodelist_name)))
51 if isinstance(node, ContainerNode):
53 elif isinstance(node, ExtendsNode):
54 extended_template = node.get_parent(Context())
56 nodes.extend(container_nodes(extended_template))
57 elif isinstance(node, ConstantIncludeNode):
58 included_template = node.template
60 nodes.extend(container_nodes(included_template))
61 elif isinstance(node, IncludeNode):
62 included_template = get_template(node.template_name.resolve(Context()))
64 nodes.extend(container_nodes(included_template))
66 raise # fail for this node
68 return nodelist_container_nodes(template.nodelist)
69 all_nodes = container_nodes(self.django_template)
70 contentlet_node_names = set([node.name for node in all_nodes if not node.references])
71 contentreference_node_names = []
72 contentreference_node_specs = []
73 for node in all_nodes:
74 if node.references and node.name not in contentreference_node_names:
75 contentreference_node_specs.append((node.name, node.references))
76 contentreference_node_names.append(node.name)
77 return contentlet_node_names, contentreference_node_specs
79 def __unicode__(self):
80 return self.get_path(pathsep=u' › ', field='name')
83 @fattr(is_usable=True)
84 def loader(template_name, template_dirs=None): # load_template_source
86 template = Template.objects.get_with_path(template_name)
87 except Template.DoesNotExist:
88 raise TemplateDoesNotExist(template_name)
89 return (template.code, template.origin)
97 Represents a page - something which is rendered according to a template. The page will have a number of related Contentlets depending on the template selected - but these will appear only after the page has been saved with that template.
99 template = models.ForeignKey(Template, related_name='pages')
100 title = models.CharField(max_length=255)
102 def get_containers(self):
103 if not hasattr(self, '_containers'):
104 self._containers = self.template.containers
105 return self._containers
106 containers = property(get_containers)
108 def render_to_string(self, node=None, request=None, path=None, subpath=None, extra_context=None):
110 context.update(extra_context or {})
111 context.update({'page': self, 'attributes': self.attributes, 'relationships': self.relationships})
113 context.update({'node': node, 'attributes': self.attributes_with_node(node), 'relationships': self.relationships_with_node(node)})
114 page_about_to_render_to_string.send(sender=self, node=node, request=request, extra_context=context)
115 string = self.template.django_template.render(RequestContext(request, context))
117 page_about_to_render_to_string.send(sender=self, node=node, request=request, extra_context=context)
118 string = self.template.django_template.render(Context(context))
119 page_finished_rendering_to_string.send(sender=self, string=string)
122 def actually_render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
123 return HttpResponse(self.render_to_string(node, request, path, subpath, extra_context), mimetype=self.template.mimetype)
125 def __unicode__(self):
132 class Contentlet(models.Model):
133 page = models.ForeignKey(Page, related_name='contentlets')
134 name = models.CharField(max_length=255)
135 content = models.TextField()
136 dynamic = models.BooleanField(default=False)
138 def __unicode__(self):
145 class ContentReference(models.Model):
146 page = models.ForeignKey(Page, related_name='contentreferences')
147 name = models.CharField(max_length=255)
148 content_type = models.ForeignKey(ContentType, verbose_name='Content type')
149 content_id = models.PositiveIntegerField(verbose_name='Content ID', blank=True, null=True)
150 content = generic.GenericForeignKey('content_type', 'content_id')
152 def __unicode__(self):
159 register_templatetags('philo.templatetags.containers')
162 register_value_model(Template)
163 register_value_model(Page)