- make Add() calling convention consistent among all functions that
[plcapi.git] / PLC / Methods / UpdatePerson.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 can_update = lambda (field, value): field in \
8              ['first_name', 'last_name', 'title', 'email',
9               'password', 'phone', 'url', 'bio', 'accepted_aup',
10               'enabled']
11
12 class UpdatePerson(Method):
13     """
14     Updates a person. Only the fields specified in person_fields are
15     updated, all other fields are left untouched.
16
17     To remove a value without setting a new one in its place (for
18     example, to remove an address from the person), specify -1 for int
19     and double fields and 'null' for string fields. first_name and
20     last_name cannot be unset.
21     
22     Users and techs can only update themselves. PIs can only update
23     themselves and other non-PIs at their sites.
24
25     Returns 1 if successful, faults otherwise.
26     """
27
28     roles = ['admin', 'pi', 'user', 'tech']
29
30     person_fields = dict(filter(can_update, Person.fields.items()))
31     for field in person_fields.values():
32         field.optional = True
33
34     accepts = [
35         PasswordAuth(),
36         Mixed(Person.fields['person_id'],
37               Person.fields['email']),
38         person_fields
39         ]
40
41     returns = Parameter(int, '1 if successful')
42
43     def call(self, auth, person_id_or_email, person_fields):
44         person_fields = dict(filter(can_update, person_fields.items()))
45
46         # Get account information
47         persons = Persons(self.api, [person_id_or_email])
48         if not persons:
49             raise PLCInvalidArgument, "No such account"
50
51         person = persons.values()[0]
52
53         # Authenticated function
54         assert self.caller is not None
55
56         # Check if we can update this account
57         if not self.caller.can_update(person):
58             raise PLCPermissionDenied, "Not allowed to update specified account"
59
60         person.update(person_fields)
61         person.sync()
62
63         return 1