Initial checkin of new API implementation
[plcapi.git] / PLC / Methods / AdmGetPersonSites.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.Sites import Site, Sites
6 from PLC.Auth import PasswordAuth
7
8 from PLC.Methods.AdmGetPersons import AdmGetPersons
9
10 class AdmGetPersonSites(AdmGetPersons):
11     """
12     Returns the sites that the specified person is associated with as
13     an array of site identifiers.
14
15     Admins may retrieve details about anyone. Users and techs may only
16     retrieve details about themselves. PIs may retrieve details about
17     themselves and others at their sites.
18     """
19
20     roles = ['admin', 'pi', 'user', 'tech']
21
22     accepts = [
23         PasswordAuth(),
24         Mixed(Person.fields['person_id'],
25               Person.fields['email'])
26         ]
27
28     returns = [Site.fields['site_id']]
29
30     def call(self, auth, person_id_or_email):
31         persons = AdmGetPersons.call(self, auth, [person_id_or_email])
32
33         # AdmGetPersons() validates person_id_or_email
34         assert persons
35         person = persons[0]
36
37         # Filter out deleted sites
38         sites = Sites(self.api, persons['site_ids'])
39
40         return [site['site_id'] for site in sites]