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 from PLC.AuthorizeHelpers import AuthorizeHelpers
16 class AddSiteTag(Method):
18 Sets the specified setting for the specified site
19 to the specified value.
21 Admins have full access. Non-admins need to
22 (1) have at least one of the roles attached to the tagtype,
23 and (2) belong in the same site as the tagged subject.
25 Returns the new site_tag_id (> 0) if successful, faults
29 roles = ['admin', 'pi', 'tech', 'user']
33 # no other way to refer to a site
34 SiteTag.fields['site_id'],
35 Mixed(TagType.fields['tag_type_id'],
36 TagType.fields['tagname']),
37 SiteTag.fields['value'],
40 returns = Parameter(int, 'New site_tag_id (> 0) if successful')
42 def call(self, auth, site_id, tag_type_id_or_name, value):
43 sites = Sites(self.api, [site_id])
45 raise PLCInvalidArgument, "No such site %r"%site_id
48 tag_types = TagTypes(self.api, [tag_type_id_or_name])
50 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
51 tag_type = tag_types[0]
53 # checks for existence - does not allow several different settings
54 conflicts = SiteTags(self.api,
55 {'site_id':site['site_id'],
56 'tag_type_id':tag_type['tag_type_id']})
59 raise PLCInvalidArgument, "Site %d already has setting %d"%(site['site_id'],
60 tag_type['tag_type_id'])
62 # check authorizations
63 if 'admin' in self.caller['roles']:
65 elif not AuthorizeHelpers.caller_may_access_tag_type (self.api, self.caller, tag_type):
66 raise PLCPermissionDenied, "%s, forbidden tag %s"%(self.name,tag_type['tagname'])
67 elif AuthorizeHelpers.person_in_site (self.api, self.caller, site):
70 raise PLCPermissionDenied, "%s: you must be part of the subject site"%self.name
72 site_tag = SiteTag(self.api)
73 site_tag['site_id'] = site['site_id']
74 site_tag['tag_type_id'] = tag_type['tag_type_id']
75 site_tag['value'] = value
78 self.object_ids = [site_tag['site_tag_id']]
80 return site_tag['site_tag_id']