remove simplejson dependency
[plcapi.git] / PLC / Methods / GetPCUTypes.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.PCUTypes import PCUType, PCUTypes
5 from PLC.Auth import Auth
6 from PLC.Filter import Filter
7
8 class GetPCUTypes(Method):
9     """
10     Returns an array of PCU Types.
11     """
12
13     roles = ['admin', 'pi', 'user', 'tech', 'node']
14
15     accepts = [
16         Auth(),
17         Mixed([Mixed(PCUType.fields['pcu_type_id'],
18                      PCUType.fields['model'])],
19                Parameter(str, 'model'),
20                Parameter(int, 'node_id'),
21                Filter(PCUType.fields)),
22         Parameter([str], "List of fields to return", nullok = True)
23         ]
24
25     returns = [PCUType.fields]
26
27
28     def call(self, auth, pcu_type_filter = None, return_fields = None):
29
30         #Must query at least pcu_type_id
31         if return_fields is not None:
32             added_fields = []
33             if 'pcu_type_id' not in return_fields:
34                 return_fields.append('pcu_type_id')
35                 added_fields.append('pcu_type_id')
36             if 'pcu_protocol_types' in return_fields and \
37                'pcu_protocol_type_ids' not in return_fields:
38                 return_fields.append('pcu_protocol_type_ids')
39                 added_fields.append('pcu_protocol_type_ids')
40         else:
41             added_fields = []
42
43         pcu_types = PCUTypes(self.api, pcu_type_filter, return_fields)
44
45         # remove added fields and protocol_types
46         for added_field in added_fields:
47             for pcu_type in pcu_types:
48                 del pcu_type[added_field]
49
50         return pcu_types