diff --git a/microsetta_private_api/admin/admin_impl.py b/microsetta_private_api/admin/admin_impl.py index ff4d2d80c..fb2904b99 100644 --- a/microsetta_private_api/admin/admin_impl.py +++ b/microsetta_private_api/admin/admin_impl.py @@ -491,6 +491,7 @@ def query_project_barcode_stats(body, token_info, strip_sampleid): def query_barcode_stats(body, token_info, strip_sampleid): validate_admin_access(token_info) if 'sample_barcodes' in body: + project_id = None barcodes = body["sample_barcodes"] elif 'project_id' in body: project_id = body["project_id"] @@ -502,7 +503,7 @@ def query_barcode_stats(body, token_info, strip_sampleid): unprocessed_barcodes = barcodes[1000:] barcodes = barcodes[0:1000] - results = {'samples': per_sample(None, barcodes, strip_sampleid)} + results = {'samples': per_sample(project_id, barcodes, strip_sampleid)} if unprocessed_barcodes: results['partial_result'] = True diff --git a/microsetta_private_api/admin/sample_summary.py b/microsetta_private_api/admin/sample_summary.py index 207cca79e..a64f00e2a 100644 --- a/microsetta_private_api/admin/sample_summary.py +++ b/microsetta_private_api/admin/sample_summary.py @@ -23,13 +23,12 @@ def per_sample(project, barcodes, strip_sampleid): template_repo = SurveyTemplateRepo(t) vs_repo = VioscreenSessionRepo(t) - if project is not None: - project_barcodes = admin_repo.get_project_barcodes(project) - else: - project = 'Unspecified' - + # all associated projects returned for each barcode, + # so no universal project needed if barcodes is None: - barcodes = project_barcodes + if project is None: + return summaries + barcodes = admin_repo.get_project_barcodes(project) for barcode in barcodes: diag = admin_repo.retrieve_diagnostics_by_barcode(barcode) @@ -44,6 +43,11 @@ def per_sample(project, barcodes, strip_sampleid): source_type = None if source is None else source.source_type vio_id = None + # find all projects for barcode + projects_info = diag['projects_info'] + all_projects = [proj_obj['project'] for proj_obj in projects_info] + barcode_project = '; '.join(sorted(all_projects)) + if source is not None and source_type == Source.SOURCE_TYPE_HUMAN: vio_id = template_repo.get_vioscreen_id_if_exists(account.id, @@ -82,7 +86,7 @@ def per_sample(project, barcodes, strip_sampleid): summary = { "sampleid": None if strip_sampleid else barcode, - "project": project, + "project": barcode_project, "source-type": source_type, "site-sampled": sample_site, "sample-date": sample_date, diff --git a/microsetta_private_api/admin/tests/test_admin_api.py b/microsetta_private_api/admin/tests/test_admin_api.py index a6bd5d54a..c7d8df302 100644 --- a/microsetta_private_api/admin/tests/test_admin_api.py +++ b/microsetta_private_api/admin/tests/test_admin_api.py @@ -1252,6 +1252,57 @@ def test_query_barcode_stats_project_barcodes_with_strip(self): self.assertEqual([v['sample-status'] for v in response_obj], exp_status) + def test_query_barcode_stats_multiple_projects(self): + barcodes = ['000005059', '000005078', '000005103'] + input_json = json.dumps({'sample_barcodes': barcodes}) + + response = self.client.post( + "api/admin/account_barcode_summary?strip_sampleid=False", + content_type='application/json', + data=input_json, + headers=MOCK_HEADERS + ) + # an empty string project should be unknown + self.assertEqual(200, response.status_code) + + response_obj = json.loads(response.data) + self.assertIn('samples', response_obj) + response_obj = response_obj['samples'] + self.assertEqual(len(response_obj), 3) + + self.assertEqual([v['sampleid'] for v in response_obj], barcodes) + response_projects = [v['project'].split('; ') for v in response_obj] + self.assertEqual(len(response_projects[0]), 2) + self.assertEqual(len(response_projects[1]), 3) + self.assertEqual(len(response_projects[2]), 3) + self.assertTrue(all( + projs[0] == 'American Gut Project' for projs in response_projects + )) + # content: should have exactly the same projects + self.assertEqual(response_projects[1], response_projects[2]) + + def test_query_barcode_stats_by_project(self): + input_json = json.dumps({'project_id': 19}) # expect 5 barcodes + exp_barcodes = [ + '000035369', '000035370', '000035371', + '000035372', '000035373' + ] + + response = self.client.post( + "api/admin/account_barcode_summary?strip_sampleid=False", + content_type='application/json', + data=input_json, + headers=MOCK_HEADERS + ) + # an empty string project should be unknown + self.assertEqual(200, response.status_code) + + response_obj = json.loads(response.data) + self.assertIn('samples', response_obj) + response_obj = response_obj['samples'] + self.assertEqual(len(response_obj), 5) + self.assertEqual([v['sampleid'] for v in response_obj], exp_barcodes) + def test_send_email(self): def mock_func(*args, **kwargs): pass