create logfile plcapi.log in /var/log and chown it to apache
[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 Auth
6
7 can_update = lambda (field, value): field in \
8              ['line1', 'line2', 'line3',
9               'city', 'state', 'postalcode', 'country']
10
11 class UpdateAddress(Method):
12     """
13     Updates the parameters of an existing address with the values in
14     address_fields.
15
16     PIs may only update addresses of their own sites.
17
18     Returns 1 if successful, faults otherwise.
19     """
20
21     roles = ['admin', 'pi']
22
23     address_fields = dict(filter(can_update, Address.fields.items()))
24
25     accepts = [
26         Auth(),
27         Address.fields['address_id'],
28         address_fields
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
33     def call(self, auth, address_id, address_fields):
34         address_fields = dict(filter(can_update, address_fields.items()))
35
36         # Get associated address details
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.update(address_fields)
47         address.sync()
48
49         # Logging variables
50         self.event_objects = {'Address': [address['address_id']]}
51         self.message = 'Address %d updated: %s' % \
52                 (address['address_id'], ", ".join(address_fields.keys()))
53
54         return 1