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
6 from philo.exceptions import MIDDLEWARE_NOT_CONFIGURED
9 @vary_on_headers('Accept')
10 def node_view(request, path=None, **kwargs):
12 :func:`node_view` handles incoming requests by checking to make sure that:
14 - the request has an attached :class:`.Node`.
15 - the attached :class:`~philo.models.nodes.Node` handles any remaining path beyond its location.
17 If these conditions are not met, then :func:`node_view` will either raise :exc:`Http404` or, if it seems like the address was mistyped (for example missing a trailing slash), return an :class:`HttpResponseRedirect` to the correct address.
19 Otherwise, :func:`node_view` will call the :class:`.Node`'s :meth:`~.Node.render_to_response` method, passing ``kwargs`` in as the ``extra_context``.
22 if "philo.middleware.RequestNodeMiddleware" not in settings.MIDDLEWARE_CLASSES:
23 raise MIDDLEWARE_NOT_CONFIGURED
26 if settings.APPEND_SLASH and request.path != "/":
34 view, args, kwargs = resolve(path)
36 return HttpResponseRedirect(path)
40 subpath = request.node._subpath
42 # Explicitly disallow trailing slashes if we are otherwise at a node's url.
43 if node._path != "/" and node._path[-1] == "/" and subpath == "/":
44 return HttpResponseRedirect(node.get_absolute_url())
46 if not node.handles_subpath(subpath):
47 # If the subpath isn't handled, check settings.APPEND_SLASH. If
48 # it's True, try to correct the subpath.
49 if not settings.APPEND_SLASH:
52 if subpath[-1] == "/":
53 subpath = subpath[:-1]
57 redirect_url = node.construct_url(subpath)
59 if node.handles_subpath(subpath):
60 return HttpResponseRedirect(redirect_url)
62 # Perhaps there is a non-philo view at this address. Can we
63 # resolve *something* there besides node_view? If not,
65 view, args, kwargs = resolve(redirect_url)
70 return HttpResponseRedirect(redirect_url)
72 return node.render_to_response(request, kwargs)