1 # a set of utilities to help make the global layout consistent across views
3 from myslice.settings import logger
5 def the_user (request):
6 "retrieves logged in user's email, or empty string"
7 if not request.user.is_authenticated ():
10 return request.user.email
12 # dropdowns are kind of ad hoc for now, and limited to one level
14 # ### a regular first-level button
15 # {'label':...,'href':..., ['domid':.., 'disabled':...]},
17 # { 'label': ..., 'href'=..., 'dropdown':True, 'contents': [ { 'label':.., 'href'} ] }
20 # see also templates/widget-topmenu.html for how these items are put together
21 # and plugins/topmenuvalidation for how this hident button is turned on when necessary
23 # current: the beginning of the label in the menu that you want to outline
24 def topmenu_items_static (current, request):
25 has_user=request.user.is_authenticated()
27 logger.debug("request user = {}".format(request.user))
29 result.append({'label':'Dashboard', 'href': '/portal/dashboard/'})
30 result.append({'label':'Request a slice', 'href': '/portal/slice_request/'})
31 # always create a disabled button for validation, and let the
32 # topmenuvalidation plugin handle that asynchroneously, based on this domid
33 result.append({'label':'Validation', 'href': '/portal/validate/', 'domid':'topmenu-validation', 'disabled':True})
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})
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':'Join us', 'href': '/portal/join/'})
45 result.append({'label':'Contact Support', 'href': '/portal/contact/'})
47 # mark active if the provided 'current', even if shorter, matches the beginning of d['label']
48 if current is not None:
49 current=current.lower()
51 def mark_active(d,up=None):
52 if d['label'][:curlen].lower() == current:
54 if up is not None: up['is_active']=True
58 for dd in d['contents']: mark_active(dd,d)
61 # tmp - transition phase
62 def topmenu_items (current, request):
63 logger.warning("WARNING -- please now use topmenu_items_live (label, page) toplevel_items is deprecated -- WARNING")
64 return topmenu_items_static (current, request)
66 # integrated helper function for an animated menu
67 from unfold.page import Page
68 from manifold.core.query import Query
69 from plugins.topmenuvalidation import TopmenuValidation
71 ### this is now the 'live' version, that has plugins
72 # for asynchronous management of topmenu
73 def topmenu_items_live (current, page):
75 # XXX TODO This should be triggered only when user is logged in
76 # We might use local storage instead
78 # REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2
79 query_pi_auths = Query.get('myslice:user').filter_by('user_hrn', '==', '$user_hrn' ).select('user_hrn','pi_authorities')
80 page.enqueue_query(query_pi_auths)
81 # # even though this plugin does not have any html materialization, the corresponding domid
82 # # must exist because it is searched at init-time to create the JS plugin
83 # # so we simply piggy-back the target button created in the topmenu
84 topmenuvalidation = TopmenuValidation (
87 domid='topmenu-validation',
89 # this one is the target for a $.show() when the query comes back
90 button_domid="topmenu-validation")
91 # although the result does not matter, rendering is required for the JS init code to make it in the page
92 topmenuvalidation.render(request)
94 return topmenu_items_static (current, request)