Skip to content

Commit

Permalink
Merge 5cef733 into 94885f3
Browse files Browse the repository at this point in the history
  • Loading branch information
AmandaBirmingham committed May 17, 2020
2 parents 94885f3 + 5cef733 commit 938662f
Show file tree
Hide file tree
Showing 155 changed files with 74,210 additions and 1,050 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ before_script:
script:
- make lint
- make test-cov
- make test-install

after_success:
- coveralls
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ lint:
test: all
py.test

test-install: all
# ensure the package is installed and the app is buildable. this test
# is a passive verification that non-py essential files are part of the
# installed entity.
cd / # go somewhere to avoid relative imports
python -c "from microsetta_private_api import server; server.build_app()"

test-cov: all
py.test --cov=microsetta_private_api

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A private microservice to support The Microsetta Initiative
## Installation
Create a new `conda` environment containing `flask` and other necessary packages:

`conda create -n microsetta-private-api flask psycopg2 natsort`
`conda create -n microsetta-private-api flask psycopg2 natsort pycryptodome`

Once the conda environment is created, activate it:

Expand Down
1 change: 1 addition & 0 deletions ci/conda_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ flake8
psycopg2
flask
natsort
pycryptodome
9 changes: 4 additions & 5 deletions microsetta_private_api/LEGACY/build_db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from microsetta_private_api.LEGACY.env_management import (
create_database, build, initialize, make_settings_table, patch_db,
populate_test_db)
create_database, initialize, patch_db, populate_test_db)

DB = 'test'
FORCE = True
Expand All @@ -12,10 +11,10 @@ def make(db, force):
create_database(force)

if db == 'production':
build(verbose=True)
# build(verbose=True)
initialize(verbose=True)
print("Making settings table")
make_settings_table()
# print("Making settings table")
# make_settings_table()
elif db == 'test':
# Test database includes initialization and settings table already
print("Populating the test database")
Expand Down
882 changes: 585 additions & 297 deletions microsetta_private_api/LEGACY/locale_data/english_gut.py

Large diffs are not rendered by default.

198 changes: 198 additions & 0 deletions microsetta_private_api/LEGACY/locale_data/spanish_gut.py

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions microsetta_private_api/LEGACY/make_test_kits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import string
import random
import datetime
import uuid
from microsetta_private_api.repo.transaction import Transaction
from microsetta_private_api.api.tests.test_integration import \
_create_mock_kit


def make_test_kits(output_filename=None, num_kits=100,
samples_lower_limit=1, samples_upper_limit=5):

if output_filename is None:
output_filename = "test_kit_ids_" + datetime.datetime.now().strftime(
"%Y%m%d%H%M%S") + ".csv"

# right now I don't want to deal with the complication of how to ensure
# that as we increment the base barcode, we don't go into >9 digits, so
# I am putting this arbitrary and low limit on it for now.
if num_kits > 1000:
raise ValueError("make_test_kits is not able to create more than 1000"
"test kits")
base_barcode = 999010000
curr_barcode = base_barcode

external_kit_ids = []

with Transaction() as t:
for curr_kit_num in range(0, num_kits):
# generate a mock internal kit id and a mock human-readable kit id
curr_kit_id = str(uuid.uuid4())
curr_external_kit_id = generate_random_kit_name()
external_kit_ids.append(curr_external_kit_id)

# select a random number of swabs to add to this kit;
# upper limit is plus 1 bc randrange stop argument is exclusive
curr_num_samples = random.randrange(samples_lower_limit,
samples_upper_limit+1)

# make barcodes/sample ids for swabs to be added to this mock kit
barcodes = []
mock_sample_ids = []
for curr_sample_index in range(0, curr_num_samples):
mock_sample_ids.append(str(uuid.uuid4()))
barcodes.append(curr_barcode)
curr_barcode += 1
# next barcode/sample to create ids for

# create the mock kit with all the specified mock barcodes/
# samples
_create_mock_kit(t, barcodes, mock_sample_ids,
curr_kit_id, curr_external_kit_id)
# next kit to create

t.commit()
# end transaction

# create a file of the kit names
with open(output_filename, 'w') as filehandle:
filehandle.write('%s\n' % "kit_id")
for curr_external_kit_id in external_kit_ids:
filehandle.write('%s\n' % curr_external_kit_id)

print("created {0} test kits with {1}-{2} samples each".format(
num_kits, samples_lower_limit, samples_upper_limit))
print("kit names in {0}".format(output_filename))


def generate_random_kit_name(name_length=8, name_base="test"):
letters_and_digits = string.ascii_letters + string.digits
rand_name = ''.join(random.choice(letters_and_digits)
for i in range(name_length))
return rand_name + name_base


make_test_kits()
Empty file.
144 changes: 144 additions & 0 deletions microsetta_private_api/admin/admin_impl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import flask
from flask import jsonify

from microsetta_private_api.repo.account_repo import AccountRepo
from microsetta_private_api.repo.transaction import Transaction
from microsetta_private_api.repo.admin_repo import AdminRepo
from werkzeug.exceptions import Unauthorized


def search_barcode(token_info, sample_barcode):
validate_admin_access(token_info)

with Transaction() as t:
admin_repo = AdminRepo(t)
diag = admin_repo.retrieve_diagnostics_by_barcode(sample_barcode)
if diag is None:
return jsonify(code=404, message="Barcode not found"), 404
return jsonify(diag), 200


def search_kit_id(token_info, kit_id):
validate_admin_access(token_info)

with Transaction() as t:
admin_repo = AdminRepo(t)
diag = admin_repo.retrieve_diagnostics_by_kit_id(kit_id)
if diag is None:
return jsonify(code=404, message="Kit ID not found"), 404
return jsonify(diag), 200


def search_email(token_info, email):
validate_admin_access(token_info)

with Transaction() as t:
admin_repo = AdminRepo(t)
diag = admin_repo.retrieve_diagnostics_by_email(email)
if diag is None:
return jsonify(code=404, message="Email not found"), 404
return jsonify(diag), 200


def scan_barcode(token_info, sample_barcode, body):
validate_admin_access(token_info)

with Transaction() as t:
admin_repo = AdminRepo(t)
admin_repo.scan_barcode(sample_barcode, body)
t.commit()

response = flask.Response()
response.status_code = 201
response.headers['Location'] = '/api/admin/search/samples/%s' % \
sample_barcode
return response


def sample_pulldown_single_survey(token_info,
sample_barcode,
survey_template_id):
validate_admin_access(token_info)

with Transaction() as t:
admin_repo = AdminRepo(t)
sample_pulldown = admin_repo.get_survey_metadata(
sample_barcode,
survey_template_id=survey_template_id)
return jsonify(sample_pulldown), 200


def sample_pulldown_multiple_survey(token_info,
sample_barcode):
validate_admin_access(token_info)

with Transaction() as t:
admin_repo = AdminRepo(t)
sample_pulldown = admin_repo.get_survey_metadata(sample_barcode)
return jsonify(sample_pulldown), 200


def project_statistics_summary(token_info):
validate_admin_access(token_info)

with Transaction() as t:
admin_repo = AdminRepo(t)
summary = admin_repo.get_project_summary_statistics()
return jsonify(summary), 200


def project_statistics_detailed(token_info, project_id):
validate_admin_access(token_info)

with Transaction() as t:
admin_repo = AdminRepo(t)
summary = admin_repo.get_project_detailed_statistics(project_id)
return jsonify(summary), 200


def validate_admin_access(token_info):
with Transaction() as t:
account_repo = AccountRepo(t)
account = account_repo.find_linked_account(token_info['iss'],
token_info['sub'])
if account is None or account.account_type != 'admin':
raise Unauthorized()


def create_project(body, token_info):
validate_admin_access(token_info)

project_name = body['project_name']
is_microsetta = body['is_microsetta']

if len(project_name) == 0:
return jsonify(code=400, message="No project name provided"), 400

with Transaction() as t:
admin_repo = AdminRepo(t)
admin_repo.create_project(project_name, is_microsetta)
t.commit()

return {}, 201


def create_kits(body, token_info):
validate_admin_access(token_info)

number_of_kits = body['number_of_kits']
number_of_samples = body['number_of_samples']
kit_prefix = body.get('kit_id_prefix', None)
projects = body['projects']

with Transaction() as t:
admin_repo = AdminRepo(t)

try:
kits = admin_repo.create_kits(number_of_kits, number_of_samples,
kit_prefix, projects)
except KeyError:
return jsonify(code=422, message="Unable to create kits"), 422
else:
t.commit()

return jsonify(kits), 201
Empty file.

0 comments on commit 938662f

Please sign in to comment.