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