9eb2bd0381d727224ed746ba7ca7b1a4f8bb3b5b
[philo.git] / models / nodes.py
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 philo.models.base import TreeEntity, Entity, QuerySetMapper
8 from philo.utils import ContentTypeSubclassLimiter
9 from philo.validators import RedirectValidator
10
11
12 _view_content_type_limiter = ContentTypeSubclassLimiter(None)
13
14
15 class Node(TreeEntity):
16         view_content_type = models.ForeignKey(ContentType, related_name='node_view_set', limit_choices_to=_view_content_type_limiter)
17         view_object_id = models.PositiveIntegerField()
18         view = generic.GenericForeignKey('view_content_type', 'view_object_id')
19         
20         @property
21         def accepts_subpath(self):
22                 return self.view.accepts_subpath
23         
24         def render_to_response(self, request, path=None, subpath=None, extra_context=None):
25                 return self.view.render_to_response(self, request, path, subpath, extra_context)
26         
27         class Meta:
28                 app_label = 'philo'
29
30
31 # the following line enables the selection of a node as the root for a given django.contrib.sites Site object
32 models.ForeignKey(Node, related_name='sites', null=True, blank=True).contribute_to_class(Site, 'root_node')
33
34
35 class View(Entity):
36         nodes = generic.GenericRelation(Node, content_type_field='view_content_type', object_id_field='view_object_id')
37         
38         accepts_subpath = False
39         
40         def attributes_with_node(self, node):
41                 return QuerySetMapper(self.attribute_set, passthrough=node.attributes)
42         
43         def relationships_with_node(self, node):
44                 return QuerySetMapper(self.relationship_set, passthrough=node.relationships)
45         
46         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
47                 raise NotImplementedError('View subclasses must implement render_to_response.')
48         
49         class Meta:
50                 abstract = True
51                 app_label = 'philo'
52
53
54 _view_content_type_limiter.cls = View
55
56
57 class MultiView(View):
58         accepts_subpath = True
59         
60         urlpatterns = []
61         
62         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
63                 if not subpath:
64                         subpath = ""
65                 subpath = "/" + subpath
66                 from django.core.urlresolvers import resolve
67                 view, args, kwargs = resolve(subpath, urlconf=self)
68                 return view(request, *args, **kwargs)
69         
70         class Meta:
71                 abstract = True
72                 app_label = 'philo'
73
74
75 class Redirect(View):
76         STATUS_CODES = (
77                 (302, 'Temporary'),
78                 (301, 'Permanent'),
79         )
80         target = models.CharField(max_length=200, validators=[RedirectValidator()])
81         status_code = models.IntegerField(choices=STATUS_CODES, default=302, verbose_name='redirect type')
82         
83         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
84                 response = HttpResponseRedirect(self.target)
85                 response.status_code = self.status_code
86                 return response
87         
88         class Meta:
89                 app_label = 'philo'
90
91
92 class File(View):
93         """ For storing arbitrary files """
94         
95         mimetype = models.CharField(max_length=255)
96         file = models.FileField(upload_to='philo/files/%Y/%m/%d')
97         
98         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
99                 wrapper = FileWrapper(self.file)
100                 response = HttpResponse(wrapper, content_type=self.mimetype)
101                 response['Content-Length'] = self.file.size
102                 return response
103         
104         class Meta:
105                 app_label = 'philo'