Refactored Node.get_absolute_url and related functions (such as MultiView.reverse...
[philo.git] / views.py
1 from django.conf import settings
2 from django.core.urlresolvers import resolve
3 from django.http import Http404, HttpResponseRedirect
4 from django.views.decorators.vary import vary_on_headers
5 from philo.exceptions import MIDDLEWARE_NOT_CONFIGURED
6
7
8 @vary_on_headers('Accept')
9 def node_view(request, path=None, **kwargs):
10         if "philo.middleware.RequestNodeMiddleware" not in settings.MIDDLEWARE_CLASSES:
11                 raise MIDDLEWARE_NOT_CONFIGURED
12         
13         if not request.node:
14                 raise Http404
15         
16         node = request.node
17         subpath = request.node.subpath
18         
19         if not node.handles_subpath(subpath):
20                 # If the subpath isn't handled, check settings.APPEND_SLASH. If
21                 # it's True, try to correct the subpath.
22                 if not settings.APPEND_SLASH:
23                         raise Http404
24                 
25                 if subpath[-1] == "/":
26                         subpath = subpath[:-1]
27                 else:
28                         subpath += "/"
29                 
30                 redirect_url = node.construct_url(subpath)
31                 
32                 if node.handles_subpath(subpath):
33                         return HttpResponseRedirect(redirect_url)
34                 
35                 # Perhaps there is a non-philo view at this address. Can we
36                 # resolve *something* there besides node_view? If not,
37                 # raise a 404.
38                 view, args, kwargs = resolve(redirect_url)
39                 
40                 if view == node_view:
41                         raise Http404
42                 else:
43                         return HttpResponseRedirect(redirect_url)
44         
45         return node.render_to_response(request, kwargs)