Skip to content

Commit

Permalink
Merge pull request #274 from wasade/pulldown
Browse files Browse the repository at this point in the history
Migrate metadata pulldown from -admin to -private-api
  • Loading branch information
wasade committed Nov 12, 2020
2 parents 4318f3a + 269ff0d commit 26276c1
Show file tree
Hide file tree
Showing 10 changed files with 1,696 additions and 1 deletion.
23 changes: 23 additions & 0 deletions microsetta_private_api/admin/admin_impl.py
Expand Up @@ -9,6 +9,8 @@
from microsetta_private_api.repo.event_log_repo import EventLogRepo
from microsetta_private_api.repo.transaction import Transaction
from microsetta_private_api.repo.admin_repo import AdminRepo
from microsetta_private_api.repo.metadata_repo import (retrieve_metadata,
drop_private_columns)
from microsetta_private_api.tasks import send_email as celery_send_email
from microsetta_private_api.admin.email_templates import EmailMessage
from microsetta_private_api.util.redirects import build_login_redirect
Expand Down Expand Up @@ -86,6 +88,27 @@ def sample_pulldown_multiple_survey(token_info,
return jsonify(sample_pulldown), 200


def qiita_compatible_metadata(token_info, include_private, body):
validate_admin_access(token_info)

samples = body.get('sample_barcodes')
if samples is None:
return jsonify(code=404, message='No samples provided'), 404

# TODO: this call constructs transactions implicitly. It would be
# better for the transaction to be established and passed in,
# similar to how other "repo" objects are managed
df, errors = retrieve_metadata(samples)

if errors:
return jsonify(code=404, message=str(errors)), 404

if not include_private:
df = drop_private_columns(df)

return jsonify(df.to_dict(orient='index')), 200


def token_grants_admin_access(token_info):
with Transaction() as t:
account_repo = AccountRepo(t)
Expand Down
62 changes: 62 additions & 0 deletions microsetta_private_api/admin/tests/test_admin_api.py
Expand Up @@ -715,3 +715,65 @@ def test_get_daklapack_articles(self):

self.assertEqual(len(article_dicts_list), len(response_obj))
self.assertEqual(FIRST_DAKLAPACK_ARTICLE, response_obj[0])

def test_metadata_qiita_compatible_invalid(self):
data = json.dumps({'sample_barcodes': ['bad']})
response = self.client.post('/api/admin/metadata/qiita-compatible',
content_type='application/json',
data=data,
headers=MOCK_HEADERS)
self.assertEqual(404, response.status_code)
data = json.dumps({'sample_barcodes': ['bad', '000004216']})
response = self.client.post('/api/admin/metadata/qiita-compatible',
content_type='application/json',
data=data,
headers=MOCK_HEADERS)
self.assertEqual(404, response.status_code)

def test_metadata_qiita_compatible_valid(self):
data = json.dumps({'sample_barcodes': ['000004216', '000004213']})
response = self.client.post('/api/admin/metadata/qiita-compatible',
content_type='application/json',
data=data,
headers=MOCK_HEADERS)
self.assertEqual(200, response.status_code)
result = json.loads(response.data)
self.assertEqual(set(result.keys()), {'000004216', '000004213'})

def test_metadata_qiita_compatible_valid_private(self):
data = json.dumps({'sample_barcodes': ['000004216', '000004213']})
response = self.client.post('/api/admin/metadata/qiita-compatible'
'?include_private=True',
content_type='application/json',
data=data,
headers=MOCK_HEADERS)
self.assertEqual(200, response.status_code)
result = json.loads(response.data)
self.assertEqual(set(result.keys()), {'000004216', '000004213'})
obs = {c.lower() for c in result['000004216']}
self.assertIn('about_yourself_text', obs)

def test_metadata_qiita_compatible_valid_no_private(self):
data = json.dumps({'sample_barcodes': ['000004216', '000004213']})
response = self.client.post('/api/admin/metadata/qiita-compatible'
'?include_private=False',
content_type='application/json',
data=data,
headers=MOCK_HEADERS)
self.assertEqual(200, response.status_code)
result = json.loads(response.data)
self.assertEqual(set(result.keys()), {'000004216', '000004213'})
obs = {c.lower() for c in result['000004216']}
self.assertNotIn('about_yourself_text', obs)

# sanity check the the default is false
data = json.dumps({'sample_barcodes': ['000004216', '000004213']})
response = self.client.post('/api/admin/metadata/qiita-compatible',
content_type='application/json',
data=data,
headers=MOCK_HEADERS)
self.assertEqual(200, response.status_code)
result = json.loads(response.data)
self.assertEqual(set(result.keys()), {'000004216', '000004213'})
obs = {c.lower() for c in result['000004216']}
self.assertNotIn('about_yourself_text', obs)
48 changes: 47 additions & 1 deletion microsetta_private_api/api/microsetta_private_api.yaml
Expand Up @@ -1143,7 +1143,42 @@ paths:
- $ref: '#/components/parameters/sample_barcode'
responses:
'200':
description: QIIME-compatible sample metadata
description: Summarized sample metadata
content:
application/json:
schema:
type: object
'401':
$ref: '#/components/responses/401Unauthorized'
'403':
$ref: '#/components/responses/403Forbidden'
'404':
$ref: '#/components/responses/404NotFound'

'/admin/metadata/qiita-compatible':
post:
operationId: microsetta_private_api.admin.admin_impl.qiita_compatible_metadata
tags:
- Admin
summary: Retrieve Qiita compatible metadata in JSON for a set of samples
description: Retrieve Qiita compatible metadata in JSON for a set of samples
parameters:
- $ref: '#/components/parameters/include_private'
requestBody:
content:
application/json:
schema:
type: object
properties:
'sample_barcodes':
type: array
items:
# not using the defined schema for sample_barcode as it is
# readOnly
type: string
responses:
'200':
description: Summarized sample metadata
content:
application/json:
schema:
Expand Down Expand Up @@ -1431,6 +1466,12 @@ components:
description: true if computed statistics about projects should be included in projects list (takes longer)
schema:
type: boolean
include_private:
name: include_private
in: query
description: include private information in a metadata pulldown; may be true or false. Optional.
schema:
$ref: '#/components/schemas/include_private'

responses:
401Unauthorized: # Can be referenced as '#/components/responses/401Unauthorized'
Expand Down Expand Up @@ -1976,6 +2017,11 @@ components:
- code
- message

# metadata section
include_private:
type: boolean
default: false

examples:
human_adult_source:
value:
Expand Down
3 changes: 3 additions & 0 deletions microsetta_private_api/repo/metadata_repo/__init__.py
@@ -0,0 +1,3 @@
from ._repo import retrieve_metadata, drop_private_columns

__all__ = ['retrieve_metadata', 'drop_private_columns']

0 comments on commit 26276c1

Please sign in to comment.