d92d799f737c08562198be22fb53421718091ae8
[plcapi.git] / PLC / Methods / UpdatePCU.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.PCUs import PCU, PCUs
6 from PLC.Auth import Auth
7
8 can_update = lambda (field, value): field not in \
9              ['pcu_id', 'site_id']
10
11 class UpdatePCU(Method):
12     """
13     Updates the parameters of an existing PCU with the values in
14     pcu_fields.
15
16     Non-admins may only update PCUs at their sites.
17
18     Returns 1 if successful, faults otherwise.
19     """
20
21     roles = ['admin', 'pi', 'tech']
22
23     update_fields = dict(filter(can_update, PCU.fields.items()))
24
25     accepts = [
26         Auth(),
27         PCU.fields['pcu_id'],
28         update_fields
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
33     def call(self, auth, pcu_id, pcu_fields):
34         pcu_fields = dict(filter(can_update, pcu_fields.items()))
35
36         # Get associated PCU details
37         pcus = PCUs(self.api, [pcu_id])
38         if not pcus:
39             raise PLCInvalidArgument, "No such PCU"
40         pcu = pcus[0]
41
42         if 'admin' not in self.caller['roles']:
43             if pcu['site_id'] not in self.caller['site_ids']:
44                 raise PLCPermissionDenied, "Not allowed to update that PCU"
45
46         pcu.update(pcu_fields)
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