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