cast instead of rebuilding dict when returning lists of dicts
[plcapi.git] / PLC / Methods / GetSlices.py
1 from PLC.Method import Method
2 from PLC.Parameter import Parameter, Mixed
3 from PLC.Auth import PasswordAuth
4 from PLC.Slices import Slice, Slices
5
6 class GetSlices(Method):
7     """
8     Return an array of structs containing details about slices. If
9     slice_id_or_name_list is specified, only the specified slices will
10     be queried.
11
12     Users may only query slices of which they are members. PIs may
13     query any of the slices at their sites. Admins may query any
14     slice. If a slice that cannot be queried is specified in
15     slice_id_or_name_list, details about that slice will not be
16     returned.
17     """
18
19     roles = ['admin', 'pi', 'user', 'tech']
20
21     accepts = [
22         PasswordAuth(),
23         [Mixed(Slice.fields['slice_id'],
24                Slice.fields['name'])],
25         Parameter([str], 'List of fields to return')
26         ]
27
28     returns = [Slice.fields]
29
30     def call(self, auth, slice_id_or_name_list = None):
31         # Get slice information
32         slices = Slices(self.api, slice_id_or_name_list).values()
33
34         # Filter out slices that are not viewable
35         if 'admin' not in self.caller['roles']:
36             member_of = lambda slice: self.caller['person_id'] in slice['person_ids']
37             if 'pi' in self.caller['roles']:
38                 can_view = lambda slice: \
39                            member_of(slice) or \
40                            slice['site_id'] in self.caller['site_ids']
41             else:
42                 can_view = member_of
43             slices = filter(can_view, slices)
44
45         # turn each slice into a real dict
46         slices = [dict(slice) for slice in slices]
47
48         return slices