9c18e52b19aeff295fe1b02c6d804e9088ee857e
[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
52
53 _view_content_type_limiter.cls = View
54
55
56 class MultiView(View):
57         accepts_subpath = True
58         
59         urlpatterns = []
60         
61         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
62                 if not subpath:
63                         subpath = ""
64                 subpath = "/" + subpath
65                 from django.core.urlresolvers import resolve
66                 view, args, kwargs = resolve(subpath, urlconf=self)
67                 return view(request, *args, **kwargs)
68         
69         class Meta:
70                 abstract = True
71
72
73 class Redirect(View):
74         STATUS_CODES = (
75                 (302, 'Temporary'),
76                 (301, 'Permanent'),
77         )
78         target = models.CharField(max_length=200, validators=[RedirectValidator()])
79         status_code = models.IntegerField(choices=STATUS_CODES, default=302, verbose_name='redirect type')
80         
81         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
82                 response = HttpResponseRedirect(self.target)
83                 response.status_code = self.status_code
84                 return response
85         
86         class Meta:
87                 app_label = 'philo'
88
89
90 class File(View):
91         """ For storing arbitrary files """
92         
93         mimetype = models.CharField(max_length=255)
94         file = models.FileField(upload_to='philo/files/%Y/%m/%d')
95         
96         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
97                 wrapper = FileWrapper(self.file)
98                 response = HttpResponse(wrapper, content_type=self.mimetype)
99                 response['Content-Length'] = self.file.size
100                 return response
101         
102         class Meta:
103                 app_label = 'philo'