Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DO NOT MERGE (CSV upload API) #17

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions script_facade/api/fhir.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import csv
from flask import Blueprint, request, current_app
import io
import timeit

from script_facade.client.client_106 import rx_history_query, patient_lookup_query
Expand Down Expand Up @@ -57,3 +59,38 @@ def patient_search(fhir_version):

patient_bundle = patient_lookup_query(patient_fname, patient_lname, patient_dob)
return patient_bundle


@blueprint.route('/upload/patient/csv', methods=['POST'])
def upload_patient_csv():
"""Given a CSV file (via request.files) upload contents"""

def decode_file(binary_data):
"""Convert binary file data to file like string buffer

Flask reads in multipart files as binary, which the csv lib can't
handle.

:returns: StringIO buffer of utf-8 decoded strings for file like use
"""
buffer = io.StringIO()
last_pos = binary_data.tell()
while True:
line = binary_data.readline()
pos = binary_data.tell()
if pos == last_pos:
# Position stops progressing at EOF
break
last_pos = pos
buffer.write(line.decode('utf-8'))

buffer.seek(0)
return buffer

contents = request.files['file']
csv.register_dialect('generic', skipinitialspace=True)
reader = csv.DictReader(decode_file(contents), dialect='generic')
for row in reader:
if 'provider_last_name' in row:
# Upsert provider
pass
10 changes: 10 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pytest
from conftest import load_json

Expand All @@ -20,3 +21,12 @@ def test_medication_order(client, mocker, med_order_bundle):
}
)
assert response.status_code == 200


def test_upload_patient(client, datadir):
file = open(os.path.join(datadir, 'patients.csv'), 'rb')
data = {'filename': 'patients.csv', 'file': file}
client.post(
'/upload/patient/csv',
content_type='multipart/form-data',
data=data)
2 changes: 2 additions & 0 deletions tests/test_api/patients.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
first_name, last_name, birthdate
John, Hancock, 1737-01-23