add interface to pcus
[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 PasswordAuth
6
7 class UpdatePCU(Method):
8     """
9     Updates the parameters of an existing PCU with the values in
10     pcu_fields.
11
12     Non-admins may only update their own keys.
13
14     Returns 1 if successful, faults otherwise.
15     """
16
17     roles = ['admin', 'pi', 'tech']
18
19     can_update = lambda (field, value): field not in \
20                  ['pcu_id', 'site_id']
21     update_fields = dict(filter(can_update, PCU.fields.items()))
22
23     accepts = [
24         PasswordAuth(),
25         PCU.fields['pcu_id'],
26         update_fields
27         ]
28
29     returns = Parameter(int, '1 if successful')
30
31     def call(self, auth, pcu_id, pcu_fields):
32         # Make sure only valid fields are specified
33         if filter(lambda field: field not in self.update_fields, pcu_fields):
34             raise PLCInvalidArgument, "Invalid field specified"
35
36         # Get associated PCU details
37         pcus = PCUs(self.api, [pcu_id]).values()
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 not pcu_ids:
44                 ok = False
45                 sites = Sites(self.api, self.caller['site_ids']).values()
46                 for site in sites:
47                     if pcu['pcu_id'] in site['pcu_ids']:
48                         ok = True
49                         break
50                 if not ok:
51                     raise PLCPermissionDenied, "Not allowed to update that PCU"
52
53         pcu.update(pcu_fields)
54         pcu.sync()
55
56         return 1