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