1 from django.contrib.auth.decorators import login_required
2 from django.utils.decorators import method_decorator
3 from django.http import HttpResponseRedirect
4 # for 'as_view' that we need to call in urls.py and the like
5 from django.views.generic.base import TemplateView
7 from manifold.manifoldresult import ManifoldException
11 # the implementation of the classes in this file rely on redefining 'dispatch'
12 # for this reason if you inherit any of these, please do not redefine 'dispatch' yourself,
13 # but rather redefine 'get' and 'post' instead
16 ########## the base class for views that require a login
17 class LoginRequiredView (TemplateView):
19 @method_decorator(login_required)
20 def dispatch(self, request, *args, **kwargs):
21 return super(LoginRequiredView, self).dispatch(request, *args, **kwargs)
24 ########## the base class for views that need to protect against ManifoldException
25 # a decorator for view classes to catch manifold exceptions
26 # by design views should not directly exercise a manifold query
27 # given that these are asynchroneous, you would expect a view to just
28 # return a mundane skeleton
29 # however of course this is not always true,
30 # e.g. we deal with metadata some other way, and so
31 # it is often a good idea for a view to monitor these exceptions
32 # and to take this opportunity to logout people
34 def logout_on_manifold_exception (fun_that_returns_httpresponse):
35 def wrapped (request, *args, **kwds):
36 # print 'wrapped by logout_on_manifold_exception'
38 return fun_that_returns_httpresponse(request,*args, **kwds)
39 except ManifoldException, manifold_result:
40 # xxx we need a means to display this message to user...
41 from django.contrib.auth import logout
42 # in some unusual cases, this might fail
45 return HttpResponseRedirect ('/')
47 # xxx we need to sugarcoat this error message in some error template...
48 print "Unexpected exception",e
51 return HttpResponseRedirect ('/')
54 # at first sight this matters only for views that require login
55 # so for now we expose a single class that behaves like
56 # login_required + logout_on_manifold_exception
57 class LoginRequiredAutoLogoutView (TemplateView):
59 @logout_on_manifold_exception
60 @method_decorator(login_required)
61 def dispatch (self, request, *args, **kwds):
62 return super(LoginRequiredAutoLogoutView,self).dispatch(request,*args,**kwds)
64 # we have more and more views that actually send manifold queries
65 # so for these we need to protect against manifold exceptions
66 # even though login is not required
67 class FreeAccessView (TemplateView):
69 def dispatch (self, request, *args, **kwds):
70 return super(FreeAccessView,self).dispatch(request,*args,**kwds)