Initial checkin of new API implementation
[plcapi.git] / PLC / Methods / AdmGetPersons.py
1 import os
2
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 PasswordAuth
8
9 class AdmGetPersons(Method):
10     """
11     Return an array of dictionaries containing details about the
12     specified accounts.
13
14     Admins may retrieve details about all accounts by not specifying
15     person_id_or_email_list or by specifying an empty list. Users and
16     techs may only retrieve details about themselves. PIs may retrieve
17     details about themselves and others at their sites.
18
19     If return_fields is specified, only the specified fields will be
20     returned, if set. Otherwise, the default set of fields returned is:
21
22     """
23
24     roles = ['admin', 'pi', 'user', 'tech']
25
26     accepts = [
27         PasswordAuth(),
28         [Mixed(Person.fields['person_id'],
29                Person.fields['email'])],
30         Parameter([str], 'List of fields to return')
31         ]
32
33     # Filter out password and deleted fields
34     can_return = lambda (field, value): field not in ['password', 'deleted']
35     default_fields = dict(filter(can_return, Person.default_fields.items()))
36     return_fields = dict(filter(can_return, Person.all_fields.items()))
37     returns = [return_fields]
38
39     def __init__(self, *args, **kwds):
40         Method.__init__(self, *args, **kwds)
41         # Update documentation with list of default fields returned
42         self.__doc__ += os.linesep.join(self.default_fields.keys())
43
44     def call(self, auth, person_id_or_email_list = None, return_fields = None):
45         # Make sure that only valid fields are specified
46         if return_fields is None:
47             return_fields = self.return_fields
48         elif filter(lambda field: field not in self.return_fields, return_fields):
49             raise PLCInvalidArgument, "Invalid return field specified"
50
51         # Authenticated function
52         assert self.caller is not None
53
54         # Only admins can not specify person_id_or_email_list or
55         # specify an empty list.
56         if not person_id_or_email_list and 'admin' not in self.caller['roles']:
57             raise PLCInvalidArgument, "List of accounts to retrieve not specified"
58
59         # Get account information
60         persons = Persons(self.api, person_id_or_email_list)
61
62         # Filter out accounts that are not viewable and turn into list
63         persons = filter(self.caller.can_view, persons.values())
64
65         # Filter out undesired or None fields (XML-RPC cannot marshal
66         # None) and turn each person into a real dict.
67         valid_return_fields_only = lambda (key, value): \
68                                    key in return_fields and value is not None
69         persons = [dict(filter(valid_return_fields_only, person.items())) \
70                    for person in persons]
71                     
72         return persons