Add PersonTags as well. Performed a simple test but not extensive regression
[plcapi.git] / PLC / Methods / AddPersonTag.py
1 # $Id: AddPersonTag.py 14587 2009-07-19 13:18:50Z thierry $
2 # $URL: http://svn.planet-lab.org/svn/PLCAPI/tags/PLCAPI-4.3-27/PLC/Methods/AddPersonTag.py $
3 #
4 # Thierry Parmentelat - INRIA
5 #
6 # $Revision: 14587 $
7 #
8 from PLC.Faults import *
9 from PLC.Method import Method
10 from PLC.Parameter import Parameter, Mixed
11 from PLC.Auth import Auth
12
13 from PLC.TagTypes import TagType, TagTypes
14 from PLC.PersonTags import PersonTag, PersonTags
15 from PLC.Persons import Person, Persons
16
17 from PLC.Nodes import Nodes
18
19 class AddPersonTag(Method):
20     """
21     Sets the specified setting for the specified person
22     to the specified value.
23
24     In general only tech(s), PI(s) and of course admin(s) are allowed to
25     do the change, but this is defined in the tag type object.
26
27     Returns the new person_tag_id (> 0) if successful, faults
28     otherwise.
29     """
30
31     roles = ['admin', 'pi', 'tech', 'user']
32
33     accepts = [
34         Auth(),
35         # no other way to refer to a person
36         PersonTag.fields['person_id'],
37         Mixed(TagType.fields['tag_type_id'],
38               TagType.fields['tagname']),
39         PersonTag.fields['value'],
40         ]
41
42     returns = Parameter(int, 'New person_tag_id (> 0) if successful')
43
44     object_type = 'Person'
45
46
47     def call(self, auth, person_id, tag_type_id_or_name, value):
48         persons = Persons(self.api, [person_id])
49         if not persons:
50             raise PLCInvalidArgument, "No such person %r"%person_id
51         person = persons[0]
52
53         tag_types = TagTypes(self.api, [tag_type_id_or_name])
54         if not tag_types:
55             raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
56         tag_type = tag_types[0]
57
58         # checks for existence - does not allow several different settings
59         conflicts = PersonTags(self.api,
60                                         {'person_id':person['person_id'],
61                                          'tag_type_id':tag_type['tag_type_id']})
62
63         if len(conflicts) :
64             raise PLCInvalidArgument, "Person %d already has setting %d"%(person['person_id'],
65                                                                                tag_type['tag_type_id'])
66
67         # check permission : it not admin, is the user affiliated with the same site as this person
68         if 'admin' not in self.caller['roles']:
69             # check caller is affiliated with at least one of Person's sites
70             if len(set(person['site_ids']) & set(self.caller['site_ids'])) == 0:
71                 raise PLCPermissionDenied, "Not a member of the person's sites: %s"%person['site_ids']
72             
73             required_min_role = tag_type ['min_role_id']
74             if required_min_role is not None and \
75                     min(self.caller['role_ids']) > required_min_role:
76                 raise PLCPermissionDenied, "Not allowed to modify the specified person setting, requires role %d",required_min_role
77
78         person_tag = PersonTag(self.api)
79         person_tag['person_id'] = person['person_id']
80         person_tag['tag_type_id'] = tag_type['tag_type_id']
81         person_tag['value'] = value
82
83         person_tag.sync()
84         self.object_ids = [person_tag['person_tag_id']]
85
86         return person_tag['person_tag_id']