slice attribute methods
[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 class AddAttribute(Method):
8     """
9     Adds a new type of attribute. Any fields specified in optional_vals
10     are used, otherwise defaults are used.
11
12     Returns the new attribute_id (> 0) 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         Attribute.fields['name'],
24         update_fields
25         ]
26
27     returns = Parameter(int, 'New attribute_id (> 0) if successful')
28
29     def call(self, auth, name, optional_vals = {}):
30         if filter(lambda field: field not in self.update_fields, optional_vals):
31             raise PLCInvalidArgument, "Invalid field specified"
32
33         attribute = Attribute(self.api, optional_vals)
34         attribute['name'] = name
35         attribute.sync()
36
37         return attribute['attribute_id']