6110de2b3157c309a66db9dd2b835608109115b1
[plcapi.git] / PLC / Methods / UpdateAddressType.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.AddressTypes import AddressType, AddressTypes
7 from PLC.Auth import Auth
8
9 can_update = lambda (field, value): field in ['name', 'description']
10
11 class UpdateAddressType(Method):
12     """
13     Updates the parameters of an existing address type with the values
14     in address_type_fields.
15
16     Returns 1 if successful, faults otherwise.
17     """
18
19     roles = ['admin']
20
21     address_type_fields = dict(filter(can_update, AddressType.fields.items()))
22
23     accepts = [
24         Auth(),
25         Mixed(AddressType.fields['address_type_id'],
26               AddressType.fields['name']),
27         address_type_fields
28         ]
29
30     returns = Parameter(int, '1 if successful')
31
32     def call(self, auth, address_type_id_or_name, address_type_fields):
33         address_type_fields = dict(filter(can_update, address_type_fields.items()))
34
35         address_types = AddressTypes(self.api, [address_type_id_or_name])
36         if not address_types:
37             raise PLCInvalidArgument, "No such address type"
38         address_type = address_types[0]
39
40         address_type.update(address_type_fields)
41         address_type.sync()
42         self.event_objects = {'AddressType': [address_type['address_type_id']]}
43
44         return 1