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