c7013aae838772e9fe35e320724aceb7856189fd
[plcapi.git] / PLC / Methods / AddPCUType.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.PCUTypes import PCUType, PCUTypes
6 from PLC.Auth import Auth
7
8 can_update = lambda (field, value): field in \
9              ['model', 'name']  
10
11 class AddPCUType(Method):
12     """
13     Adds a new pcu type.
14
15     Returns the new pcu_type_id (> 0) if successful, faults otherwise.
16     """
17
18     roles = ['admin']
19
20     pcu_type_fields = dict(filter(can_update, PCUType.fields.items()))
21
22     accepts = [
23         Auth(),
24         pcu_type_fields
25         ]
26
27     returns = Parameter(int, 'New pcu_type_id (> 0) if successful')
28
29     
30     def call(self, auth, pcu_type_fields):
31         pcu_type_fields = dict(filter(can_update, pcu_type_fields.items()))
32         pcu_type = PCUType(self.api, pcu_type_fields)
33         pcu_type.sync()
34         self.event_object = {'PCUType': [pcu_type['pcu_type_id']]}      
35
36         return pcu_type['pcu_type_id']