diff --git a/microsetta_admin/server.py b/microsetta_admin/server.py index c949a29..f1530d4 100644 --- a/microsetta_admin/server.py +++ b/microsetta_admin/server.py @@ -1,3 +1,4 @@ +import csv import jwt from flask import render_template, Flask, request, session, send_file, url_for import secrets @@ -401,63 +402,79 @@ def per_sample_summary(): projects=projects, error_message=result, **build_login_variables()) - - # if we are here then the user is querying using barcodes and we - # simply need to set up the query below to perform. - sample_barcodes = [sample_barcode, ] else: - # assume POST, since there are only two methods defined in route. - # if we are here, it is because the user is querying using an uploaded - # file containing sample names. - sample_barcodes, err = upload_util.parse_request_csv_col(request, - 'file', - 'sample_name') - if err is not None: - # there was an error. abort early. - return render_template('per_sample_summary.html', - resource=None, - projects=projects, - **build_login_variables(), - search_error=[{'error': err}]) + search_field = request.form.get('search_field') + search_value = request.form.get('single_search') + uploaded_file = request.files.get('upload_list') - # perform the main query. - payload = {'sample_barcodes': sample_barcodes} - status, result = APIRequest.post('/api/admin/account_barcode_summary?stri' - 'p_sampleid=%s' % str(strip_sampleid), - json=payload) + search_values = [] - if status == 200: - if result['partial_result'] is True: - unprocessed_barcodes = result['unprocessed_barcodes'] + if uploaded_file: + file_content = io.TextIOWrapper(uploaded_file, + encoding='utf-8-sig') + csv_reader = csv.reader(file_content) + + for row in csv_reader: + search_values.extend(row) else: - unprocessed_barcodes = None - resource = pd.DataFrame(result['samples']) - order = ['sampleid', 'project', 'account-email', - 'source-type', 'site-sampled', 'sample-date', - 'sample-time', 'sample-status', 'sample-received', - 'ffq-taken', 'ffq-complete', 'vioscreen_username'] - order.extend(sorted(set(resource.columns) - set(order))) - resource = resource[order] + search_values = [search_value] if search_value else [] - if unprocessed_barcodes: - return render_template('per_sample_summary.html', - resource=resource, - projects=projects, - error_message="Too many barcodes. S" - "erver processed only" - " the first 1000.", - **build_login_variables()) + payload = {} + payload[search_field] = search_values + + # perform the main query. + status, result = APIRequest.post('/api/admin/account_barcode_summary?' + 'strip_sampleid=%s' % + str(strip_sampleid), + json=payload) + + if status == 200: + if result['partial_result'] is True: + unprocessed_barcodes = result['unprocessed_barcodes'] + else: + unprocessed_barcodes = None + resource = pd.DataFrame(result['samples']) + if not resource.empty: + order = ['sampleid', 'project', 'account-email', + 'source-type', 'site-sampled', 'sample-date', + 'sample-time', 'sample-status', 'sample-received', + 'first-scan-status', 'first-scan-timestamp', + 'latest-scan-status', 'latest-scan-timestamp', + 'sample-has-inconsistencies', 'sample-is-valid', + 'no-associated-source', 'no-collection-info', + 'no-registered-account', 'received-unknown-validity', + 'ffq-taken', 'ffq-complete', 'vioscreen_username', + 'kit-id', 'outbound-tracking', + 'inbound-tracking' + ] + order.extend(sorted(set(resource.columns) - set(order))) + resource = resource[order] + else: + return render_template('per_sample_summary.html', + resource=resource, + projects=projects, + error_message="No sample found", + **build_login_variables()) + + if unprocessed_barcodes: + return render_template('per_sample_summary.html', + resource=resource, + projects=projects, + error_message="Too many barcodes. S" + "erver processed only" + " the first 1000.", + **build_login_variables()) + else: + return render_template('per_sample_summary.html', + resource=resource, + projects=projects, + **build_login_variables()) else: return render_template('per_sample_summary.html', - resource=resource, + resource=None, projects=projects, + error_message=result, **build_login_variables()) - else: - return render_template('per_sample_summary.html', - resource=None, - projects=projects, - error_message=result, - **build_login_variables()) def _get_by_sample_barcode(sample_barcodes, strip_sampleid, projects): diff --git a/microsetta_admin/templates/per_sample_summary.html b/microsetta_admin/templates/per_sample_summary.html index 3b06ab1..be90368 100644 --- a/microsetta_admin/templates/per_sample_summary.html +++ b/microsetta_admin/templates/per_sample_summary.html @@ -11,6 +11,25 @@ block.innerHTML = ""; } }; + + function handleFormSubmit(event) { + var uploadList = document.getElementById('upload_list'); + var singleSearch = document.getElementById('single_search'); + + if (uploadList.files.length > 0 && singleSearch.value.trim() !== "") { + alert("Please provide either a text input or a file, not both."); + event.preventDefault(); + return false; + } else if (uploadList.files.length === 0 && singleSearch.value.trim() === "") { + alert("Please provide either a text input or a file."); + event.preventDefault(); + return false; + } else { + remove_error_messages(); + return true; + } + } + $(document).ready(function() { {% if resource is not none %} $('#search_results').DataTable({ @@ -27,36 +46,50 @@ {% block content %}
+ {{ error_message |e }} +
+{% endif %}- {{ error_message |e }} -
- {% endif %} - -