2 # Thierry Parmentelat - INRIA
4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Auth import Auth
9 from PLC.Persons import Person, Persons
10 from PLC.TagTypes import TagType, TagTypes
11 from PLC.PersonTags import PersonTag, PersonTags
13 from PLC.AuthorizeHelpers import AuthorizeHelpers
15 class AddPersonTag(Method):
17 Sets the specified setting for the specified person
18 to the specified value.
20 Admins have full access. Non-admins can change their own tags.
22 Returns the new person_tag_id (> 0) if successful, faults
26 roles = ['admin', 'pi', 'tech', 'user']
30 # no other way to refer to a person
31 PersonTag.fields['person_id'],
32 Mixed(TagType.fields['tag_type_id'],
33 TagType.fields['tagname']),
34 PersonTag.fields['value'],
37 returns = Parameter(int, 'New person_tag_id (> 0) if successful')
39 def call(self, auth, person_id, tag_type_id_or_name, value):
40 persons = Persons(self.api, [person_id])
42 raise PLCInvalidArgument, "No such person %r"%person_id
45 tag_types = TagTypes(self.api, [tag_type_id_or_name])
47 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
48 tag_type = tag_types[0]
50 # checks for existence - does not allow several different settings
51 conflicts = PersonTags(self.api, {'person_id':person['person_id'],
52 'tag_type_id':tag_type['tag_type_id']})
55 raise PLCInvalidArgument, "Person %d (%s) already has setting %d"% \
56 (person['person_id'],person['email'], tag_type['tag_type_id'])
58 # check authorizations
59 if 'admin' in self.caller['roles']:
61 # user can change tags on self
62 elif AuthorizeHelpers.person_access_person (self.api, self.caller, person):
65 raise PLCPermissionDenied, "%s: you can only change your own tags"%self.name
68 person_tag = PersonTag(self.api)
69 person_tag['person_id'] = person['person_id']
70 person_tag['tag_type_id'] = tag_type['tag_type_id']
71 person_tag['value'] = value
74 self.object_ids = [person_tag['person_tag_id']]
76 return person_tag['person_tag_id']