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