Merge branch 'master' of ssh://git.onelab.eu/git/myslice
[unfold.git] / ui / topmenu.py
1 from pprint import pprint
2 from manifold.manifoldapi       import execute_query
3 from manifold.core.query        import Query
4 # a set of utilities to help make the global layout consistent across views
5
6 # dropdowns are kind of ad hoc for now, and limited to one level
7 # [ 
8 # ### a regular first-level button
9 # {'label':...,'href':...}, 
10 # ### a dropdown
11 # { 'label': ..., 'href'=..., 'dropdown':True, 'contents': [ { 'label':.., 'href'} ] }
12 # , ..]
13
14 # current: the beginning of the label in the menu that you want to outline
15 def topmenu_items (current,request=None):
16     has_user=request.user.is_authenticated()
17     result=[]
18     if has_user:
19         result.append({'label':'Dashboard', 'href': '/portal/dashboard/'})
20         result.append({'label':'Request a slice', 'href': '/portal/slice_request/'})
21         # ** Where am I a PI **
22         # For this we need to ask SFA (of all authorities) = PI function
23         pi_authorities_query = Query.get('ple:user').filter_by('user_hrn', '==', '$user_hrn').select('pi_authorities')
24         try:
25             pi_authorities_tmp = execute_query(request, pi_authorities_query)
26         except:
27             pi_authorities_tmp = set()
28         pi_authorities = set()
29         for pa in pi_authorities_tmp:
30             pi_authorities |= set(pa['pi_authorities'])
31         print "pi_authorities =", pi_authorities
32         if len(pi_authorities) > 0:
33             result.append({'label':'Validation', 'href': '/portal/validate/'})
34         dropdown = []
35         dropdown.append({'label':'Platforms', 'href': '/portal/platforms/'})
36         dropdown.append({'label':'My Account', 'href': '/portal/account/'})
37         dropdown.append({'label':'Contact Support', 'href': '/portal/contact/'})
38         result.append({'label': 'More', 'href':"#", 'dropdown':True, 'contents':dropdown})
39     else:
40         result.append({'label':'Home', 'href': '/login'})
41         # looks like this is accessible to non-logged users
42         result.append({'label':'Platforms', 'href': '/portal/platforms/'})
43         result.append({'label':'Register', 'href': '/portal/register/'})
44         result.append({'label':'Contact Support', 'href': '/portal/contact/'})
45     # mark active if the provided 'current', even if shorter, matches the beginning of d['label']
46     
47     if current is not None:
48         current=current.lower()
49         curlen=len(current)
50         def mark_active(d,up=None):
51             if d['label'][:curlen].lower() == current: 
52                 d['is_active']=True
53                 if up is not None: up['is_active']=True
54         for d in result:
55             mark_active(d)
56             if 'dropdown' in d:
57                 for dd in d['contents']: mark_active(dd,d)
58     return result
59
60 def the_user (request):
61     "retrieves logged in user's email, or empty string"
62     if not request.user.is_authenticated (): 
63         return ''
64     else: 
65         return request.user.email