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