1 from django.db import models
2 from django.contrib.contenttypes.models import ContentType
3 from django.contrib.contenttypes import generic
4 from django.contrib.sites.models import Site
5 from django.http import HttpResponse, HttpResponseServerError, HttpResponseRedirect
6 from django.core.servers.basehttp import FileWrapper
7 from django.core.urlresolvers import resolve, clear_url_caches
8 from django.template import add_to_builtins as register_templatetags
9 from inspect import getargspec
10 from philo.exceptions import MIDDLEWARE_NOT_CONFIGURED
11 from philo.models.base import TreeEntity, Entity, QuerySetMapper, register_value_model
12 from philo.utils import ContentTypeSubclassLimiter
13 from philo.validators import RedirectValidator
14 from philo.exceptions import ViewDoesNotProvideSubpaths, AncestorDoesNotExist
15 from philo.signals import view_about_to_render, view_finished_rendering
18 _view_content_type_limiter = ContentTypeSubclassLimiter(None)
21 class Node(TreeEntity):
22 view_content_type = models.ForeignKey(ContentType, related_name='node_view_set', limit_choices_to=_view_content_type_limiter)
23 view_object_id = models.PositiveIntegerField()
24 view = generic.GenericForeignKey('view_content_type', 'view_object_id')
27 def accepts_subpath(self):
29 return self.view.accepts_subpath
32 def render_to_response(self, request, extra_context=None):
33 return self.view.render_to_response(request, extra_context)
35 def get_absolute_url(self):
36 root = Site.objects.get_current().root_node
38 return '/%s' % self.get_path(root=root)
39 except AncestorDoesNotExist:
46 # the following line enables the selection of a node as the root for a given django.contrib.sites Site object
47 models.ForeignKey(Node, related_name='sites', null=True, blank=True).contribute_to_class(Site, 'root_node')
51 nodes = generic.GenericRelation(Node, content_type_field='view_content_type', object_id_field='view_object_id')
53 accepts_subpath = False
55 def get_subpath(self, obj):
56 raise ViewDoesNotProvideSubpaths
58 def attributes_with_node(self, node):
59 return QuerySetMapper(self.attribute_set, passthrough=node.attributes)
61 def render_to_response(self, request, extra_context=None):
62 if not hasattr(request, 'node'):
63 raise MIDDLEWARE_NOT_CONFIGURED
65 extra_context = extra_context or {}
66 view_about_to_render.send(sender=self, request=request, extra_context=extra_context)
67 response = self.actually_render_to_response(request, extra_context)
68 view_finished_rendering.send(sender=self, response=response)
71 def actually_render_to_response(self, request, extra_context=None):
72 raise NotImplementedError('View subclasses must implement render_to_response.')
78 _view_content_type_limiter.cls = View
81 class MultiView(View):
82 accepts_subpath = True
86 def actually_render_to_response(self, request, extra_context=None):
88 subpath = request.node.subpath
91 subpath = "/" + subpath
92 view, args, kwargs = resolve(subpath, urlconf=self)
93 view_args = getargspec(view)
94 if extra_context is not None and ('extra_context' in view_args[0] or view_args[2] is not None):
95 if 'extra_context' in kwargs:
96 extra_context.update(kwargs['extra_context'])
97 kwargs['extra_context'] = extra_context
98 return view(request, *args, **kwargs)
104 class Redirect(View):
109 target = models.CharField(max_length=200, validators=[RedirectValidator()])
110 status_code = models.IntegerField(choices=STATUS_CODES, default=302, verbose_name='redirect type')
112 def actually_render_to_response(self, request, extra_context=None):
113 response = HttpResponseRedirect(self.target)
114 response.status_code = self.status_code
121 # Why does this exist?
123 """ For storing arbitrary files """
125 mimetype = models.CharField(max_length=255)
126 file = models.FileField(upload_to='philo/files/%Y/%m/%d')
128 def actually_render_to_response(self, request, extra_context=None):
129 wrapper = FileWrapper(self.file)
130 response = HttpResponse(wrapper, content_type=self.mimetype)
131 response['Content-Length'] = self.file.size
137 def __unicode__(self):
138 return self.file.name
141 register_templatetags('philo.templatetags.nodes')
142 register_value_model(Node)