all tag types use permission helpers in AuthorizeHelpers
[plcapi.git] / PLC / Methods / AddSiteTag.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 AddSiteTag(Method):
15     """
16     Sets the specified setting for the specified site
17     to the specified value.
18
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.
22
23     Returns the new site_tag_id (> 0) if successful, faults
24     otherwise.
25     """
26
27     roles = ['admin', 'pi', 'tech', 'user']
28
29     accepts = [
30         Auth(),
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'],
36         ]
37
38     returns = Parameter(int, 'New site_tag_id (> 0) if successful')
39
40     def call(self, auth, site_id, tag_type_id_or_name, value):
41         sites = Sites(self.api, [site_id])
42         if not sites:
43             raise PLCInvalidArgument, "No such site %r"%site_id
44         site = sites[0]
45
46         tag_types = TagTypes(self.api, [tag_type_id_or_name])
47         if not tag_types:
48             raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
49         tag_type = tag_types[0]
50
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']})
55
56         if len(conflicts) :
57             raise PLCInvalidArgument, "Site %d already has setting %d"%(site['site_id'],
58                                                                         tag_type['tag_type_id'])
59
60         # check authorizations
61         site.caller_may_write_tag(self.api,self.caller,tag_type)
62             
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
67
68         site_tag.sync()
69         self.object_ids = [site_tag['site_tag_id']]
70
71         return site_tag['site_tag_id']