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