- hack xmlrpclib so that it can marshal subclasses of built-in types (e.g., Row)
[plcapi.git] / PLC / Methods / GetPCUs.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.PCUs import PCU, PCUs
5 from PLC.Auth import PasswordAuth
6
7 class GetPCUs(Method):
8     """
9     Return an array of structs containing details about PCUs. If
10     pcu_id_list is specified, only the specified PCUs will be queried.
11
12     Admin may query all PCUs. Non-admins may only query the PCUs at
13     their sites.
14     """
15
16     roles = ['admin', 'pi', 'tech']
17
18     accepts = [
19         PasswordAuth(),
20         [PCU.fields['pcu_id']]
21         ]
22
23     returns = [PCU.fields]
24
25     def call(self, auth, pcu_ids = None):
26         # If we are not admin, make sure to only return our own PCUs
27         if 'admin' not in self.caller['roles']:
28             # Get list of PCUs that we are able to view
29             valid_pcu_ids = []
30             if self.caller['site_ids']:
31                 sites = Sites(self.api, self.caller['site_ids']).values()
32                 for site in sites:
33                     valid_pcu_ids += site['pcu_ids']
34
35             pcu_ids = set(pcu_ids).intersection(valid_pcu_ids)
36             if not pcu_ids:
37                 return []
38
39         return PCUs(self.api, pcu_ids).values()