2 from django.conf import settings
3 from django.contrib.contenttypes.models import ContentType
4 from django.contrib.contenttypes import generic
5 from django.core.exceptions import ValidationError
6 from django.db import models
7 from django.http import HttpResponse
8 from django.template import TemplateDoesNotExist, Context, RequestContext, Template as DjangoTemplate, add_to_builtins as register_templatetags
9 from philo.models.base import TreeModel, register_value_model
10 from philo.models.fields import TemplateField
11 from philo.models.nodes import View
12 from philo.templatetags.containers import ContainerNode
13 from philo.utils import fattr, nodelist_crawl
14 from philo.validators import LOADED_TEMPLATE_ATTR
15 from philo.signals import page_about_to_render_to_string, page_finished_rendering_to_string
18 class Template(TreeModel):
19 name = models.CharField(max_length=255)
20 documentation = models.TextField(null=True, blank=True)
21 mimetype = models.CharField(max_length=255, default=getattr(settings, 'DEFAULT_CONTENT_TYPE', 'text/html'))
22 code = TemplateField(secure=False, verbose_name='django template code')
27 Returns a tuple where the first item is a list of names of contentlets referenced by containers,
28 and the second item is a list of tuples of names and contenttypes of contentreferences referenced by containers.
29 This will break if there is a recursive extends or includes in the template code.
30 Due to the use of an empty Context, any extends or include tags with dynamic arguments probably won't work.
32 def process_node(node, nodes):
33 if isinstance(node, ContainerNode):
36 all_nodes = nodelist_crawl(DjangoTemplate(self.code).nodelist, process_node)
37 contentlet_node_names = set([node.name for node in all_nodes if not node.references])
38 contentreference_node_names = []
39 contentreference_node_specs = []
40 for node in all_nodes:
41 if node.references and node.name not in contentreference_node_names:
42 contentreference_node_specs.append((node.name, node.references))
43 contentreference_node_names.append(node.name)
44 return contentlet_node_names, contentreference_node_specs
46 def __unicode__(self):
47 return self.get_path(pathsep=u' › ', field='name')
55 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.
57 template = models.ForeignKey(Template, related_name='pages')
58 title = models.CharField(max_length=255)
60 def get_containers(self):
61 if not hasattr(self, '_containers'):
62 self._containers = self.template.containers
63 return self._containers
64 containers = property(get_containers)
66 def render_to_string(self, request=None, extra_context=None):
68 context.update(extra_context or {})
69 context.update({'page': self, 'attributes': self.attributes})
70 template = DjangoTemplate(self.template.code)
72 context.update({'node': request.node, 'attributes': self.attributes_with_node(request.node)})
73 page_about_to_render_to_string.send(sender=self, request=request, extra_context=context)
74 string = template.render(RequestContext(request, context))
76 page_about_to_render_to_string.send(sender=self, request=request, extra_context=context)
77 string = template.render(Context(context))
78 page_finished_rendering_to_string.send(sender=self, string=string)
81 def actually_render_to_response(self, request, extra_context=None):
82 return HttpResponse(self.render_to_string(request, extra_context), mimetype=self.template.mimetype)
84 def __unicode__(self):
87 def clean_fields(self, exclude=None):
89 super(Page, self).clean_fields(exclude)
90 except ValidationError, e:
91 errors = e.message_dict
95 if 'template' not in errors and 'template' not in exclude:
97 self.template.clean_fields()
99 except ValidationError, e:
100 errors['template'] = e.messages
103 raise ValidationError(errors)
109 class Contentlet(models.Model):
110 page = models.ForeignKey(Page, related_name='contentlets')
111 name = models.CharField(max_length=255, db_index=True)
112 content = TemplateField()
114 def __unicode__(self):
121 class ContentReference(models.Model):
122 page = models.ForeignKey(Page, related_name='contentreferences')
123 name = models.CharField(max_length=255, db_index=True)
124 content_type = models.ForeignKey(ContentType, verbose_name='Content type')
125 content_id = models.PositiveIntegerField(verbose_name='Content ID', blank=True, null=True)
126 content = generic.GenericForeignKey('content_type', 'content_id')
128 def __unicode__(self):
135 register_templatetags('philo.templatetags.containers')
138 register_value_model(Template)
139 register_value_model(Page)