myslice.ini possible themes = onelab, fed4fire, fibre, smartfire
[myslice.git] / unfold / loginrequired.py
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
6
7 from manifold.manifoldresult            import ManifoldException
8
9 ###
10 # IMPORTANT NOTE
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
14 ###
15
16 ########## the base class for views that require a login
17 class LoginRequiredView (TemplateView):
18
19     @method_decorator(login_required)
20     def dispatch(self, request, *args, **kwargs):
21         return super(LoginRequiredView, self).dispatch(request, *args, **kwargs)
22
23
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 
33
34 def logout_on_manifold_exception (fun_that_returns_httpresponse):
35     def wrapped (request, *args, **kwds):
36 #        print 'wrapped by logout_on_manifold_exception'
37         try:
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
43             try: logout(request)
44             except: pass
45             return HttpResponseRedirect ('/')
46         except Exception, e:
47             # xxx we need to sugarcoat this error message in some error template...
48             print "Unexpected exception",e
49             import traceback
50             traceback.print_exc()
51             return HttpResponseRedirect ('/')
52     return wrapped
53
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):
58
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)
63
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):
68
69     def dispatch (self, request, *args, **kwds):
70         return super(FreeAccessView,self).dispatch(request,*args,**kwds)