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
8 from inspect import getargspec
9 from philo.models.base import TreeEntity, Entity, QuerySetMapper
10 from philo.utils import ContentTypeSubclassLimiter
11 from philo.validators import RedirectValidator
14 _view_content_type_limiter = ContentTypeSubclassLimiter(None)
17 class Node(TreeEntity):
18 view_content_type = models.ForeignKey(ContentType, related_name='node_view_set', limit_choices_to=_view_content_type_limiter)
19 view_object_id = models.PositiveIntegerField()
20 view = generic.GenericForeignKey('view_content_type', 'view_object_id')
23 def accepts_subpath(self):
24 return self.view.accepts_subpath
26 def render_to_response(self, request, path=None, subpath=None, extra_context=None):
27 return self.view.render_to_response(self, request, path, subpath, extra_context)
33 # the following line enables the selection of a node as the root for a given django.contrib.sites Site object
34 models.ForeignKey(Node, related_name='sites', null=True, blank=True).contribute_to_class(Site, 'root_node')
38 nodes = generic.GenericRelation(Node, content_type_field='view_content_type', object_id_field='view_object_id')
40 accepts_subpath = False
42 def attributes_with_node(self, node):
43 return QuerySetMapper(self.attribute_set, passthrough=node.attributes)
45 def relationships_with_node(self, node):
46 return QuerySetMapper(self.relationship_set, passthrough=node.relationships)
48 def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
49 raise NotImplementedError('View subclasses must implement render_to_response.')
55 _view_content_type_limiter.cls = View
58 class MultiView(View):
59 accepts_subpath = True
63 def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
66 subpath = "/" + subpath
67 view, args, kwargs = resolve(subpath, urlconf=self)
68 view_args = getargspec(view)[0]
69 if extra_context is not None and 'extra_context' in view_args:
70 if 'extra_context' in kwargs:
71 extra_context.update(kwargs['extra_context'])
72 kwargs['extra_context'] = extra_context
73 if 'node' in view_args:
75 return view(request, *args, **kwargs)
86 target = models.CharField(max_length=200, validators=[RedirectValidator()])
87 status_code = models.IntegerField(choices=STATUS_CODES, default=302, verbose_name='redirect type')
89 def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
90 response = HttpResponseRedirect(self.target)
91 response.status_code = self.status_code
99 """ For storing arbitrary files """
101 mimetype = models.CharField(max_length=255)
102 file = models.FileField(upload_to='philo/files/%Y/%m/%d')
104 def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
105 wrapper = FileWrapper(self.file)
106 response = HttpResponse(wrapper, content_type=self.mimetype)
107 response['Content-Length'] = self.file.size