d0b3d8e58b454735a49b91142996575fa8264e31
[plcapi.git] / PLC / Methods / UpdateNodeTag.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4
5 from PLC.Faults import *
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Auth import Auth
9
10 from PLC.Sites import Sites
11 from PLC.Nodes import Node, Nodes
12 from PLC.TagTypes import TagType, TagTypes
13 from PLC.NodeTags import NodeTag, NodeTags
14
15 # need to import so the core classes get decorated with caller_may_write_tag
16 from PLC.AuthorizeHelpers import AuthorizeHelpers
17
18 class UpdateNodeTag(Method):
19     """
20     Updates the value of an existing node tag
21
22     Admins have full access.  Non-admins need to 
23     (1) have at least one of the roles attached to the tagtype, 
24     and (2) belong in the same site as the tagged subject.
25
26     Returns 1 if successful, faults otherwise.
27     """
28
29     roles = ['admin', 'pi', 'tech', 'user']
30
31     accepts = [
32         Auth(),
33         NodeTag.fields['node_tag_id'],
34         NodeTag.fields['value']
35         ]
36
37     returns = Parameter(int, '1 if successful')
38
39     def call(self, auth, node_tag_id, value):
40         node_tags = NodeTags(self.api, [node_tag_id])
41         if not node_tags:
42             raise PLCInvalidArgument, "No such node tag %r"%node_tag_id
43         node_tag = node_tags[0]
44
45         tag_type_id = node_tag['tag_type_id']
46         tag_type = TagTypes (self.api,[tag_type_id])[0]
47
48         nodes = Nodes (self.api, node_tag['node_id'])
49         if not nodes:
50             raise PLCInvalidArgument, "No such node %d"%node_tag['node_id']
51         node=nodes[0]
52
53         # check authorizations
54         node.caller_may_write_tag(self.api,self.caller,tag_type)
55
56         node_tag['value'] = value
57         node_tag.sync()
58
59         self.object_ids = [node_tag['node_tag_id']]
60         return 1