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
9 ########## the base class for views that require a login
10 class LoginRequiredView (TemplateView):
12 @method_decorator(login_required)
13 def dispatch(self, request, *args, **kwargs):
14 return super(LoginRequiredView, self).dispatch(request, *args, **kwargs)
17 ########## the base class for views that need to protect against ManifoldException
18 # a decorator for view classes to catch manifold exceptions
19 # by design views should not directly exercise a manifold query
20 # given that these are asynchroneous, you would expect a view to just
21 # return a mundane skeleton
22 # however of course this is not always true,
23 # e.g. we deal with metadata some other way, and so
24 # it is often a good idea for a view to monitor these exceptions
25 # and to take this opportunity to logout people
27 def logout_on_manifold_exception (fun_that_returns_httpresponse):
28 def wrapped (request, *args, **kwds):
29 print 'wrapped by logout_on_manifold_exception'
31 return fun_that_returns_httpresponse(request,*args, **kwds)
32 except ManifoldException, manifold_result:
33 # xxx we need a means to display this message to user...
34 from django.contrib.auth import logout
36 return HttpResponseRedirect ('/')
38 # xxx we need to sugarcoat this error message in some error template...
39 print "Unexpected exception",e
42 return HttpResponseRedirect ('/')
45 # at first sight this matters only for views that require login
46 # so for now we expose a single class that behaves like
47 # login_required + logout_on_manifold_exception
48 class LoginRequiredAutoLogoutView (TemplateView):
50 @logout_on_manifold_exception
51 @method_decorator(login_required)
52 def dispatch (self, request, *args, **kwds):
53 return super(LoginRequiredAutoLogoutView,self).dispatch(request,*args,**kwds)