manual_delegation: commands updated
[unfold.git] / portal / platformview.py
1 from django.template             import RequestContext
2 from django.shortcuts            import render_to_response
3
4 from manifold.core.query         import Query
5 from unfold.page                 import Page
6 from plugins.raw                 import Raw
7 from plugins.stack               import Stack
8 from plugins.tabs                import Tabs
9 from plugins.googlemap           import GoogleMap
10 from plugins.senslabmap          import SensLabMap
11
12 from unfold.loginrequired        import FreeAccessView
13 from ui.topmenu                  import topmenu_items_live, the_user
14
15 from plugins.querytable          import QueryTable
16
17 from myslice.configengine        import ConfigEngine
18 from myslice.theme import ThemeView
19
20 # View for 1 platform and its details
21 class PlatformView(FreeAccessView, ThemeView):
22     template_name = "platform.html"
23
24     def get_context_data(self, **kwargs):
25         page = Page(self.request)
26         page.add_js_files  ( [ "js/common.functions.js" ] )
27
28         for key, value in kwargs.iteritems():
29             print "%s = %s" % (key, value)       
30             if key == "platformname":
31                 platformname=value
32                 
33         network_query  = Query().get('local:platform')\
34             .filter_by('platform', '==', platformname)\
35             .select('platform','platform_longname','gateway_type')
36         page.enqueue_query(network_query)
37
38         # ListResources of the platform
39         metadata = page.get_metadata()
40         resource_md = metadata.details_by_object('resource')
41         resource_fields = [column['name'] for column in resource_md['column']]
42         resources = platformname + ':resource'
43         query_resource_all = Query.get(resources).select(resource_fields)
44         page.enqueue_query(query_resource_all)
45         query_resource_default_fields = Query.get(resources).select('hrn','hostname', 'type','country')
46         page.enqueue_query(query_resource_default_fields)
47
48         page.expose_js_metadata()
49         networklist = QueryTable(
50             page  = page,
51             title = 'List',
52             domid = 'checkboxes',
53             # this is the query at the core of the slice list
54             query = network_query,
55             query_all = network_query,
56             checkboxes = False,
57             datatables_options = {
58             'iDisplayLength' : 25,
59             'bLengthChange'  : True,
60             },
61         )
62
63 #        networklist = SimpleList(
64 #            title = None,
65 #            page  = page,
66 #            key   = 'platform',
67 #            query = network_query,
68 #        )
69 #
70         # --------------------------------------------------------------------------
71         # RESOURCES
72         # for internal use in the querytable plugin;
73         # needs to be a unique column present for each returned record
74         main_query_init_key = 'hrn'
75
76         # the resources part is made of a Tabs (Geographic, List), 
77         resources_as_gmap = GoogleMap(
78             page       = page,
79             title      = 'Geographic view',
80             domid      = 'resources-map',
81             # tab's sons preferably turn this off
82             togglable  = False,
83             query      = query_resource_default_fields,
84             query_all  = query_resource_all,
85             # this key is the one issued by google
86             googlemap_api_key = ConfigEngine().googlemap_api_key(),
87             # the key to use at init-time
88             init_key   = main_query_init_key,
89             checkboxes = False,
90             # center on Paris
91             latitude   = 49.,
92             longitude  = 9,
93             zoom       = 4,
94         )
95         resources_as_3dmap = SensLabMap(
96             page       = page,
97             title      = '3D Map',
98             domid      = 'senslabmap',
99             query      = query_resource_default_fields,
100             query_all  = query_resource_all,
101         )
102         resources_as_list = QueryTable(
103             page       = page,
104             domid      = 'resources-list',
105             title      = 'List view',
106             # this is the query at the core of the slice list
107             query      = query_resource_default_fields,
108             query_all  = query_resource_all,
109             init_key     = main_query_init_key,
110             checkboxes = False,
111             datatables_options = {
112                 'iDisplayLength': 25,
113                 'bLengthChange' : True,
114                 'bAutoWidth'    : True,
115                 },
116             )
117         resources_sons = [
118              resources_as_gmap,
119              resources_as_3dmap,
120              resources_as_list,
121              ]
122         resources_area = Tabs ( page=page,
123                                 domid="resources",
124                                 togglable=True,
125                                 title="Resources",
126                                 outline_complete=True,
127                                 sons= resources_sons,
128                                 active_domid = 'resources-map',
129                                 persistent_active=True,
130                                 )
131
132         context = super(PlatformView, self).get_context_data(**kwargs)
133         context['person']   = self.request.user
134         context['networks'] = networklist.render(self.request)
135         context['resources'] = resources_area.render(self.request)
136
137         # XXX This is repeated in all pages
138         # more general variables expected in the template
139         context['title'] = 'Platforms connected to MySlice'
140         # the menu items on the top
141         context['topmenu_items'] = topmenu_items_live('Platforms', page)
142         # so we can sho who is logged
143         context['username'] = the_user(self.request)
144         context['theme'] = self.theme
145         context.update(page.prelude_env())
146
147         return context