translate most modules to using print() as imported from __future__
[myslice.git] / unfold / loginrequired.py
1 from __future__ import print_function
2
3 from django.contrib.auth.decorators     import login_required
4 from django.utils.decorators            import method_decorator
5 from django.http                        import HttpResponseRedirect
6 # for 'as_view' that we need to call in urls.py and the like
7 from django.views.generic.base          import TemplateView
8
9 from manifoldapi.manifoldresult            import ManifoldException
10
11 ###
12 # IMPORTANT NOTE
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
16 ###
17
18 ########## the base class for views that require a login
19 class LoginRequiredView (TemplateView):
20
21     @method_decorator(login_required)
22     def dispatch(self, request, *args, **kwargs):
23         return super(LoginRequiredView, self).dispatch(request, *args, **kwargs)
24
25
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 
35
36 def logout_on_manifold_exception (fun_that_returns_httpresponse):
37     def wrapped (request, *args, **kwds):
38 #        print 'wrapped by logout_on_manifold_exception'
39         try:
40             return fun_that_returns_httpresponse(request,*args, **kwds)
41         except ManifoldException, manifold_result:
42             # xxx we need a means to display this message to user...
43             from django.contrib.auth import logout
44             # in some unusual cases, this might fail
45             try: logout(request)
46             except: pass
47             return HttpResponseRedirect ('/')
48         except Exception, e:
49             # xxx we need to sugarcoat this error message in some error template...
50             print("Unexpected exception",e)
51             import traceback
52             traceback.print_exc()
53             return HttpResponseRedirect ('/')
54     return wrapped
55
56 # at first sight this matters only for views that require login
57 # so for now we expose a single class that behaves like 
58 # login_required + logout_on_manifold_exception
59 class LoginRequiredAutoLogoutView (TemplateView):
60
61     @logout_on_manifold_exception
62     @method_decorator(login_required)
63     def dispatch (self, request, *args, **kwds):
64         return super(LoginRequiredAutoLogoutView,self).dispatch(request,*args,**kwds)
65
66 # we have more and more views that actually send manifold queries
67 # so for these we need to protect against manifold exceptions
68 # even though login is not required
69 class FreeAccessView (TemplateView):
70
71     def dispatch (self, request, *args, **kwds):
72         return super(FreeAccessView,self).dispatch(request,*args,**kwds)