1 from django.contrib.sites.models import Site
2 from philo.models import Node
5 class LazyNode(object):
6 def __get__(self, request, obj_type=None):
7 if not hasattr(request, '_cached_node_path'):
10 if not hasattr(request, '_found_node'):
12 current_site = Site.objects.get_current()
13 except Site.DoesNotExist:
17 node, subpath = Node.objects.get_with_path(request._cached_node_path, root=getattr(current_site, 'root_node', None), absolute_result=False)
18 except Node.DoesNotExist:
22 node.subpath = subpath
24 request._found_node = node
26 return request._found_node
29 class RequestNodeMiddleware(object):
30 """Middleware to process the request's path and attach the closest ancestor node."""
31 def process_request(self, request):
32 request.__class__.node = LazyNode()
34 def process_view(self, request, view_func, view_args, view_kwargs):
35 request._cached_node_path = view_kwargs.get('path', '/')