2 from django.conf import settings
3 from django.contrib.contenttypes.models import ContentType
4 from django.contrib.contenttypes import generic
5 from django.db import models
6 from django.http import HttpResponse
7 from django.template import TemplateDoesNotExist, Context, RequestContext, Template as DjangoTemplate, add_to_builtins as register_templatetags
8 from philo.models.base import TreeModel, register_value_model
9 from philo.models.fields import TemplateField
10 from philo.models.nodes import View
11 from philo.templatetags.containers import ContainerNode
12 from philo.utils import fattr, nodelist_crawl
13 from philo.validators import LOADED_TEMPLATE_ATTR
14 from philo.signals import page_about_to_render_to_string, page_finished_rendering_to_string
17 class Template(TreeModel):
18 name = models.CharField(max_length=255)
19 documentation = models.TextField(null=True, blank=True)
20 mimetype = models.CharField(max_length=255, default=getattr(settings, 'DEFAULT_CONTENT_TYPE', 'text/html'))
21 code = TemplateField(secure=False, verbose_name='django template code')
25 return 'philo.models.Template: ' + self.path
28 def django_template(self):
29 return DjangoTemplate(self.code)
34 Returns a tuple where the first item is a list of names of contentlets referenced by containers,
35 and the second item is a list of tuples of names and contenttypes of contentreferences referenced by containers.
36 This will break if there is a recursive extends or includes in the template code.
37 Due to the use of an empty Context, any extends or include tags with dynamic arguments probably won't work.
39 def process_node(node, nodes):
40 if isinstance(node, ContainerNode):
43 all_nodes = nodelist_crawl(self.django_template.nodelist, process_node)
44 contentlet_node_names = set([node.name for node in all_nodes if not node.references])
45 contentreference_node_names = []
46 contentreference_node_specs = []
47 for node in all_nodes:
48 if node.references and node.name not in contentreference_node_names:
49 contentreference_node_specs.append((node.name, node.references))
50 contentreference_node_names.append(node.name)
51 return contentlet_node_names, contentreference_node_specs
53 def __unicode__(self):
54 return self.get_path(pathsep=u' › ', field='name')
57 @fattr(is_usable=True)
58 def loader(template_name, template_dirs=None): # load_template_source
60 template = Template.objects.get_with_path(template_name)
61 except Template.DoesNotExist:
62 raise TemplateDoesNotExist(template_name)
63 return (template.code, template.origin)
71 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.
73 template = models.ForeignKey(Template, related_name='pages')
74 title = models.CharField(max_length=255)
76 def get_containers(self):
77 if not hasattr(self, '_containers'):
78 self._containers = self.template.containers
79 return self._containers
80 containers = property(get_containers)
82 def render_to_string(self, node=None, request=None, path=None, subpath=None, extra_context=None):
84 context.update(extra_context or {})
85 context.update({'page': self, 'attributes': self.attributes, 'relationships': self.relationships})
87 context.update({'node': node, 'attributes': self.attributes_with_node(node), 'relationships': self.relationships_with_node(node)})
88 page_about_to_render_to_string.send(sender=self, node=node, request=request, extra_context=context)
89 string = self.template.django_template.render(RequestContext(request, context))
91 page_about_to_render_to_string.send(sender=self, node=node, request=request, extra_context=context)
92 string = self.template.django_template.render(Context(context))
93 page_finished_rendering_to_string.send(sender=self, string=string)
96 def actually_render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
97 return HttpResponse(self.render_to_string(node, request, path, subpath, extra_context), mimetype=self.template.mimetype)
99 def __unicode__(self):
106 class Contentlet(models.Model):
107 page = models.ForeignKey(Page, related_name='contentlets')
108 name = models.CharField(max_length=255)
109 content = TemplateField()
111 def __unicode__(self):
118 class ContentReference(models.Model):
119 page = models.ForeignKey(Page, related_name='contentreferences')
120 name = models.CharField(max_length=255)
121 content_type = models.ForeignKey(ContentType, verbose_name='Content type')
122 content_id = models.PositiveIntegerField(verbose_name='Content ID', blank=True, null=True)
123 content = generic.GenericForeignKey('content_type', 'content_id')
125 def __unicode__(self):
132 register_templatetags('philo.templatetags.containers')
135 register_value_model(Template)
136 register_value_model(Page)