diff --git a/backend/restapi/__init__.py b/backend/restapi/__init__.py index e5b9abd..837972e 100644 --- a/backend/restapi/__init__.py +++ b/backend/restapi/__init__.py @@ -228,7 +228,33 @@ def get_columns(): except OSError: pass - return jsonify(list(df.columns.values)) + return(jsonify(list(df.columns.values))) + + @app.route('/api/instrument/columnCheck', methods=["POST"]) + def column_check(): + file_to_import = request.files.get("file", None) + + if not os.path.exists(INSTRUMENTS + "/tmp"): + os.makedirs(INSTRUMENTS + "/tmp") + + file_to_import.save(INSTRUMENTS + "/tmp/" + file_to_import.filename) + + if file_to_import.filename.split(".")[-1] == "xlsx": + df = pd.read_excel(INSTRUMENTS + "/tmp/" + file_to_import.filename) + if file_to_import.filename.split(".")[-1] == "csv": + df = pd.read_csv(INSTRUMENTS + "/tmp/" + file_to_import.filename) + + required_opal_columns = ["index", "table", "name", "valueType", "unit", "label:en", "label:la", "label:de", + "alias"] + actual_columns = df.columns.values + missing_columns = [i for i in required_opal_columns if i not in actual_columns] + + try: + os.remove(INSTRUMENTS + "/tmp/" + file_to_import.filename) + except OSError: + pass + + return(jsonify(missing_columns)) @app.route('/api/instrument', methods=["POST"]) def import_instrument(): diff --git a/frontend/src/components/import/ImportExcel.tsx b/frontend/src/components/import/ImportExcel.tsx index b9e203a..a3ca97e 100644 --- a/frontend/src/components/import/ImportExcel.tsx +++ b/frontend/src/components/import/ImportExcel.tsx @@ -44,12 +44,45 @@ export default (props: { }).then((res) => res.json()); }); + const { + data: columnCheck, + mutate: checkColumns, + isSuccess: isSuccessColumnCheck, + isLoading: isLoadingColumnCheck, + isError: isErrorColumnCheck + } = useMutation(() => { + const file = new FormData(); + file.append("file", props.files); + return fetch(`/api/instrument/columnCheck`, { + method: "POST", + body: file, + }).then((res) => res.json()); + }); + const pick = () => { if (props.hasFile) { get_columns(); } }; + const check_columns = () => { + if (props.hasFile) { + checkColumns(); + } + }; + + const displayColumnCheck = (item: any) => { + return ( + item ? + item.length > 1 ? + item.map((element: string, i: any) => +
{element}
) + : item + : "-" + ) + } + console.log(columnCheck) + return ( <> { pick(); + check_columns() + console.log(columnCheck) }} isDisabled={!props.hasFile} > @@ -104,6 +139,14 @@ export default (props: { {isError && The file could not be validated. Try again, use another format or report via the blue feedback button.} + {isLoadingColumnCheck && } + + {columnCheck && columnCheck.length == 0 && + OPAL format validated + } + {columnCheck && columnCheck.length != 0 && + The following columns are required for the OPAL + format: {displayColumnCheck(columnCheck)}}