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