move related imports as well
[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 (current,request=None):
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     # mark active if the provided 'current', even if shorter, matches the beginning of d['label']
44     
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