Merge branch 'master' of ssh://git.onelab.eu/git/myslice
[unfold.git] / portal / dashboardview.py
1 import json
2 from manifold.core.query         import Query
3 from manifold.manifoldapi        import execute_query
4
5 from unfold.page                 import Page
6
7 from plugins.lists.testbedlist   import TestbedList
8 from plugins.lists.slicelist     import SliceList
9
10 from unfold.loginrequired        import LoginRequiredAutoLogoutView
11
12 from ui.topmenu                  import topmenu_items_live, the_user
13
14 #This view requires login 
15 class DashboardView (LoginRequiredAutoLogoutView):
16
17     template_name = "dashboard.html"
18     
19     def get_context_data(self, **kwargs):
20         # We might have slices on different registries with different user accounts 
21         # We note that this portal could be specific to a given registry, to which we register users, but i'm not sure that simplifies things
22         # Different registries mean different identities, unless we identify via SFA HRN or have associated the user email to a single hrn
23
24         #messages.info(self.request, 'You have logged in')
25         page = Page(self.request)
26
27         print "Dashboard page"
28         # Slow...
29         #slice_query = Query().get('slice').filter_by('user.user_hrn', 'contains', user_hrn).select('slice_hrn')
30         testbed_query  = Query().get('network').select('network_hrn','platform')
31         # DEMO GEC18 Query only PLE
32 #        user_query  = Query().get('local:user').select('config','email')
33 #        user_details = execute_query(self.request, user_query)
34
35         # not always found in user_details...
36 #        config={}
37   #      for user_detail in user_details:
38   #          #email = user_detail['email']
39   #          if user_detail['config']:
40   #              config = json.loads(user_detail['config'])
41   #      user_detail['authority'] = config.get('authority',"Unknown Authority")
42 #
43 #        print user_detail
44 #        if user_detail['authority'] is not None:
45 #            sub_authority = user_detail['authority'].split('.')
46 #            root_authority = sub_authority[0]
47 #            slice_query = Query().get(root_authority+':user').filter_by('user_hrn', '==', '$user_hrn').select('user_hrn', 'slice.slice_hrn')
48 #        else:
49         slice_query = Query().get('user').filter_by('user_hrn', '==', '$user_hrn').select('user_hrn', 'slice.slice_hrn')
50         page.enqueue_query(slice_query)
51         page.enqueue_query(testbed_query)
52
53         slicelist = SliceList(
54             page  = page,
55             title = "slices",
56             query = slice_query,
57         )
58         testbedlist = TestbedList(
59             page  = page,
60             title = "testbeds",
61             query = testbed_query,
62         )
63  
64         context = super(DashboardView, self).get_context_data(**kwargs)
65         context['person']   = self.request.user
66         context['testbeds'] = testbedlist.render(self.request) 
67         context['slices']   = slicelist.render(self.request)
68
69         # XXX This is repeated in all pages
70         # more general variables expected in the template
71         context['title'] = 'Dashboard'
72         # the menu items on the top
73         context['topmenu_items'] = topmenu_items_live('Dashboard', page) 
74         # so we can sho who is logged
75         context['username'] = the_user(self.request) 
76
77         page.expose_js_metadata()
78
79         # the page header and other stuff
80         context.update(page.prelude_env())
81
82         return context
83