- implement AddressType and Address manipulation
[plcapi.git] / PLC / Methods / UpdateAddress.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Addresses import Address, Addresses
5 from PLC.Auth import PasswordAuth
6
7 class UpdateAddress(Method):
8     """
9     Updates the parameters of an existing address with the values in
10     address_fields.
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     can_update = lambda (field, value): field in \
20                  ['line1', 'line2', 'line3',
21                   'city', 'state', 'postalcode', 'country']
22     update_fields = dict(filter(can_update, Address.fields.items()))
23
24     accepts = [
25         PasswordAuth(),
26         Address.fields['address_id'],
27         update_fields
28         ]
29
30     returns = Parameter(int, '1 if successful')
31
32     def call(self, auth, address_id, address_fields):
33         if filter(lambda field: field not in self.update_fields, address_fields):
34             raise PLCInvalidArgument, "Invalid field specified"
35
36         # Get associated address details
37         addresses = Addresses(self.api, [address_id]).values()
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.update(address_fields)
47         address.sync()
48
49         return 1