- make Add() calling convention consistent among all functions that
[plcapi.git] / PLC / Methods / UpdateAddressType.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.AddressTypes import AddressType, AddressTypes
5 from PLC.Auth import PasswordAuth
6
7 can_update = lambda (field, value): field in ['name', 'description']
8
9 class UpdateAddressType(Method):
10     """
11     Updates the parameters of an existing address type with the values
12     in address_type_fields.
13
14     Returns 1 if successful, faults otherwise.
15     """
16
17     roles = ['admin']
18
19     address_type_fields = dict(filter(can_update, AddressType.fields.items()))
20     for field in address_type_fields.values():
21         field.optional = True
22
23     accepts = [
24         PasswordAuth(),
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]).values()
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
43         return 1