Skip to content

Commit

Permalink
feat: validate OPAl format during import
Browse files Browse the repository at this point in the history
  • Loading branch information
jusa3 committed Sep 11, 2023
1 parent 5508096 commit 9b98e02
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
28 changes: 27 additions & 1 deletion backend/restapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
43 changes: 43 additions & 0 deletions frontend/src/components/import/ImportExcel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
<dd key={i}>{element}</dd>)
: item
: "-"
)
}
console.log(columnCheck)

return (
<>
<EuiDescribedFormGroup
Expand Down Expand Up @@ -94,6 +127,8 @@ export default (props: {
<EuiButton
onClick={() => {
pick();
check_columns()
console.log(columnCheck)
}}
isDisabled={!props.hasFile}
>
Expand All @@ -104,6 +139,14 @@ export default (props: {
{isError &&
<EuiTextColor color="danger">The file could not be validated. Try again, use another format
or report via the blue feedback button.</EuiTextColor>}
{isLoadingColumnCheck && <EuiLoadingSpinner/>}

{columnCheck && columnCheck.length == 0 &&
<EuiTextColor color="success">OPAL format validated</EuiTextColor>
}
{columnCheck && columnCheck.length != 0 &&
<EuiTextColor color="danger">The following columns are required for the OPAL
format: {displayColumnCheck(columnCheck)}</EuiTextColor>}
</>
</EuiFormRow>
</EuiDescribedFormGroup>
Expand Down

0 comments on commit 9b98e02

Please sign in to comment.