Add 'php/phpxmlrpc/' from commit 'cd5dbb4a511e7a616a61187a5de1a611a9748cbd'
[plcapi.git] / PLC / Methods / UpdatePCU.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.PCUs import PCU, PCUs
5 from PLC.Auth import Auth
6
7 can_update = lambda (field, value): field not in \
8              ['pcu_id', 'site_id']
9
10 class UpdatePCU(Method):
11     """
12     Updates the parameters of an existing PCU with the values in
13     pcu_fields.
14
15     Non-admins may only update PCUs at their sites.
16
17     Returns 1 if successful, faults otherwise.
18     """
19
20     roles = ['admin', 'pi', 'tech']
21
22     update_fields = dict(filter(can_update, PCU.fields.items()))
23
24     accepts = [
25         Auth(),
26         PCU.fields['pcu_id'],
27         update_fields
28         ]
29
30     returns = Parameter(int, '1 if successful')
31
32     def call(self, auth, pcu_id, pcu_fields):
33         pcu_fields = dict(filter(can_update, pcu_fields.items()))
34
35         # Get associated PCU details
36         pcus = PCUs(self.api, [pcu_id])
37         if not pcus:
38             raise PLCInvalidArgument, "No such PCU"
39         pcu = pcus[0]
40
41         if 'admin' not in self.caller['roles']:
42             if pcu['site_id'] not in self.caller['site_ids']:
43                 raise PLCPermissionDenied, "Not allowed to update that PCU"
44
45         pcu.update(pcu_fields)
46         pcu.update_last_updated(commit=False)
47         pcu.sync()
48
49         # Logging variables
50         self.event_objects = {'PCU': [pcu['pcu_id']]}
51         self.message = 'PCU %d updated: %s' % \
52                 (pcu['pcu_id'], ", ".join(pcu_fields.keys()))
53         return 1