- ignore rollback errors
[plcapi.git] / PLC / Methods / AddAttribute.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Attributes import Attribute, Attributes
5 from PLC.Auth import PasswordAuth
6
7 can_update = lambda (field, value): field in \
8              ['description', 'min_role_id']
9
10 class AddAttribute(Method):
11     """
12     Adds a new type of attribute. Any fields specified in
13     attribute_fields are used, otherwise defaults are used.
14
15     Returns the new attribute_id (> 0) if successful, faults otherwise.
16     """
17
18     roles = ['admin']
19
20     update_fields = dict(filter(can_update, Attribute.fields.items()))
21
22     accepts = [
23         PasswordAuth(),
24         Attribute.fields['name'],
25         update_fields
26         ]
27
28     returns = Parameter(int, 'New attribute_id (> 0) if successful')
29
30     def call(self, auth, name, attribute_fields = {}):
31         attribute_fields = dict(filter(can_update, attribute_fields.items()))
32         attribute = Attribute(self.api, attribute_fields)
33         attribute['name'] = name
34         attribute.sync()
35
36         return attribute['attribute_id']