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