Re-tweaked page add form to preserve 'continue on normal save' functionality. This...
[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 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.models.base import TreeEntity, Entity, QuerySetMapper, register_value_model
11 from philo.utils import ContentTypeSubclassLimiter
12 from philo.validators import RedirectValidator
13 from philo.exceptions import ViewDoesNotProvideSubpaths, AncestorDoesNotExist
14
15
16 _view_content_type_limiter = ContentTypeSubclassLimiter(None)
17
18
19 class Node(TreeEntity):
20         view_content_type = models.ForeignKey(ContentType, related_name='node_view_set', limit_choices_to=_view_content_type_limiter)
21         view_object_id = models.PositiveIntegerField()
22         view = generic.GenericForeignKey('view_content_type', 'view_object_id')
23         
24         @property
25         def accepts_subpath(self):
26                 if self.view:
27                         return self.view.accepts_subpath
28                 return False
29         
30         def render_to_response(self, request, path=None, subpath=None, extra_context=None):
31                 return self.view.render_to_response(self, request, path, subpath, extra_context)
32         
33         def get_absolute_url(self):
34                 root = Site.objects.get_current().root_node
35                 try:
36                         return '/%s' % self.get_path(root=root)
37                 except AncestorDoesNotExist:
38                         return None
39         
40         class Meta:
41                 app_label = 'philo'
42
43
44 # the following line enables the selection of a node as the root for a given django.contrib.sites Site object
45 models.ForeignKey(Node, related_name='sites', null=True, blank=True).contribute_to_class(Site, 'root_node')
46
47
48 class View(Entity):
49         nodes = generic.GenericRelation(Node, content_type_field='view_content_type', object_id_field='view_object_id')
50         
51         accepts_subpath = False
52         
53         def get_subpath(self, obj):
54                 raise ViewDoesNotProvideSubpaths
55         
56         def attributes_with_node(self, node):
57                 return QuerySetMapper(self.attribute_set, passthrough=node.attributes)
58         
59         def relationships_with_node(self, node):
60                 return QuerySetMapper(self.relationship_set, passthrough=node.relationships)
61         
62         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
63                 raise NotImplementedError('View subclasses must implement render_to_response.')
64         
65         class Meta:
66                 abstract = True
67
68
69 _view_content_type_limiter.cls = View
70
71
72 class MultiView(View):
73         accepts_subpath = True
74         
75         urlpatterns = []
76         
77         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
78                 clear_url_caches()
79                 if not subpath:
80                         subpath = ""
81                 subpath = "/" + subpath
82                 view, args, kwargs = resolve(subpath, urlconf=self)
83                 view_args = getargspec(view)
84                 if extra_context is not None and ('extra_context' in view_args[0] or view_args[2] is not None):
85                         if 'extra_context' in kwargs:
86                                 extra_context.update(kwargs['extra_context'])
87                         kwargs['extra_context'] = extra_context
88                 if 'node' in view_args[0] or view_args[2] is not None:
89                         kwargs['node'] = node
90                 return view(request, *args, **kwargs)
91         
92         class Meta:
93                 abstract = True
94
95
96 class Redirect(View):
97         STATUS_CODES = (
98                 (302, 'Temporary'),
99                 (301, 'Permanent'),
100         )
101         target = models.CharField(max_length=200, validators=[RedirectValidator()])
102         status_code = models.IntegerField(choices=STATUS_CODES, default=302, verbose_name='redirect type')
103         
104         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
105                 response = HttpResponseRedirect(self.target)
106                 response.status_code = self.status_code
107                 return response
108         
109         class Meta:
110                 app_label = 'philo'
111
112
113 class File(View):
114         """ For storing arbitrary files """
115         
116         mimetype = models.CharField(max_length=255)
117         file = models.FileField(upload_to='philo/files/%Y/%m/%d')
118         
119         def render_to_response(self, node, request, path=None, subpath=None, extra_context=None):
120                 wrapper = FileWrapper(self.file)
121                 response = HttpResponse(wrapper, content_type=self.mimetype)
122                 response['Content-Length'] = self.file.size
123                 return response
124         
125         class Meta:
126                 app_label = 'philo'
127         
128         def __unicode__(self):
129                 return self.file.name
130
131
132 register_templatetags('philo.templatetags.nodes')
133 register_value_model(Node)