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