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