svn keywords
[plcapi.git] / PLC / Methods / UpdateAddress.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.Addresses import Address, Addresses
7 from PLC.Auth import Auth
8
9 can_update = lambda (field, value): field in \
10              ['line1', 'line2', 'line3',
11               'city', 'state', 'postalcode', 'country']
12
13 class UpdateAddress(Method):
14     """
15     Updates the parameters of an existing address with the values in
16     address_fields.
17
18     PIs may only update addresses of their own sites.
19
20     Returns 1 if successful, faults otherwise.
21     """
22
23     roles = ['admin', 'pi']
24
25     address_fields = dict(filter(can_update, Address.fields.items()))
26
27     accepts = [
28         Auth(),
29         Address.fields['address_id'],
30         address_fields
31         ]
32
33     returns = Parameter(int, '1 if successful')
34
35     def call(self, auth, address_id, address_fields):
36         address_fields = dict(filter(can_update, address_fields.items()))
37
38         # Get associated address details
39         addresses = Addresses(self.api, [address_id])
40         if not addresses:
41             raise PLCInvalidArgument, "No such address"
42         address = addresses[0]
43
44         if 'admin' not in self.caller['roles']:
45             if address['site_id'] not in self.caller['site_ids']:
46                 raise PLCPermissionDenied, "Address must be associated with one of your sites"
47
48         address.update(address_fields)
49         address.sync()
50         
51         # Logging variables
52         self.event_objects = {'Address': [address['address_id']]}
53         self.message = 'Address %d updated: %s' % \
54                 (address['address_id'], ", ".join(address_fields.keys()))
55         
56         return 1