In topmenu the link to Validation page is only visible to PIs of some authority ...
[myslice.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         pi_authorities_tmp = execute_query(request, pi_authorities_query)
25         pi_authorities = set()
26         for pa in pi_authorities_tmp:
27             pi_authorities |= set(pa['pi_authorities'])
28         print "pi_authorities =", pi_authorities
29         if len(pi_authorities) > 0:
30             result.append({'label':'Validation', 'href': '/portal/validate/'})
31         dropdown = []
32         dropdown.append({'label':'Platforms', 'href': '/portal/platforms/'})
33         dropdown.append({'label':'My Account', 'href': '/portal/account/'})
34         dropdown.append({'label':'Contact Support', 'href': '/portal/contact/'})
35         result.append({'label': 'More', 'href':"#", 'dropdown':True, 'contents':dropdown})
36     else:
37         result.append({'label':'Home', 'href': '/login'})
38         # looks like this is accessible to non-logged users
39         result.append({'label':'Platforms', 'href': '/portal/platforms/'})
40         result.append({'label':'Register', 'href': '/portal/register/'})
41         result.append({'label':'Contact Support', 'href': '/portal/contact/'})
42     # mark active if the provided 'current', even if shorter, matches the beginning of d['label']
43     
44     if current is not None:
45         current=current.lower()
46         curlen=len(current)
47         def mark_active(d,up=None):
48             if d['label'][:curlen].lower() == current: 
49                 d['is_active']=True
50                 if up is not None: up['is_active']=True
51         for d in result:
52             mark_active(d)
53             if 'dropdown' in d:
54                 for dd in d['contents']: mark_active(dd,d)
55     return result
56
57 def the_user (request):
58     "retrieves logged in user's email, or empty string"
59     if not request.user.is_authenticated (): 
60         return ''
61     else: 
62         return request.user.email