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, *args, **kwargs):
14 return super(LoginRequiredView, self).dispatch(*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 (view_as_a_function):
28 def wrapped (request, *args, **kwds):
30 return view_as_a_function(request,*args, **kwds)
31 except ManifoldException, manifold_result:
32 # xxx we need a means to display this message to user...
33 from django.contrib.auth import logout
35 return HttpResponseRedirect ('/')
37 # xxx we need to sugarcoat this error message in some error template...
38 print "Unexpected exception",e
41 return HttpResponseRedirect ('/')
44 # at first sight this matters only for views that require login
45 # however we prefer this to be explicit
46 # i.e. a user class has to inherit both LoginRequiredView and LogoutOnManifoldExceptionView
48 class LogoutOnManifoldExceptionView (TemplateView):
50 @logout_on_manifold_exception
51 def get (self, request, *args, **kwds):
52 return self.get_or_logout (request, *args, **kwds)