force import of AuthorizeHelpers
[plcapi.git] / PLC / Methods / DeleteSiteTag.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 # need to import so the core classes get decorated with caller_may_write_tag
15 from PLC.AuthorizeHelpers import AuthorizeHelpers
16
17 class DeleteSiteTag(Method):
18     """
19     Deletes the specified site setting
20
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.
24
25     Returns 1 if successful, faults otherwise.
26     """
27
28     roles = ['admin', 'pi', 'user']
29
30     accepts = [
31         Auth(),
32         SiteTag.fields['site_tag_id']
33         ]
34
35     returns = Parameter(int, '1 if successful')
36
37     def call(self, auth, site_tag_id):
38         site_tags = SiteTags(self.api, [site_tag_id])
39         if not site_tags:
40             raise PLCInvalidArgument, "No such site tag %r"%site_tag_id
41         site_tag = site_tags[0]
42
43         tag_type_id = site_tag['tag_type_id']
44         tag_type = TagTypes (self.api,[tag_type_id])[0]
45
46         sites = Sites (self.api, site_tag['site_id'])
47         if not sites:
48             raise PLCInvalidArgument, "No such site %d"%site_tag['site_id']
49         site=sites[0]
50         
51         # check authorizations
52         site.caller_may_write_tag(self.api,self.caller,tag_type)
53
54         site_tag.delete()
55         self.object_ids = [site_tag['site_tag_id']]
56
57         return 1