f4ec6ddad4a86103a0f2d9e0ff550548c83d17eb
[plcapi.git] / PLC / Methods / AddPerson.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Persons import Person, Persons
6 from PLC.Auth import Auth
7
8 can_update = lambda (field, value): field in \
9              ['first_name', 'last_name', 'title',
10               'email', 'password', 'phone', 'url', 'bio']
11
12 class AddPerson(Method):
13     """
14     Adds a new account. Any fields specified in person_fields are
15     used, otherwise defaults are used.
16
17     Accounts are disabled by default. To enable an account, use
18     UpdatePerson().
19
20     Returns the new person_id (> 0) if successful, faults otherwise.
21     """
22
23     roles = ['admin', 'pi']
24
25     person_fields = dict(filter(can_update, Person.fields.items()))
26
27     accepts = [
28         Auth(),
29         person_fields
30         ]
31
32     returns = Parameter(int, 'New person_id (> 0) if successful')
33
34     def call(self, auth, person_fields):
35         person_fields = dict(filter(can_update, person_fields.items()))
36         person_fields['enabled'] = False
37         person = Person(self.api, person_fields)
38         person.sync()
39
40         # Logging variables
41         self.event_objects = {'Person': [person['person_id']]}
42         self.message = 'Person %d added' % person['person_id']  
43
44         return person['person_id']