3830d8099125ea30dbef8e41645138ab6afa0253
[plcapi.git] / PLC / Methods / GetPersonTags.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Filter import Filter
8 from PLC.Auth import Auth
9
10 from PLC.Persons import Person, Persons
11 from PLC.PersonTags import PersonTag, PersonTags
12
13 class GetPersonTags(Method):
14     """
15     Returns an array of structs containing details about
16     persons and related settings.
17
18     If person_tag_filter is specified and is an array of
19     person setting identifiers, only person settings matching
20     the filter will be returned. If return_fields is specified, only
21     the specified details will be returned.
22     """
23
24     roles = ['admin', 'pi', 'user', 'tech']
25
26     accepts = [
27         Auth(),
28         Mixed([PersonTag.fields['person_tag_id']],
29               Parameter(int,"Person setting id"),
30               Filter(PersonTag.fields)),
31         Parameter([str], "List of fields to return", nullok = True)
32         ]
33
34     returns = [PersonTag.fields]
35
36
37     def call(self, auth, person_tag_filter = None, return_fields = None):
38
39         # only persons can call this (as per roles, but..)
40         if not isinstance(self.caller,Person):
41             return []
42
43         # If we are not admin, make sure to only return viewable accounts
44         valid_person_ids=None
45         added_fields=[]
46         if 'admin' not in self.caller['roles']:
47             # Get accounts that we are able to view
48             valid_person_ids = [self.caller['person_id']]
49             if 'pi' in self.caller['roles'] and self.caller['site_ids']:
50                 sites = Sites(self.api, self.caller['site_ids'])
51                 for site in sites:
52                     valid_person_ids += site['person_ids']
53
54             if not valid_person_ids:
55                 return []
56             
57             # if we have to filter out on person_id, make sure this is returned from db
58             if return_fields:
59                 added_fields = set(['person_id']).difference(return_fields)
60                 return_fields += added_fields
61
62         person_tags = PersonTags(self.api, person_tag_filter, return_fields)
63         
64         if valid_person_ids is not None:
65             person_tags = [ person_tag for person_tag in person_tags 
66                             if person_tag['person_id'] in valid_person_ids]
67
68         # Remove added fields if not initially specified
69         if added_fields:
70             for person_tag in person_tags:
71                 for field in added_fields:
72                     if field in person_tag:
73                         del person_tag[field]
74         return person_tags