5dc1a8c299cbfe11872250082b8376e06b4ced29
[plcapi.git] / PLC / Methods / UpdateSiteTag.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.Auth import Auth
8
9 from PLC.Sites import Site, Sites
10 from PLC.Nodes import Nodes
11 from PLC.TagTypes import TagType, TagTypes
12 from PLC.SiteTags import SiteTag, SiteTags
13
14 class UpdateSiteTag(Method):
15     """
16     Updates the value of an existing site setting
17
18     Admins have full access.  Non-admins need to 
19     (1) have at least one of the roles attached to the tagtype, 
20     and (2) belong in the same site as the tagged subject.
21
22     Returns 1 if successful, faults otherwise.
23     """
24
25     roles = ['admin', 'pi', 'tech', 'user']
26
27     accepts = [
28         Auth(),
29         SiteTag.fields['site_tag_id'],
30         SiteTag.fields['value']
31         ]
32
33     returns = Parameter(int, '1 if successful')
34
35     def call(self, auth, site_tag_id, value):
36         site_tags = SiteTags(self.api, [site_tag_id])
37         if not site_tags:
38             raise PLCInvalidArgument, "No such site setting %r"%site_tag_id
39         site_tag = site_tags[0]
40
41         tag_type_id = site_tag['tag_type_id']
42         tag_type = TagTypes (self.api,[tag_type_id])[0]
43
44         sites = Sites (self.api, site_tag['site_id'])
45         if not sites:
46             raise PLCInvalidArgument, "No such site %d"%site_tag['site_id']
47         site=sites[0]
48         
49         # check authorizations
50         site.caller_may_write_tag(self.api,self.caller,tag_type)
51             
52         site_tag['value'] = value
53         site_tag.sync()
54
55         self.object_ids = [site_tag['site_tag_id']]
56         return 1