Merge branch 'master' into gilbert
[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                 if settings.APPEND_SLASH and request.path != "/":
15                         path = request.path
16                         
17                         if path[-1] == "/":
18                                 path = path[:-1]
19                         else:
20                                 path += "/"
21                         
22                         view, args, kwargs = resolve(path)
23                         if view != node_view:
24                                 return HttpResponseRedirect(path)
25                 raise Http404
26         
27         node = request.node
28         subpath = request.node.subpath
29         
30         # Explicitly disallow trailing slashes if we are otherwise at a node's url.
31         if request.path and request.path != "/" and request.path[-1] == "/" and subpath == "/":
32                 return HttpResponseRedirect(node.get_absolute_url())
33         
34         if not node.handles_subpath(subpath):
35                 # If the subpath isn't handled, check settings.APPEND_SLASH. If
36                 # it's True, try to correct the subpath.
37                 if not settings.APPEND_SLASH:
38                         raise Http404
39                 
40                 if subpath[-1] == "/":
41                         subpath = subpath[:-1]
42                 else:
43                         subpath += "/"
44                 
45                 redirect_url = node.construct_url(subpath)
46                 
47                 if node.handles_subpath(subpath):
48                         return HttpResponseRedirect(redirect_url)
49                 
50                 # Perhaps there is a non-philo view at this address. Can we
51                 # resolve *something* there besides node_view? If not,
52                 # raise a 404.
53                 view, args, kwargs = resolve(redirect_url)
54                 
55                 if view == node_view:
56                         raise Http404
57                 else:
58                         return HttpResponseRedirect(redirect_url)
59         
60         return node.render_to_response(request, kwargs)