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