diff options
author | Munyoki Kilyungi | 2024-02-21 15:43:55 +0300 |
---|---|---|
committer | BonfaceKilz | 2024-03-26 10:01:13 +0300 |
commit | 0075b503ed08514b7a2b494f9c8b61bed49c8f40 (patch) | |
tree | 07a94c25d2bc9a2800291409fe1cf8c03be09924 /gn2 | |
parent | 2e8902835fede7b393ba5a659779bc0ce882d300 (diff) | |
download | genenetwork2-0075b503ed08514b7a2b494f9c8b61bed49c8f40.tar.gz |
Add function for saving metadata in git.
* gn2/wqflask/edit.py (save_dataset_metadata): New function.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn2')
-rw-r--r-- | gn2/wqflask/edit.py | 68 |
1 files changed, 62 insertions, 6 deletions
diff --git a/gn2/wqflask/edit.py b/gn2/wqflask/edit.py index 8d176608..61b1ee61 100644 --- a/gn2/wqflask/edit.py +++ b/gn2/wqflask/edit.py @@ -1,8 +1,12 @@ import requests +import subprocess from pathlib import Path +from pymonad.either import Either, Left + from flask import (Blueprint, + flash, redirect, render_template, request) @@ -14,6 +18,35 @@ from gn2.utility.tools import GN3_LOCAL_URL metadata = Blueprint("metadata", __name__) +def save_dataset_metadata( + git_dir: str, output: str, + content: str, msg: str +) -> Either: + """Save dataset metadata to git""" + def __run_cmd(cmd, status_code): + __result = subprocess.run( + cmd.split(" "), shell=True, + capture_output=True + ) + if __result.stderr or status_code != 0: + return Left({ + "command": cmd, + "error": __result.stderr.decode() + }) + return 0 + + with Path(output) as _f: + _f.write(content) + + return ( + Either.insert(0) + .then(__run_cmd(f"git -C {git_dir} pull")) + .then(__run_cmd(f"git -C {git_dir} add .")) + .then(__run_cmd(f"git -C {git_dir} commit --all --message {msg}")) + .then(__run_cmd(f"git -C {git_dir} push origin master --dry-run")) + ) + + @metadata.route("/edit") @login_required(pagename="Dataset Metadata Editing") def metadata_edit(): @@ -39,10 +72,33 @@ def metadata_edit(): @metadata.route("/save", methods=["POST"]) @login_required(pagename="Dataset Metadata Editing") def save(): - __content = request.form.get("editor") - __summary = request.form.get("summary") - __type = request.form.get("type") - match __type: + from gn2.utility.tools import get_setting + __gn_docs = Path( + get_setting("DATA_DIR"), + "gn-docs" + ) + __output = Path( + __gn_docs, + "general/datasets/", + request.form.get("id").split("/")[-1] + ) + match request.form.get("type"): case "dcat:Dataset": - # XXX: TODO: Method for saving data - return redirect(f"/datasets/{request.form.get('label')}") + save_dataset_metadata( + git_dir=__gn_docs, + output=__output, + content=request.form.get("editor"), + msg=request.form.get("edit-summary") + ).either( + lambda error: flash( + f"{error=}", + "error" + ), + lambda x: flash( + "Successfully updated data.", + "success" + ) + ) + return redirect( + Path("/datasets", request.form.get('label')) + ) |