renamed validatebutton into topmenuvalidation
[myslice.git] / ui / topmenu.py
1 import json
2 from pprint import pprint
3 from manifold.manifoldapi       import execute_query
4 from manifold.core.query        import Query
5 # a set of utilities to help make the global layout consistent across views
6
7 # dropdowns are kind of ad hoc for now, and limited to one level
8 # [ 
9 # ### a regular first-level button
10 # {'label':...,'href':..., ['domid':.., 'disabled':...]}, 
11 # ### a dropdown
12 # { 'label': ..., 'href'=..., 'dropdown':True, 'contents': [ { 'label':.., 'href'} ] }
13 # , ..]
14
15 # see also templates/widget-topmenu.html for how these items are put together
16 # and plugins/topmenuvalidation for how this hident button is turned on when necessary
17
18 # current: the beginning of the label in the menu that you want to outline
19 def topmenu_items (current,request=None):
20     has_user=request.user.is_authenticated()
21     result=[]
22     print request.user
23     if has_user:
24         result.append({'label':'Dashboard', 'href': '/portal/dashboard/'})
25         result.append({'label':'Request a slice', 'href': '/portal/slice_request/'})
26 ###        # ** Where am I a PI **
27 ###        # For this we need to ask SFA (of all authorities) = PI function
28 ###        user_query  = Query().get('local:user').select('config','email')
29 ###        user_details = execute_query(request, user_query)
30 ###
31 ###        # Required: the user must have an authority in its user.config
32 ###        # XXX Temporary solution
33 ###        # not always found in user_details...
34 ###        config={}
35 #### Deactivated until fixed 
36 ####        if user_details is not None:
37 ####            for user_detail in user_details:
38 ####                #email = user_detail['email']
39 ####                if user_detail['config']:
40 ####                    config = json.loads(user_detail['config'])
41 ####            user_detail['authority'] = config.get('authority',"Unknown Authority")
42 ####            print "topmenu: %s", (user_detail['authority'])
43 ####            if user_detail['authority'] is not None:
44 ####                sub_authority = user_detail['authority'].split('.')
45 ####                root_authority = sub_authority[0]
46 ####                pi_authorities_query = Query.get(root_authority+':user').filter_by('user_hrn', '==', '$user_hrn').select('pi_authorities')
47 ####        else:
48 ####            pi_authorities_query = Query.get('user').filter_by('user_hrn', '==', '$user_hrn').select('pi_authorities')
49 ####        try:
50 ####            pi_authorities_tmp = execute_query(request, pi_authorities_query)
51 ####        except:
52 ####            pi_authorities_tmp = set()
53 ####        pi_authorities = set()
54 ####        for pa in pi_authorities_tmp:
55 ####            if 'pi_authorities' in pa:
56 ####                pi_authorities |= set(pa['pi_authorities'])
57 ####        print "pi_authorities =", pi_authorities
58 ####        if len(pi_authorities) > 0:
59 ####            result.append({'label':'Validation', 'href': '/portal/validate/'})
60 ###        result.append({'label':'Validation', 'href': '/portal/validate/'})
61         # always create a disabled button for validation, and let the 
62         # topmenuvalidation plugin handle that asynchroneously, based on this domid
63         result.append({'label':'Validation', 'href': '/portal/validate/', 'domid':'topmenu-validation', 'disabled':True})
64         dropdown = []
65         dropdown.append({'label':'Platforms', 'href': '/portal/platforms/'})
66         dropdown.append({'label':'My Account', 'href': '/portal/account/'})
67         dropdown.append({'label':'Contact Support', 'href': '/portal/contact/'})
68         result.append({'label': 'More', 'href':"#", 'dropdown':True, 'contents':dropdown})
69     else:
70         result.append({'label':'Home', 'href': '/login'})
71         # looks like this is accessible to non-logged users
72         result.append({'label':'Platforms', 'href': '/portal/platforms/'})
73         result.append({'label':'Register', 'href': '/portal/register/'})
74         result.append({'label':'Contact Support', 'href': '/portal/contact/'})
75     # mark active if the provided 'current', even if shorter, matches the beginning of d['label']
76     
77     if current is not None:
78         current=current.lower()
79         curlen=len(current)
80         def mark_active(d,up=None):
81             if d['label'][:curlen].lower() == current: 
82                 d['is_active']=True
83                 if up is not None: up['is_active']=True
84         for d in result:
85             mark_active(d)
86             if 'dropdown' in d:
87                 for dd in d['contents']: mark_active(dd,d)
88     return result
89
90 def the_user (request):
91     "retrieves logged in user's email, or empty string"
92     if not request.user.is_authenticated (): 
93         return ''
94     else: 
95         return request.user.email