2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Addresses import Address, Addresses
6 from PLC.Auth import Auth
8 can_update = lambda (field, value): field in \
9 ['line1', 'line2', 'line3',
10 'city', 'state', 'postalcode', 'country']
12 class UpdateAddress(Method):
14 Updates the parameters of an existing address with the values in
17 PIs may only update addresses of their own sites.
19 Returns 1 if successful, faults otherwise.
22 roles = ['admin', 'pi']
24 address_fields = dict(filter(can_update, Address.fields.items()))
28 Address.fields['address_id'],
32 returns = Parameter(int, '1 if successful')
34 def call(self, auth, address_id, address_fields):
35 address_fields = dict(filter(can_update, address_fields.items()))
37 # Get associated address details
38 addresses = Addresses(self.api, [address_id])
40 raise PLCInvalidArgument, "No such address"
41 address = addresses[0]
43 if 'admin' not in self.caller['roles']:
44 if address['site_id'] not in self.caller['site_ids']:
45 raise PLCPermissionDenied, "Address must be associated with one of your sites"
47 address.update(address_fields)
51 self.event_objects = {'Address': [address['address_id']]}
52 self.message = 'Address %d updated: %s' % \
53 (address['address_id'], ", ".join(address_fields.keys()))