logout_on_manifold_exception : a decorator for dealing with manifold exceptions insid...
[myslice.git] / myslice / viewutils.py
1 # a set of utilities to help make the global layout consistent across views
2
3 def topmenu_items (current,request=None):
4     has_user=request.user.is_authenticated()
5     result=[]
6     if has_user:
7         result.append({'label':'Platforms', 'href': '/portal/platforms/'})
8         result.append({ 'label':'Dashboard', 'href': '/portal/dashboard/'})
9         # This should probably go in dashboard at some point
10         result.append({ 'label':'Request a slice', 'href': '/portal/slice_request/'})
11         result.append({'label':'My Account', 'href': '/portal/account/'})
12     else:
13         result.append({'label':'Home', 'href': '/login'})
14         # looks like this is accessible to non-logged users
15         result.append({'label':'Platforms', 'href': '/portal/platforms/'})
16         result.append({ 'label':'Register', 'href': '/portal/register/'})
17     result.append({'label':'Contact Support', 'href': '/portal/contact/'})
18     for d in result:
19         #if d['label'].lower()find(current)>=0: d['is_active']=True
20         if d['label'] == current: d['is_active']=True
21     if not request: return result
22 #    result.append (login_out_items [ has_user] )
23     return result
24
25 def the_user (request):
26     "This code below is broken"
27     if not request.user.is_authenticated (): 
28 #        print 'void user!'
29         return ''
30     else: 
31         return request.user.email
32
33
34 # a decorator for view classes to catch manifold exceptions
35 # by design views should not directly exercise a manifold query
36 # given that these are asynchroneous, you would expect a view to just 
37 # return a mundane skeleton
38 # however of course this is not always true, and if only for metadata 
39 # that for some reason we deal with some other way, it is often a good idea
40 # for a view to monitor these exceptions - and to take this opportunity to 
41 # logout people if it's a matter of expired session for example
42 def logout_on_manifold_exception (view_as_a_function):
43     def wrapped (request, *args, **kwds):
44         try:
45             return view_as_a_function(request,*args, **kwds)
46         except ManifoldException, manifold_result:
47             # xxx we need a means to display this message to user...
48             from django.contrib.auth import logout
49             logout(request)
50             return HttpResponseRedirect ('/')
51         except Exception, e:
52             # xxx we need to sugarcoat this error message in some error template...
53             print "Unexpected exception",e
54             import traceback
55             traceback.print_exc()
56             return HttpResponseRedirect ('/')
57     return wrapped
58