8fc868c2e67171c2bf017eee8615e1d49aab5c2b
[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         
36         accepted_type_fields = dict(filter(can_update, tag_type_fields.items()))
37         rejected_keys = [ k for k in tag_type_fields if k not in accepted_type_fields ]
38         if rejected_keys:
39             error="Cannot update TagType column(s) %r"%rejected_keys
40             if 'roles' in rejected_keys or 'role_ids' in rejected_keys:
41                 error += " see AddRoleToTagType DeleteRoleFromTagType"
42             raise PLCInvalidArgument, error
43
44         tag_types = TagTypes(self.api, [tag_type_id_or_name])
45         if not tag_types:
46             raise PLCInvalidArgument, "No such tag type"
47         tag_type = tag_types[0]
48
49         tag_type.update(accepted_type_fields)
50         tag_type.sync()
51         self.object_ids = [tag_type['tag_type_id']]
52
53         return 1