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