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