Removed ple prefix to the user query in topmenu
[unfold.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':'Contact Support', 'href': '/portal/contact/'})
43
44     # mark active if the provided 'current', even if shorter, matches the beginning of d['label']
45     if current is not None:
46         current=current.lower()
47         curlen=len(current)
48         def mark_active(d,up=None):
49             if d['label'][:curlen].lower() == current: 
50                 d['is_active']=True
51                 if up is not None: up['is_active']=True
52         for d in result:
53             mark_active(d)
54             if 'dropdown' in d:
55                 for dd in d['contents']: mark_active(dd,d)
56     return result
57
58 # tmp - transition phase
59 def topmenu_items (current, request):
60     print "WARNING -- please now use topmenu_items_live (label, page) toplevel_items is deprecated -- WARNING"
61     return topmenu_items_static (current, request)
62
63 # integrated helper function for an animated menu
64 from unfold.page import Page
65 from manifold.core.query import Query
66 from plugins.topmenuvalidation import TopmenuValidation
67
68 ### this is now the 'live' version, that has plugins 
69 # for asynchronous management of topmenu
70 def topmenu_items_live (current, page):
71     request=page.request
72     query_pi_auths = Query.get('user').filter_by('user_hrn', '==', '$user_hrn' ).select('pi_authorities')
73     page.enqueue_query(query_pi_auths)
74 #        # even though this plugin does not have any html materialization, the corresponding domid
75 #        # must exist because it is searched at init-time to create the JS plugin
76 #        # so we simply piggy-back the target button created in the topmenu
77     topmenuvalidation = TopmenuValidation (
78         page=page, 
79         # see above
80         domid='topmenu-validation',
81         query=query_pi_auths,
82         # this one is the target for a $.show() when the query comes back
83         button_domid="topmenu-validation")
84     # although the result does not matter, rendering is required for the JS init code to make it in the page
85     topmenuvalidation.render(request)
86
87     return topmenu_items_static (current, request)
88