2 # Thierry Parmentelat - INRIA
4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Auth import Auth
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
14 class AddSiteTag(Method):
16 Sets the specified setting for the specified site
17 to the specified value.
19 Admins have full access. Non-admins need to
20 (1) have at least one of the roles attached to the tagtype,
21 and (2) belong in the same site as the tagged subject.
23 Returns the new site_tag_id (> 0) if successful, faults
27 roles = ['admin', 'pi', 'tech', 'user']
31 # no other way to refer to a site
32 SiteTag.fields['site_id'],
33 Mixed(TagType.fields['tag_type_id'],
34 TagType.fields['tagname']),
35 SiteTag.fields['value'],
38 returns = Parameter(int, 'New site_tag_id (> 0) if successful')
40 def call(self, auth, site_id, tag_type_id_or_name, value):
41 sites = Sites(self.api, [site_id])
43 raise PLCInvalidArgument, "No such site %r"%site_id
46 tag_types = TagTypes(self.api, [tag_type_id_or_name])
48 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
49 tag_type = tag_types[0]
51 # checks for existence - does not allow several different settings
52 conflicts = SiteTags(self.api,
53 {'site_id':site['site_id'],
54 'tag_type_id':tag_type['tag_type_id']})
57 raise PLCInvalidArgument, "Site %d already has setting %d"%(site['site_id'],
58 tag_type['tag_type_id'])
60 # check authorizations
61 site.caller_may_write_tag(self.api,self.caller,tag_type)
63 site_tag = SiteTag(self.api)
64 site_tag['site_id'] = site['site_id']
65 site_tag['tag_type_id'] = tag_type['tag_type_id']
66 site_tag['value'] = value
69 self.object_ids = [site_tag['site_tag_id']]
71 return site_tag['site_tag_id']