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