Initial checkin of new API implementation
[plcapi.git] / PLC / Methods / AdmAddPerson.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 PasswordAuth
6
7 class AdmAddPerson(Method):
8     """
9     Adds a new account. Any fields specified in optional_vals are
10     used, otherwise defaults are used.
11
12     Accounts are disabled by default. To enable an account, use
13     AdmSetPersonEnabled() or AdmUpdatePerson().
14
15     Returns the new person_id (> 0) if successful, faults otherwise.
16     """
17
18     roles = ['admin', 'pi']
19
20     can_update = lambda (field, value): field in \
21                  ['title', 'email', 'password', 'phone', 'url', 'bio']
22     update_fields = dict(filter(can_update, Person.fields.items()))
23
24     accepts = [
25         PasswordAuth(),
26         Person.fields['first_name'],
27         Person.fields['last_name'],
28         update_fields
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
33     def call(self, auth, first_name, last_name, optional_vals = {}):
34         if filter(lambda field: field not in self.update_fields, optional_vals):
35             raise PLCInvalidArgument, "Invalid fields specified"
36
37         person = Person(self.api, optional_vals)
38         person['first_name'] = first_name
39         person['last_name'] = last_name
40         person['enabled'] = False
41         person.flush()
42
43         return person['person_id']