38092e47daa47108cdd99a997e1afb546c4ba6ec
[plcapi.git] / PLC / Methods / AddAddressType.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 not in ['address_type_id']
9
10 class AddAddressType(Method):
11     """
12     Adds a new address type. Fields specified in address_type_fields
13     are used.
14
15     Returns the new address_type_id (> 0) 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         address_type_fields
25         ]
26
27     returns = Parameter(int, 'New address_type_id (> 0) if successful')
28         
29
30     def call(self, auth, address_type_fields):
31         address_type_fields = dict(filter(can_update, address_type_fields.items()))
32         address_type = AddressType(self.api, address_type_fields)
33         address_type.sync()
34
35         self.event_objects = {'AddressType' : [address_type['address_type_id']]}
36         
37         return address_type['address_type_id']