Setting tag plcapi-5.4-2
[plcapi.git] / PLC / Methods / DeletePersonTag.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.Persons import Person, Persons
10 from PLC.TagTypes import TagType, TagTypes
11 from PLC.PersonTags import PersonTag, PersonTags
12
13 # need to import so the core classes get decorated with caller_may_write_tag
14 from PLC.AuthorizeHelpers import AuthorizeHelpers
15
16 class DeletePersonTag(Method):
17     """
18     Deletes the specified person setting
19
20     Admins have full access.  Non-admins can change their own tags.
21
22     Returns 1 if successful, faults otherwise.
23     """
24
25     roles = ['admin', 'pi', 'user']
26
27     accepts = [
28         Auth(),
29         PersonTag.fields['person_tag_id']
30         ]
31
32     returns = Parameter(int, '1 if successful')
33
34     def call(self, auth, person_tag_id):
35         person_tags = PersonTags(self.api, [person_tag_id])
36         if not person_tags:
37             raise PLCInvalidArgument, "No such person tag %r"%person_tag_id
38         person_tag = person_tags[0]
39
40         tag_type_id = person_tag['tag_type_id']
41         tag_type = TagTypes (self.api,[tag_type_id])[0]
42
43         persons = Persons (self.api, person_tag['person_id'])
44         if not persons:
45             raise PLCInvalidArgument, "No such person %d"%person_tag['person_id']
46         person=persons[0]
47
48         # check authorizations
49         person.caller_may_write_tag(self.api,self.caller,tag_type)
50
51         person_tag.delete()
52         self.object_ids = [person_tag['person_tag_id']]
53
54         return 1