def subjects(self, request, *args, **kwargs): """ Returns a list of subjects associated with a protocol. """ p = self.get_object() if p.isUserAuthorized(request.user): subjects = p.getSubjects() organizations = p.organizations.all() if subjects: subs = [eHBSubjectSerializer(sub).data for sub in subjects] else: return Response([]) ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) for sub in subs: for ehb_org in ehb_orgs: if sub['organization_id'] == ehb_org.id: sub['organization_name'] = ehb_org.name else: return Response( { "detail": "You are not authorized to view subjects in this protocol" }, status=403) if subjects: return Response(subs, headers={'Access-Control-Allow-Origin': '*'}) return Response([])
def subjects(self, request, *args, **kwargs): """ Returns a list of subjects associated with a protocol. """ p = self.get_object() if p.isUserAuthorized(request.user): subjects = p.getSubjects() organizations = p.organizations.all() if subjects: subs = [eHBSubjectSerializer(sub).data for sub in subjects] else: return Response([]) ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) for sub in subs: for ehb_org in ehb_orgs: if sub['organization_id'] == ehb_org.id: sub['organization_name'] = ehb_org.name else: return Response( {"detail": "You are not authorized to view subjects in this protocol"}, status=403 ) if subjects: return Response( subs, headers={'Access-Control-Allow-Origin': '*'} ) return Response([])
def get_protocol_subjects(self, protocol, lbls): subjects = protocol.getSubjects() subject_ids = [] if subjects: subs = [eHBSubjectSerializer(sub).data for sub in subjects] for s in subs: subject_ids.append(s['id']) return subject_ids
def get(self, request, pk, *args, **kwargs): """ Returns a list of subjects associated with the protocol datasource and their external records """ try: pds = ProtocolDataSource.objects.get(pk=pk) except ObjectDoesNotExist: return Response( {'error': 'ProtocolDatasource requested not found'}, status=404) subjects = pds.protocol.getSubjects() organizations = pds.protocol.organizations.all() if pds.protocol.isUserAuthorized(request.user): if subjects: params = self.buildQueryParams( subjects, pds.data_source.ehb_service_es_id, pds.path) # TODO: Cache check res = self.er_rh.query(*params) subjects = [ eHBSubjectSerializer(subject).data for subject in subjects ] ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) for sub in subjects: sub.update({"external_records": []}) sub['organization'] = sub['organization_id'] sub.pop('organization_id') for ehb_org in ehb_orgs: if sub['organization'] == ehb_org.id: sub['organization_name'] = ehb_org.name for ex_rec in res: if ex_rec["success"]: for sub in subjects: for rec in ex_rec["external_record"]: if rec.subject_id == sub["id"]: sub["external_records"].append( json.loads( rec.json_from_identity(rec))) return Response({"subjects": subjects, "count": len(subjects)}) else: return Response({"subjects": [], "count": 0}) else: return Response( { "detail": "You are not authorized to view subjects from this protocol datasource" }, status=403)
def get(self, request, pk, *args, **kwargs): """ Returns a list of subjects associated with the protocol datasource and their external records """ try: pds = ProtocolDataSource.objects.get(pk=pk) except ObjectDoesNotExist: return Response({'error': 'ProtocolDatasource requested not found'}, status=404) subjects = pds.protocol.getSubjects() organizations = pds.protocol.organizations.all() if pds.protocol.isUserAuthorized(request.user): if subjects: params = self.buildQueryParams(subjects, pds.data_source.ehb_service_es_id, pds.path) # TODO: Cache check res = self.er_rh.query(*params) subjects = [eHBSubjectSerializer(subject).data for subject in subjects] ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) for sub in subjects: sub.update({"external_records": []}) sub['organization'] = sub['organization_id'] sub.pop('organization_id') for ehb_org in ehb_orgs: if sub['organization'] == ehb_org.id: sub['organization_name'] = ehb_org.name for ex_rec in res: if ex_rec["success"]: for sub in subjects: for rec in ex_rec["external_record"]: if rec.subject_id == sub["id"]: sub["external_records"].append(json.loads(rec.json_from_identity(rec))) return Response({ "subjects": subjects, "count": len(subjects)}) else: return Response({ "subjects": [], "count": 0}) else: return Response( {"detail": "You are not authorized to view subjects from this protocol datasource"}, status=403 )
def subjects(self, request, *args, **kwargs): """ Returns a list of subjects associated with the protocol datasource and their """ p = self.get_object() subjects = p.protocol.getSubjects() organizations = p.protocol.organizations.all() if p.protocol.isUserAuthorized(request.user): if subjects: params = self.buildQueryParams(subjects, p.data_source.ehb_service_es_id, p.path) res = ServiceClient.ext_rec_client.query(*params) subjects = [ eHBSubjectSerializer(subject).data for subject in subjects ] ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) for sub in subjects: sub.update({"external_records": []}) for ehb_org in ehb_orgs: if sub['organization_id'] == ehb_org.id: sub['organization_name'] = ehb_org.name for ex_rec in res: if ex_rec["success"]: for sub in subjects: for rec in ex_rec["external_record"]: if rec.subject_id == sub["id"]: sub["external_records"].append( eHBExternalRecordSerializer(rec).data) return Response({"subjects": subjects, "count": len(subjects)}) else: return Response({"subjects": [], "count": 0}) else: return Response( { "detail": "You are not authorized to view subjects from this protocol datasource" }, status=403)
def subjects(self, request, *args, **kwargs): """ Returns a list of subjects associated with the protocol datasource and their """ p = self.get_object() subjects = p.protocol.getSubjects() organizations = p.protocol.organizations.all() if p.protocol.isUserAuthorized(request.user): if subjects: params = self.buildQueryParams(subjects, p.data_source.ehb_service_es_id, p.path) res = ServiceClient.ext_rec_client.query(*params) subjects = [eHBSubjectSerializer(subject).data for subject in subjects] ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) for sub in subjects: sub.update({"external_records": []}) for ehb_org in ehb_orgs: if sub['organization_id'] == ehb_org.id: sub['organization_name'] = ehb_org.name for ex_rec in res: if ex_rec["success"]: for sub in subjects: for rec in ex_rec["external_record"]: if rec.subject_id == sub["id"]: sub["external_records"].append(eHBExternalRecordSerializer(rec).data) return Response({ "subjects": subjects, "count": len(subjects) }) else: return Response({ "subjects": [], "count": 0 }) else: return Response( {"detail": "You are not authorized to view subjects from this protocol datasource"}, status=403 )
def get(self, request, pk, *args, **kwargs): """ Returns a list of subjects associated with a protocol. """ try: p = Protocol.objects.get(pk=pk) except ObjectDoesNotExist: return Response({'error': 'Protocol requested not found'}, status=404) # Check cache cache_data = cache.get('protocol{0}_sub_data'.format(p.id)) if cache_data: return Response( json.loads(cache_data), headers={'Access-Control-Allow-Origin': '*'} ) if p.isUserAuthorized(request.user): subjects = p.getSubjects() organizations = p.organizations.all() if subjects: subs = [eHBSubjectSerializer(sub).data for sub in subjects] else: return Response([]) ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) # Check if the protocol has external IDs configured. If so retrieve them manageExternalIDs = False protocoldatasources = p.getProtocolDataSources() for pds in protocoldatasources: if pds.driver == 3: ExIdSource = pds manageExternalIDs = True if manageExternalIDs: try: config = json.loads(ExIdSource.driver_configuration) if 'sort_on' in list(config.keys()): # er_label_rh = ServiceClient.get_rh_for(record_type=ServiceClient.EXTERNAL_RECORD_LABEL) # lbl = er_label_rh.get(id=config['sort_on']) lbl = '' addl_id_column = lbl except: pass for sub in subs: sub['external_records'] = [] sub['external_ids'] = [] sub['organization'] = sub['organization_id'] sub['organization_id_label'] = sub['organization_id_label'] sub.pop('organization_id') for pds in protocoldatasources: sub['external_records'].extend(pds.getSubjectExternalRecords(sub)) if manageExternalIDs: # Break out external ids into a separate object for ease of use for record in sub['external_records']: if record['external_system'] == 3: sub['external_ids'].append(record) for ehb_org in ehb_orgs: if sub['organization'] == ehb_org.id: sub['organization_name'] = ehb_org.name else: return Response( {"detail": "You are not authorized to view subjects in this protocol"}, status=403 ) if subjects: return Response( subs, headers={'Access-Control-Allow-Origin': '*'} ) return Response([])
def cache_records(self, protocol_id): protocol_id = protocol_id[0] if protocol_id == 'all': protocols = Protocol.objects.all() else: protocols = Protocol.objects.filter(id=int(protocol_id)).all() er_label_rh = ServiceClient.get_rh_for( record_type=ServiceClient.EXTERNAL_RECORD_LABEL) lbls = er_label_rh.query() print('Caching {0} protocol(s)...'.format(len(protocols))) for protocol in protocols: print('Caching {}'.format(protocol)) subjects = protocol.getSubjects() organizations = protocol.organizations.all() if subjects: subs = [eHBSubjectSerializer(sub).data for sub in subjects] else: continue ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) # Check if the protocol has external IDs configured. If so retrieve them manageExternalIDs = False protocoldatasources = protocol.getProtocolDataSources() for pds in protocoldatasources: if pds.driver == 3: ExIdSource = pds manageExternalIDs = True if manageExternalIDs: try: config = json.loads(ExIdSource.driver_configuration) if 'sort_on' in list(config.keys()): # er_label_rh = ServiceClient.get_rh_for(record_type=ServiceClient.EXTERNAL_RECORD_LABEL) # lbl = er_label_rh.get(id=config['sort_on']) lbl = '' addl_id_column = lbl except: raise pass for sub in subs: sub['external_records'] = [] sub['external_ids'] = [] sub['organization'] = sub['organization_id'] sub.pop('organization_id') for pds in protocoldatasources: try: sub['external_records'].extend( self.getExternalRecords(pds, sub, lbls)) except: print("there was an error processing external records") print("subject DB id:") print(sub['id']) print("protocol data source:") print(pds) pass if manageExternalIDs: # Break out external ids into a separate object for ease of use for record in sub['external_records']: if record['external_system'] == 3: try: sub['external_ids'].append(record) except: print( "an error occured getting external records" ) print(sub['external_ids']) for ehb_org in ehb_orgs: if sub['organization'] == ehb_org.id: sub['organization_name'] = ehb_org.name cache_key = 'protocol{0}_sub_data'.format(protocol.id) cache.set(cache_key, json.dumps(subs)) cache.persist(cache_key)
def get(self, request, pk, *args, **kwargs): """ Returns a list of subjects associated with a protocol. """ try: p = Protocol.objects.get(pk=pk) except ObjectDoesNotExist: return Response({'error': 'Protocol requested not found'}, status=404) # Check cache cache_data = cache.get('protocol{0}_sub_data'.format(p.id)) if cache_data: return Response(json.loads(cache_data), headers={'Access-Control-Allow-Origin': '*'}) if p.isUserAuthorized(request.user): subjects = p.getSubjects() organizations = p.organizations.all() if subjects: subs = [eHBSubjectSerializer(sub).data for sub in subjects] else: return Response([]) ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) # Check if the protocol has external IDs configured. If so retrieve them manageExternalIDs = False protocoldatasources = p.getProtocolDataSources() for pds in protocoldatasources: if pds.driver == 3: ExIdSource = pds manageExternalIDs = True if manageExternalIDs: try: config = json.loads(ExIdSource.driver_configuration) if 'sort_on' in list(config.keys()): # er_label_rh = ServiceClient.get_rh_for(record_type=ServiceClient.EXTERNAL_RECORD_LABEL) # lbl = er_label_rh.get(id=config['sort_on']) lbl = '' addl_id_column = lbl except: pass for sub in subs: sub['external_records'] = [] sub['external_ids'] = [] sub['organization'] = sub['organization_id'] sub['organization_id_label'] = sub['organization_id_label'] sub.pop('organization_id') for pds in protocoldatasources: sub['external_records'].extend( pds.getSubjectExternalRecords(sub)) if manageExternalIDs: # Break out external ids into a separate object for ease of use for record in sub['external_records']: if record['external_system'] == 3: sub['external_ids'].append(record) for ehb_org in ehb_orgs: if sub['organization'] == ehb_org.id: sub['organization_name'] = ehb_org.name else: return Response( { "detail": "You are not authorized to view subjects in this protocol" }, status=403) if subjects: return Response(subs, headers={'Access-Control-Allow-Origin': '*'}) return Response([])
def cache_records(self, protocol_id): """Cache subject records from a given protocol locally.""" # TODO: consider passing in a list if we ever need to cache a small # number of protocols at one time. look at why only using first item in # list. protocol_id = protocol_id[0] if protocol_id == 'all': # Special "all" protocol gets all protocols. protocols = Protocol.objects.all() else: protocols = Protocol.objects.filter(id=int(protocol_id)).all() # Get external record label request handler. er_label_rh = ServiceClient.get_rh_for( record_type=ServiceClient.EXTERNAL_RECORD_LABEL) # Retrieve the actual external record labels. lbls = er_label_rh.query() # Tell user how many protocols are being cached. print('Caching {0} protocol(s)...'.format(len(protocols))) for protocol in protocols: # Tell user which protocol is being cached. print('Caching {}'.format(protocol)) # Get list of subjects and organizations in the protocol. subjects = protocol.getSubjects() organizations = protocol.organizations.all() # Serialize retrieved subjects or continue if there are none. if subjects: subs = [eHBSubjectSerializer(sub).data for sub in subjects] else: continue ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) # TODO: Explain this block, down to the `for sub in subs` loop. # Check if the protocol has external IDs configured. # If so, retrieve them. manageExternalIDs = False protocoldatasources = protocol.getProtocolDataSources() for pds in protocoldatasources: if pds.driver == 3: ExIdSource = pds manageExternalIDs = True if manageExternalIDs: try: config = json.loads(ExIdSource.driver_configuration) if 'sort_on' in list(config.keys()): # er_label_rh = ServiceClient.get_rh_for( # record_type=ServiceClient.EXTERNAL_RECORD_LABEL) # lbl = er_label_rh.get(id=config['sort_on']) lbl = '' addl_id_column = lbl # noqa except: raise pass # Transform subjects for ease of use. for sub in subs: # Initialize new fields. sub['external_records'] = [] sub['external_ids'] = [] sub['organization'] = sub['organization_id'] sub.pop('organization_id') # Add external records from all data sources. for pds in protocoldatasources: try: sub['external_records'].extend( self.getExternalRecords(pds, sub, lbls)) except: print("there was an error processing external records") print("subject DB id:") print(sub['id']) print("protocol data source:") print(pds) pass # TODO: Explain this block. if manageExternalIDs: # Break out external ids into a separate object for ease of # use. for record in sub['external_records']: if record['external_system'] == 3: try: sub['external_ids'].append(record) except: print( "an error occured getting external records" ) print(sub['external_ids']) # Add organization name to subject record for display, since # organization IDs can vary across apps. (?) for ehb_org in ehb_orgs: if sub['organization'] == ehb_org.id: sub['organization_name'] = ehb_org.name # Cache the array of subjects. cache_key = 'protocol{0}_sub_data'.format(protocol.id) cache.set(cache_key, json.dumps(subs)) cache.persist(cache_key)
def cache_records(self, protocol_id): protocol_id = protocol_id[0] if protocol_id == 'all': protocols = Protocol.objects.all() else: protocols = Protocol.objects.filter(id=int(protocol_id)).all() er_label_rh = ServiceClient.get_rh_for(record_type=ServiceClient.EXTERNAL_RECORD_LABEL) lbls = er_label_rh.query() print('Caching {0} protocol(s)...'.format(len(protocols))) for protocol in protocols: print('Caching {}'.format(protocol)) subjects = protocol.getSubjects() organizations = protocol.organizations.all() if subjects: subs = [eHBSubjectSerializer(sub).data for sub in subjects] else: continue ehb_orgs = [] # We can't rely on Ids being consistent across apps so we must # append the name here for display downstream. for o in organizations: ehb_orgs.append(o.getEhbServiceInstance()) # Check if the protocol has external IDs configured. If so retrieve them manageExternalIDs = False protocoldatasources = protocol.getProtocolDataSources() for pds in protocoldatasources: if pds.driver == 3: ExIdSource = pds manageExternalIDs = True if manageExternalIDs: try: config = json.loads(ExIdSource.driver_configuration) if 'sort_on' in list(config.keys()): # er_label_rh = ServiceClient.get_rh_for(record_type=ServiceClient.EXTERNAL_RECORD_LABEL) # lbl = er_label_rh.get(id=config['sort_on']) lbl = '' addl_id_column = lbl except: raise pass for sub in subs: sub['external_records'] = [] sub['external_ids'] = [] sub['organization'] = sub['organization_id'] sub.pop('organization_id') for pds in protocoldatasources: sub['external_records'].extend(self.getExternalRecords(pds, sub, lbls)) if manageExternalIDs: # Break out external ids into a separate object for ease of use for record in sub['external_records']: if record['external_system'] == 3: sub['external_ids'].append(record) for ehb_org in ehb_orgs: if sub['organization'] == ehb_org.id: sub['organization_name'] = ehb_org.name cache_key = 'protocol{0}_sub_data'.format(protocol.id) cache.set(cache_key, json.dumps(subs)) cache.persist(cache_key)