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 manifoldapi.manifoldresult import ManifoldException
9 from myslice.settings import logger
13 # the implementation of the classes in this file rely on redefining 'dispatch'
14 # for this reason if you inherit any of these, please do not redefine 'dispatch' yourself,
15 # but rather redefine 'get' and 'post' instead
18 ########## the base class for views that require a login
19 class LoginRequiredView (TemplateView):
21 @method_decorator(login_required)
22 def dispatch(self, request, *args, **kwargs):
23 return super(LoginRequiredView, self).dispatch(request, *args, **kwargs)
26 ########## the base class for views that need to protect against ManifoldException
27 # a decorator for view classes to catch manifold exceptions
28 # by design views should not directly exercise a manifold query
29 # given that these are asynchroneous, you would expect a view to just
30 # return a mundane skeleton
31 # however of course this is not always true,
32 # e.g. we deal with metadata some other way, and so
33 # it is often a good idea for a view to monitor these exceptions
34 # and to take this opportunity to logout people
36 def logout_on_manifold_exception (fun_that_returns_httpresponse):
37 def wrapped (request, *args, **kwds):
39 return fun_that_returns_httpresponse(request,*args, **kwds)
40 except ManifoldException, manifold_result:
41 # xxx we need a means to display this message to user...
42 from django.contrib.auth import logout
43 # in some unusual cases, this might fail
46 return HttpResponseRedirect ('/')
48 # xxx we need to sugarcoat this error message in some error template...
49 logger.error("Unexpected exception {}".format(e))
51 logger.error(traceback.format_exc())
52 return HttpResponseRedirect ('/')
55 # at first sight this matters only for views that require login
56 # so for now we expose a single class that behaves like
57 # login_required + logout_on_manifold_exception
58 class LoginRequiredAutoLogoutView (TemplateView):
60 @logout_on_manifold_exception
61 @method_decorator(login_required)
62 def dispatch (self, request, *args, **kwds):
63 return super(LoginRequiredAutoLogoutView,self).dispatch(request,*args,**kwds)
65 # we have more and more views that actually send manifold queries
66 # so for these we need to protect against manifold exceptions
67 # even though login is not required
68 class FreeAccessView (TemplateView):
70 def dispatch (self, request, *args, **kwds):
71 return super(FreeAccessView,self).dispatch(request,*args,**kwds)