add interface to pcus
[plcapi.git] / PLC / Methods / DeletePCU.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 DeletePCU(Method):
8     """
9     Deletes a PCU.
10
11     Non-admins may only delete PCUs at their sites.
12
13     Returns 1 if successful, faults otherwise.
14     """
15
16     roles = ['admin', 'pi', 'tech']
17
18     accepts = [
19         PasswordAuth(),
20         PCU.fields['pcu_id'],
21         ]
22
23     returns = Parameter(int, '1 if successful')
24
25     def call(self, auth, pcu_id):
26         # Get associated PCU details
27         pcus = PCUs(self.api, [pcu_id]).values()
28         if not pcus:
29             raise PLCInvalidArgument, "No such PCU"
30         pcu = pcus[0]
31
32         if 'admin' not in self.caller['roles']:
33             if not pcu_ids:
34                 ok = False
35                 sites = Sites(self.api, self.caller['site_ids']).values()
36                 for site in sites:
37                     if pcu['pcu_id'] in site['pcu_ids']:
38                         ok = True
39                         break
40                 if not ok:
41                     raise PLCPermissionDenied, "Not allowed to delete that PCU"
42
43         pcu.delete()
44
45         return 1