- implement AddressType and Address manipulation
[plcapi.git] / PLC / Methods / AddAddressType.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 class AddAddressType(Method):
8     """
9     Adds a new address type. Fields specified in address_type_fields
10     are used.
11
12     Returns the new address_type_id (> 0) if successful, faults otherwise.
13     """
14
15     roles = ['admin']
16
17     can_update = lambda (field, value): field in ['description']
18     update_fields = dict(filter(can_update, AddressType.fields.items()))
19
20     accepts = [
21         PasswordAuth(),
22         AddressType.fields['name'],
23         update_fields
24         ]
25
26     returns = Parameter(int, 'New address_type_id (> 0) if successful')
27
28     def call(self, auth, name, address_type_fields = {}):
29         address_type = AddressType(self.api, address_type_fields)
30         address_type['name'] = name
31         address_type.sync()
32
33         return address_type['address_type_id']