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