From d9007d388ac908beabd142d7c4738d5eb7b9230d Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Tue, 31 Aug 2010 11:34:18 -0400 Subject: [PATCH] Switched over to render_to_response and actually_render_to_response, since render_to_response is the open api used by say multiviews and should always be called when a page is to render. Calling a hidden method would not be good. Also added render_to_string listeners for pages. --- models/nodes.py | 14 +++++++------- models/pages.py | 12 +++++++++--- signals.py | 4 +++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/models/nodes.py b/models/nodes.py index b633542..14f5063 100644 --- a/models/nodes.py +++ b/models/nodes.py @@ -29,7 +29,7 @@ class Node(TreeEntity): return False def render_to_response(self, request, path=None, subpath=None, extra_context=None): - return self.view._render_to_response(self, request, path, subpath, extra_context) + return self.view.render_to_response(self, request, path, subpath, extra_context) def get_absolute_url(self): root = Site.objects.get_current().root_node @@ -60,14 +60,14 @@ class View(Entity): def relationships_with_node(self, node): return QuerySetMapper(self.relationship_set, passthrough=node.relationships) - def _render_to_response(self, node, request, path=None, subpath=None, extra_context=None): + def render_to_response(self, node, request, path=None, subpath=None, extra_context=None): extra_context = extra_context or {} view_about_to_render.send(sender=self, node=node, request=request, path=path, subpath=subpath, extra_context=extra_context) - response = self.render_to_response(node, request, path, subpath, extra_context) + response = self.actually_render_to_response(node, request, path, subpath, extra_context) view_finished_rendering.send(sender=self, response=response) return response - def render_to_response(self, node, request, path=None, subpath=None, extra_context=None): + def actually_render_to_response(self, node, request, path=None, subpath=None, extra_context=None): raise NotImplementedError('View subclasses must implement render_to_response.') class Meta: @@ -82,7 +82,7 @@ class MultiView(View): urlpatterns = [] - def render_to_response(self, node, request, path=None, subpath=None, extra_context=None): + def actually_render_to_response(self, node, request, path=None, subpath=None, extra_context=None): clear_url_caches() if not subpath: subpath = "" @@ -109,7 +109,7 @@ class Redirect(View): target = models.CharField(max_length=200, validators=[RedirectValidator()]) status_code = models.IntegerField(choices=STATUS_CODES, default=302, verbose_name='redirect type') - def render_to_response(self, node, request, path=None, subpath=None, extra_context=None): + def actually_render_to_response(self, node, request, path=None, subpath=None, extra_context=None): response = HttpResponseRedirect(self.target) response.status_code = self.status_code return response @@ -124,7 +124,7 @@ class File(View): mimetype = models.CharField(max_length=255) file = models.FileField(upload_to='philo/files/%Y/%m/%d') - def render_to_response(self, node, request, path=None, subpath=None, extra_context=None): + def actually_render_to_response(self, node, request, path=None, subpath=None, extra_context=None): wrapper = FileWrapper(self.file) response = HttpResponse(wrapper, content_type=self.mimetype) response['Content-Length'] = self.file.size diff --git a/models/pages.py b/models/pages.py index 4260761..7b4415d 100644 --- a/models/pages.py +++ b/models/pages.py @@ -14,6 +14,7 @@ from philo.models.base import TreeModel, register_value_model from philo.models.nodes import View from philo.utils import fattr from philo.templatetags.containers import ContainerNode +from philo.signals import page_about_to_render_to_string, page_finished_rendering_to_string class Template(TreeModel): @@ -110,10 +111,15 @@ class Page(View): context.update({'page': self, 'attributes': self.attributes, 'relationships': self.relationships}) if node and request: context.update({'node': node, 'attributes': self.attributes_with_node(node), 'relationships': self.relationships_with_node(node)}) - return self.template.django_template.render(RequestContext(request, context)) - return self.template.django_template.render(Context(context)) + page_about_to_render_to_string.send(sender=self, node=node, request=request, extra_context=context) + string = self.template.django_template.render(RequestContext(request, context)) + else: + page_about_to_render_to_string.send(sender=self, node=node, request=request, extra_context=context) + string = self.template.django_template.render(Context(context)) + page_finished_rendering_to_string.send(sender=self, string=string) + return string - def render_to_response(self, node, request, path=None, subpath=None, extra_context=None): + def actually_render_to_response(self, node, request, path=None, subpath=None, extra_context=None): return HttpResponse(self.render_to_string(node, request, path, subpath, extra_context), mimetype=self.template.mimetype) def __unicode__(self): diff --git a/signals.py b/signals.py index 372eb1b..d2fb26f 100644 --- a/signals.py +++ b/signals.py @@ -3,4 +3,6 @@ from django.dispatch import Signal entity_class_prepared = Signal(providing_args=['class']) view_about_to_render = Signal(providing_args=['node', 'request', 'path', 'subpath', 'extra_context']) -view_finished_rendering = Signal(providing_args=['response']) \ No newline at end of file +view_finished_rendering = Signal(providing_args=['response']) +page_about_to_render_to_string = Signal(providing_args=['node', 'request', 'path', 'subpath', 'extra_context']) +page_finished_rendering_to_string = Signal(providing_args=['string']) \ No newline at end of file -- 2.20.1