X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FMethods%2FGetPCUs.py;h=c59fa40fafe7ca7987fd9d96c3b9e8d5cb76029f;hb=c3d614f7f131b986e6193960032b0a50ff1fd624;hp=1f8e201889b42dc16392a50a9d7a50e4b11df988;hpb=8e199f468aaf48ac1dad3090c149711f38aa6c38;p=plcapi.git diff --git a/PLC/Methods/GetPCUs.py b/PLC/Methods/GetPCUs.py index 1f8e201..c59fa40 100644 --- a/PLC/Methods/GetPCUs.py +++ b/PLC/Methods/GetPCUs.py @@ -2,6 +2,9 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed from PLC.Filter import Filter +from PLC.Sites import Site, Sites +from PLC.Persons import Person, Persons +from PLC.Nodes import Node, Nodes from PLC.PCUs import PCU, PCUs from PLC.Auth import Auth @@ -17,7 +20,7 @@ class GetPCUs(Method): their sites. """ - roles = ['admin', 'pi', 'tech'] + roles = ['admin', 'pi', 'tech', 'node'] accepts = [ Auth(), @@ -27,18 +30,20 @@ class GetPCUs(Method): ] returns = [PCU.fields] - event_type = 'Get' - object_type = 'PCU' def call(self, auth, pcu_filter = None, return_fields = None): - # If we are not admin, make sure to only return our own PCUs - if 'admin' not in self.caller['roles']: - # Get list of PCUs that we are able to view + # If we are not admin + if not (isinstance(self.caller, Person) and 'admin' in self.caller['roles']): + # Return only the PCUs at our site valid_pcu_ids = [] - if self.caller['site_ids']: - sites = Sites(self.api, self.caller['site_ids']) - for site in sites: - valid_pcu_ids += site['pcu_ids'] + + if isinstance(self.caller, Person): + site_ids = self.caller['site_ids'] + elif isinstance(self.caller, Node): + site_ids = [self.caller['site_id']] + + for site in Sites(self.api, site_ids): + valid_pcu_ids += site['pcu_ids'] if not valid_pcu_ids: return [] @@ -46,10 +51,23 @@ class GetPCUs(Method): if pcu_filter is None: pcu_filter = valid_pcu_ids + # Must query at least slice_id (see below) + if return_fields is not None and 'pcu_id' not in return_fields: + return_fields.append('pcu_id') + added_fields = True + else: + added_fields = False + pcus = PCUs(self.api, pcu_filter, return_fields) # Filter out PCUs that are not viewable - if 'admin' not in self.caller['roles']: - pcus = filter(lambda pcu: pcu['pcu_id'] in valid_pcu_ids, pcus) + if not (isinstance(self.caller, Person) and 'admin' in self.caller['roles']): + pcus = [pcu for pcu in pcus if pcu['pcu_id'] in valid_pcu_ids] + + # Remove pcu_id if not specified + if added_fields: + for pcu in pcus: + if 'pcu_id' in pcu: + del pcu['pcu_id'] return pcus