- implement AddressType and Address manipulation
[plcapi.git] / PLC / Methods / AddAddressTypeToAddress.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.Addresses import Address, Addresses
6 from PLC.Auth import PasswordAuth
7
8 class AddAddressTypeToAddress(Method):
9     """
10     Adds an address type to the specified address.
11
12     PIs may only update addresses of their own sites.
13
14     Returns 1 if successful, faults otherwise.
15     """
16
17     roles = ['admin', 'pi']
18
19     accepts = [
20         PasswordAuth(),
21         Mixed(AddressType.fields['address_type_id'],
22               AddressType.fields['name']),
23         Address.fields['address_id']
24         ]
25
26     returns = Parameter(int, '1 if successful')
27
28     def call(self, auth, address_type_id_or_name, address_id):
29         address_types = AddressTypes(self.api, [address_type_id_or_name]).values()
30         if not address_types:
31             raise PLCInvalidArgument, "No such address type"
32         address_type = address_types[0]
33
34         addresses = Addresses(self.api, [address_id]).values()
35         if not addresses:
36             raise PLCInvalidArgument, "No such address"
37         address = addresses[0]
38
39         if 'admin' not in self.caller['roles']:
40             if address['site_id'] not in self.caller['site_ids']:
41                 raise PLCPermissionDenied, "Address must be associated with one of your sites"
42
43         address.add_address_type(address_type)
44
45         return 1