82979d55f36f5795fef6ef00a5cf5c597e645298
[myslice.git] / ui / topmenu.py
1 import json
2 from pprint import pprint
3 from manifold.manifoldapi       import execute_query
4 from manifold.core.query        import Query
5 # a set of utilities to help make the global layout consistent across views
6
7 # dropdowns are kind of ad hoc for now, and limited to one level
8 # [ 
9 # ### a regular first-level button
10 # {'label':...,'href':..., ['domid':.., 'disabled':...]}, 
11 # ### a dropdown
12 # { 'label': ..., 'href'=..., 'dropdown':True, 'contents': [ { 'label':.., 'href'} ] }
13 # , ..]
14
15 # see also templates/widget-topmenu.html for how these items are put together
16 # and plugins/topmenuvalidation for how this hident button is turned on when necessary
17
18 # current: the beginning of the label in the menu that you want to outline
19 def topmenu_items (current,request=None):
20     has_user=request.user.is_authenticated()
21     result=[]
22     print request.user
23     if has_user:
24         result.append({'label':'Dashboard', 'href': '/portal/dashboard/'})
25         result.append({'label':'Request a slice', 'href': '/portal/slice_request/'})
26         # always create a disabled button for validation, and let the 
27         # topmenuvalidation plugin handle that asynchroneously, based on this domid
28         result.append({'label':'Validation', 'href': '/portal/validate/', 'domid':'topmenu-validation', 'disabled':True})
29         dropdown = []
30         dropdown.append({'label':'Platforms', 'href': '/portal/platforms/'})
31         dropdown.append({'label':'My Account', 'href': '/portal/account/'})
32         dropdown.append({'label':'Contact Support', 'href': '/portal/contact/'})
33         result.append({'label': 'More', 'href':"#", 'dropdown':True, 'contents':dropdown})
34     else:
35         result.append({'label':'Home', 'href': '/login'})
36         # looks like this is accessible to non-logged users
37         result.append({'label':'Platforms', 'href': '/portal/platforms/'})
38         result.append({'label':'Register', 'href': '/portal/register/'})
39         result.append({'label':'Contact Support', 'href': '/portal/contact/'})
40     # mark active if the provided 'current', even if shorter, matches the beginning of d['label']
41     
42     if current is not None:
43         current=current.lower()
44         curlen=len(current)
45         def mark_active(d,up=None):
46             if d['label'][:curlen].lower() == current: 
47                 d['is_active']=True
48                 if up is not None: up['is_active']=True
49         for d in result:
50             mark_active(d)
51             if 'dropdown' in d:
52                 for dd in d['contents']: mark_active(dd,d)
53     return result
54
55 def the_user (request):
56     "retrieves logged in user's email, or empty string"
57     if not request.user.is_authenticated (): 
58         return ''
59     else: 
60         return request.user.email