- ignore rollback errors
[plcapi.git] / PLC / Methods / UpdateAttribute.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 UpdateAttribute(Method):
11     """
12     Updates the parameters of an existing attribute with the values in
13     attribute_fields.
14
15     Returns 1 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         Mixed(Attribute.fields['attribute_id'],
25               Attribute.fields['name']),
26         update_fields
27         ]
28
29     returns = Parameter(int, '1 if successful')
30
31     def call(self, auth, attribute_id_or_name, attribute_fields):
32         attribute_fields = dict(filter(can_update, attribute_fields.items()))
33
34         attributes = Attributes(self.api, [attribute_id_or_name]).values()
35         if not attributes:
36             raise PLCInvalidArgument, "No such attribute"
37         attribute = attributes[0]
38
39         attribute.update(attribute_fields)
40
41         attribute.sync()
42
43         return 1