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