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