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 $
4 # Thierry Parmentelat - INRIA
8 from PLC.Faults import *
9 from PLC.Method import Method
10 from PLC.Parameter import Parameter, Mixed
11 from PLC.Auth import Auth
13 from PLC.TagTypes import TagType, TagTypes
14 from PLC.PersonTags import PersonTag, PersonTags
15 from PLC.Persons import Person, Persons
17 from PLC.Nodes import Nodes
19 class AddPersonTag(Method):
21 Sets the specified setting for the specified person
22 to the specified value.
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.
27 Returns the new person_tag_id (> 0) if successful, faults
31 roles = ['admin', 'pi', 'tech', 'user']
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'],
42 returns = Parameter(int, 'New person_tag_id (> 0) if successful')
44 object_type = 'Person'
47 def call(self, auth, person_id, tag_type_id_or_name, value):
48 persons = Persons(self.api, [person_id])
50 raise PLCInvalidArgument, "No such person %r"%person_id
53 tag_types = TagTypes(self.api, [tag_type_id_or_name])
55 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
56 tag_type = tag_types[0]
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']})
64 raise PLCInvalidArgument, "Person %d already has setting %d"%(person['person_id'],
65 tag_type['tag_type_id'])
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']
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
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
84 self.object_ids = [person_tag['person_tag_id']]
86 return person_tag['person_tag_id']