From b2f456977cd5884c625f704e024213ad86163929 Mon Sep 17 00:00:00 2001 From: Joseph Spiros Date: Sat, 26 Jun 2010 09:06:18 -0400 Subject: [PATCH] Adding extra_context to render_to_response on Nodes and Views, adding (attributes|relationships)_with_node to Views, and implemented usage of both additions in Page. --- models/base.py | 8 +++++--- models/nodes.py | 20 +++++++++++++------- models/pages.py | 7 +++++-- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/models/base.py b/models/base.py index 291fe70..f062e2c 100644 --- a/models/base.py +++ b/models/base.py @@ -64,17 +64,19 @@ class QuerySetMapper(object, DictMixin): def __init__(self, queryset, passthrough=None): self.queryset = queryset self.passthrough = passthrough + def __getitem__(self, key): try: return self.queryset.get(key__exact=key).value except ObjectDoesNotExist: - if self.passthrough: + if self.passthrough is not None: return self.passthrough.__getitem__(key) raise KeyError + def keys(self): keys = set(self.queryset.values_list('key', flat=True).distinct()) - if self.passthrough: - keys += set(self.passthrough.keys()) + if self.passthrough is not None: + keys |= set(self.passthrough.keys()) return list(keys) diff --git a/models/nodes.py b/models/nodes.py index bb1601e..9eb2bd0 100644 --- a/models/nodes.py +++ b/models/nodes.py @@ -4,7 +4,7 @@ from django.contrib.contenttypes import generic from django.contrib.sites.models import Site from django.http import HttpResponse, HttpResponseServerError, HttpResponseRedirect from django.core.servers.basehttp import FileWrapper -from philo.models.base import TreeEntity, Entity +from philo.models.base import TreeEntity, Entity, QuerySetMapper from philo.utils import ContentTypeSubclassLimiter from philo.validators import RedirectValidator @@ -21,8 +21,8 @@ class Node(TreeEntity): def accepts_subpath(self): return self.view.accepts_subpath - def render_to_response(self, request, path=None, subpath=None): - return self.view.render_to_response(self, request, path, subpath) + 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) class Meta: app_label = 'philo' @@ -37,7 +37,13 @@ class View(Entity): accepts_subpath = False - def render_to_response(self, node, request, path=None, subpath=None): + def attributes_with_node(self, node): + return QuerySetMapper(self.attribute_set, passthrough=node.attributes) + + 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): raise NotImplementedError('View subclasses must implement render_to_response.') class Meta: @@ -53,7 +59,7 @@ class MultiView(View): urlpatterns = [] - def render_to_response(self, node, request, path=None, subpath=None): + def render_to_response(self, node, request, path=None, subpath=None, extra_context=None): if not subpath: subpath = "" subpath = "/" + subpath @@ -74,7 +80,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): + def 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 @@ -89,7 +95,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): + def 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 b9e481b..354293f 100644 --- a/models/pages.py +++ b/models/pages.py @@ -97,8 +97,11 @@ class Page(View): template = models.ForeignKey(Template, related_name='pages') title = models.CharField(max_length=255) - def render_to_response(self, node, request, path=None, subpath=None): - return HttpResponse(self.template.django_template.render(RequestContext(request, {'page': self})), mimetype=self.template.mimetype) + def render_to_response(self, node, request, path=None, subpath=None, extra_context=None): + context = {} + context.update(extra_context or {}) + context.update({'page': self, 'attributes': self.attributes_with_node(node), 'relationships': self.relationships_with_node(node)}) + return HttpResponse(self.template.django_template.render(RequestContext(request, context)), mimetype=self.template.mimetype) def __unicode__(self): return self.title -- 2.20.1