From: Thierry Parmentelat Date: Tue, 3 Sep 2013 15:59:08 +0000 (+0200) Subject: logout_on_manifold_exception : a decorator for dealing with manifold exceptions insid... X-Git-Tag: myslice-0.2-1~24^2~3 X-Git-Url: http://git.onelab.eu/?p=myslice.git;a=commitdiff_plain;h=f8ab9d13886a50daf7500e0b4d669e37f91ab4de logout_on_manifold_exception : a decorator for dealing with manifold exceptions inside a view (when implemented as a function) --- diff --git a/myslice/viewutils.py b/myslice/viewutils.py index 7fbdc77f..3fa8ceba 100644 --- a/myslice/viewutils.py +++ b/myslice/viewutils.py @@ -30,3 +30,29 @@ def the_user (request): else: return request.user.email + +# 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, and if only for metadata +# that for some reason we deal with some other way, it is often a good idea +# for a view to monitor these exceptions - and to take this opportunity to +# logout people if it's a matter of expired session for example +def logout_on_manifold_exception (view_as_a_function): + def wrapped (request, *args, **kwds): + try: + return view_as_a_function(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 + diff --git a/portal/sliceview.py b/portal/sliceview.py index 534ed78a..5bf37fb0 100644 --- a/portal/sliceview.py +++ b/portal/sliceview.py @@ -11,7 +11,7 @@ from manifold.manifoldresult import ManifoldException from manifold.metadata import MetaData as Metadata # need to remove this dep. from trash.trashutils import quickfilter_criterias -from myslice.viewutils import topmenu_items, the_user +from myslice.viewutils import topmenu_items, the_user, logout_on_manifold_exception from plugins.raw.raw import Raw from plugins.stack.stack import Stack @@ -31,27 +31,9 @@ from plugins.messages.messages import Messages tmp_default_slice='ple.upmc.myslicedemo' debug = True +@logout_on_manifold_exception @login_required def slice_view (request, slicename=tmp_default_slice): - # xxx Thierry - ugly hack - # fetching metadata here might fail - e.g. with an expired session.. - # let's catch this early on and log out our user if needed - # it should of course be handled in a more generic way - try: - return _slice_view(request,slicename) - except ManifoldException, manifold_result: - # xxx needs 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 ... - -def _slice_view (request, slicename): page = Page(request) page.expose_js_metadata()