slice attribute methods
[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 class UpdateAttribute(Method):
8     """
9     Updates the parameters of an existing attribute with the values in
10     update_fields.
11
12     Returns 1 if successful, faults otherwise.
13     """
14
15     roles = ['admin']
16
17     can_update = lambda (field, value): field in \
18                  ['description', 'min_role_id']
19     update_fields = dict(filter(can_update, Attribute.fields.items()))
20
21     accepts = [
22         PasswordAuth(),
23         Mixed(Attribute.fields['attribute_id'],
24               Attribute.fields['name']),
25         update_fields
26         ]
27
28     returns = Parameter(int, '1 if successful')
29
30     def call(self, auth, attribute_id_or_name, update_fields):
31         if filter(lambda field: field not in self.update_fields, update_fields):
32             raise PLCInvalidArgument, "Invalid field specified"
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(update_fields)
40
41         attribute.sync()
42
43         return 1