X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=unfold%2Floginrequired.py;fp=unfold%2Floginrequired.py;h=0f46ff79916a30697c4dde15da10f06efb8f8a05;hb=31540dd504798e0aca69e10d8144fbedc5b16af8;hp=0000000000000000000000000000000000000000;hpb=937653fd70bbf7d95bcf870e7f2b46b4a8fec486;p=myslice.git diff --git a/unfold/loginrequired.py b/unfold/loginrequired.py new file mode 100644 index 00000000..0f46ff79 --- /dev/null +++ b/unfold/loginrequired.py @@ -0,0 +1,53 @@ +from django.contrib.auth.decorators import login_required +from django.utils.decorators import method_decorator +from django.http import HttpResponseRedirect +# for 'as_view' that we need to call in urls.py and the like +from django.views.generic.base import TemplateView + +from manifold.manifoldresult import ManifoldException + +########## the base class for views that require a login +class LoginRequiredView (TemplateView): + + @method_decorator(login_required) + def dispatch(self, request, *args, **kwargs): + return super(LoginRequiredView, self).dispatch(request, *args, **kwargs) + + +########## the base class for views that need to protect against ManifoldException +# a decorator for view classes to catch manifold exceptions +# by design views should not directly exercise a manifold query +# given that these are asynchroneous, you would expect a view to just +# return a mundane skeleton +# however of course this is not always true, +# e.g. we deal with metadata some other way, and so +# it is often a good idea for a view to monitor these exceptions +# and to take this opportunity to logout people + +def logout_on_manifold_exception (fun_that_returns_httpresponse): + def wrapped (request, *args, **kwds): + print 'wrapped by logout_on_manifold_exception' + try: + return fun_that_returns_httpresponse(request,*args, **kwds) + except ManifoldException, manifold_result: + # xxx we need a means to display this message to user... + from django.contrib.auth import logout + logout(request) + return HttpResponseRedirect ('/') + except Exception, e: + # xxx we need to sugarcoat this error message in some error template... + print "Unexpected exception",e + import traceback + traceback.print_exc() + return HttpResponseRedirect ('/') + return wrapped + +# at first sight this matters only for views that require login +# so for now we expose a single class that behaves like +# login_required + logout_on_manifold_exception +class LoginRequiredAutoLogoutView (TemplateView): + + @logout_on_manifold_exception + @method_decorator(login_required) + def dispatch (self, request, *args, **kwds): + return super(LoginRequiredAutoLogoutView,self).dispatch(request,*args,**kwds)