reviewing the tags permission system
[plcapi.git] / PLC / Methods / UpdateTagType.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.TagTypes import TagType, TagTypes
8 from PLC.Auth import Auth
9
10 can_update = lambda (field, value): field in \
11              ['tagname', 'description', 'category']
12
13 class UpdateTagType(Method):
14     """
15     Updates the parameters of an existing tag type
16     with the values in tag_type_fields.
17
18     Returns 1 if successful, faults otherwise.
19     """
20
21     roles = ['admin']
22
23     tag_type_fields = dict(filter(can_update, TagType.fields.items()))
24
25     accepts = [
26         Auth(),
27         Mixed(TagType.fields['tag_type_id'],
28               TagType.fields['tagname']),
29         tag_type_fields
30         ]
31
32     returns = Parameter(int, '1 if successful')
33
34     def call(self, auth, tag_type_id_or_name, tag_type_fields):
35         tag_type_fields = dict(filter(can_update, tag_type_fields.items()))
36
37         tag_types = TagTypes(self.api, [tag_type_id_or_name])
38         if not tag_types:
39             raise PLCInvalidArgument, "No such tag type"
40         tag_type = tag_types[0]
41
42         tag_type.update(tag_type_fields)
43         tag_type.sync()
44         self.object_ids = [tag_type['tag_type_id']]
45
46         return 1