- added class variables: event_type, object_type, object_ids (used by event logger)
[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 can_update = lambda (field, value): field in ['description']
8
9 class AddAddressType(Method):
10     """
11     Adds a new address type. Fields specified in address_type_fields
12     are used.
13
14     Returns the new address_type_id (> 0) if successful, faults otherwise.
15     """
16
17     roles = ['admin']
18
19     update_fields = dict(filter(can_update, AddressType.fields.items()))
20
21     accepts = [
22         PasswordAuth(),
23         AddressType.fields['name'],
24         update_fields
25         ]
26
27     returns = Parameter(int, 'New address_type_id (> 0) if successful')
28         
29     event_type = 'Add'    
30     object_type = 'AddressType'
31     object_ids = []
32
33     def call(self, auth, name, address_type_fields = {}):
34         address_type_fields = dict(filter(can_update, address_type_fields.items()))
35         address_type = AddressType(self.api, address_type_fields)
36         address_type['name'] = name
37         address_type.sync()
38         self.object_ids = [address_type['address_type_id']]
39         
40         return address_type['address_type_id']