Modified the feed behavior for blogs and newsletters to always use the full content...
[philo.git] / middleware.py
1 from django.conf import settings
2 from django.contrib.sites.models import Site
3 from django.http import Http404
4 from philo.models import Node, View
5
6
7 class LazyNode(object):
8         def __get__(self, request, obj_type=None):
9                 if not hasattr(request, '_cached_node_path'):
10                         return None
11                 
12                 if not hasattr(request, '_found_node'):
13                         try:
14                                 current_site = Site.objects.get_current()
15                         except Site.DoesNotExist:
16                                 current_site = None
17                         
18                         try:
19                                 node, subpath = Node.objects.get_with_path(request._cached_node_path, root=getattr(current_site, 'root_node', None), absolute_result=False)
20                         except Node.DoesNotExist:
21                                 node = None
22                         
23                         if node:
24                                 node.subpath = subpath
25                         
26                         request._found_node = node
27                 
28                 return request._found_node
29
30
31 class RequestNodeMiddleware(object):
32         """Middleware to process the request's path and attach the closest ancestor node."""
33         def process_request(self, request):
34                 request.__class__.node = LazyNode()
35         
36         def process_view(self, request, view_func, view_args, view_kwargs):
37                 request._cached_node_path = view_kwargs.get('path', '/')
38         
39         def process_exception(self, request, exception):
40                 if settings.DEBUG or not hasattr(request, 'node') or not request.node:
41                         return
42                 
43                 if isinstance(exception, Http404):
44                         error_view = request.node.attributes.get('Http404', None)
45                 else:
46                         error_view = request.node.attributes.get('Http500', None)
47                 
48                 if error_view is None or not isinstance(error_view, View):
49                         # Should this be duck-typing? Perhaps even no testing?
50                         return
51                 
52                 extra_context = {'exception': exception}
53                 return error_view.render_to_response(request, extra_context)