d07cc2a9f480d21a2172cb5c1cf6d69e9454e2db
[plcapi.git] / PLC / Methods / UpdatePCUType.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
8 can_update = lambda (field, value): field in \
9              ['model', 'name']
10
11 class UpdatePCUType(Method):
12     """
13     Updates a PCU type. Only the fields specified in
14     pcu_typee_fields are updated, all other fields are left untouched.
15
16     Returns 1 if successful, faults otherwise.
17     """
18
19     roles = ['admin']
20
21     pcu_type_fields = dict(filter(can_update, PCUType.fields.items()))
22
23     accepts = [
24         Auth(),
25         PCUType.fields['pcu_type_id'],
26         pcu_type_fields
27         ]
28
29     returns = Parameter(int, '1 if successful')
30
31     def call(self, auth, pcu_type_id, pcu_type_fields):
32         pcu_type_fields = dict(filter(can_update, pcu_type_fields.items()))
33
34         pcu_types = PCUTypes(self.api, [pcu_type_id])
35         if not pcu_types:
36             raise PLCInvalidArgument, "No such pcu type"
37
38         pcu_type = pcu_types[0]
39         pcu_type.update(pcu_type_fields)
40         pcu_type.sync()
41         self.event_objects = {'PCUType': [pcu_type['pcu_type_id']]}     
42
43         return 1