ManageUser: Display OK. Edit- ToDo
[myslice.git] / ui / topmenu.py
1 # a set of utilities to help make the global layout consistent across views
2
3 def the_user (request):
4     "retrieves logged in user's email, or empty string"
5     if not request.user.is_authenticated (): 
6         return ''
7     else: 
8         return request.user.email
9
10 # dropdowns are kind of ad hoc for now, and limited to one level
11 # [ 
12 # ### a regular first-level button
13 # {'label':...,'href':..., ['domid':.., 'disabled':...]}, 
14 # ### a dropdown
15 # { 'label': ..., 'href'=..., 'dropdown':True, 'contents': [ { 'label':.., 'href'} ] }
16 # , ..]
17
18 # see also templates/widget-topmenu.html for how these items are put together
19 # and plugins/topmenuvalidation for how this hident button is turned on when necessary
20
21 # current: the beginning of the label in the menu that you want to outline
22 def topmenu_items_static (current, request):
23     has_user=request.user.is_authenticated()
24     result=[]
25     print request.user
26     if has_user:
27         result.append({'label':'Dashboard', 'href': '/portal/dashboard/'})
28         result.append({'label':'Request a slice', 'href': '/portal/slice_request/'})
29         # always create a disabled button for validation, and let the 
30         # topmenuvalidation plugin handle that asynchroneously, based on this domid
31         result.append({'label':'Validation', 'href': '/portal/validate/', 'domid':'topmenu-validation', 'disabled':True})
32         dropdown = []
33         dropdown.append({'label':'Platforms', 'href': '/portal/platforms/'})
34         dropdown.append({'label':'My Account', 'href': '/portal/account/'})
35         dropdown.append({'label':'Contact Support', 'href': '/portal/contact/'})
36         result.append({'label': 'More', 'href':"#", 'dropdown':True, 'contents':dropdown})
37     else:
38         result.append({'label':'Home', 'href': '/login'})
39         # looks like this is accessible to non-logged users
40         result.append({'label':'Platforms', 'href': '/portal/platforms/'})
41         result.append({'label':'Register', 'href': '/portal/register/'})
42         result.append({'label':'Join us', 'href': '/portal/join/'})
43         result.append({'label':'Contact Support', 'href': '/portal/contact/'})
44
45     # mark active if the provided 'current', even if shorter, matches the beginning of d['label']
46     if current is not None:
47         current=current.lower()
48         curlen=len(current)
49         def mark_active(d,up=None):
50             if d['label'][:curlen].lower() == current: 
51                 d['is_active']=True
52                 if up is not None: up['is_active']=True
53         for d in result:
54             mark_active(d)
55             if 'dropdown' in d:
56                 for dd in d['contents']: mark_active(dd,d)
57     return result
58
59 # tmp - transition phase
60 def topmenu_items (current, request):
61     print "WARNING -- please now use topmenu_items_live (label, page) toplevel_items is deprecated -- WARNING"
62     return topmenu_items_static (current, request)
63
64 # integrated helper function for an animated menu
65 from unfold.page import Page
66 from manifold.core.query import Query
67 from plugins.topmenuvalidation import TopmenuValidation
68
69 ### this is now the 'live' version, that has plugins 
70 # for asynchronous management of topmenu
71 def topmenu_items_live (current, page):
72     request=page.request
73     query_pi_auths = Query.get('user').filter_by('user_hrn', '==', '$user_hrn' ).select('pi_authorities')
74     page.enqueue_query(query_pi_auths)
75 #        # even though this plugin does not have any html materialization, the corresponding domid
76 #        # must exist because it is searched at init-time to create the JS plugin
77 #        # so we simply piggy-back the target button created in the topmenu
78     topmenuvalidation = TopmenuValidation (
79         page=page, 
80         # see above
81         domid='topmenu-validation',
82         query=query_pi_auths,
83         # this one is the target for a $.show() when the query comes back
84         button_domid="topmenu-validation")
85     # although the result does not matter, rendering is required for the JS init code to make it in the page
86     topmenuvalidation.render(request)
87
88     return topmenu_items_static (current, request)
89