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