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