From 93a870ad484f641e96c04675e5f2aaad4a12c2d5 Mon Sep 17 00:00:00 2001 From: John Nduli Date: Tue, 27 Aug 2024 10:21:35 +0300 Subject: feat: add post actions for editting wiki content --- gn2/wqflask/views.py | 61 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 23 deletions(-) (limited to 'gn2/wqflask/views.py') diff --git a/gn2/wqflask/views.py b/gn2/wqflask/views.py index 18f749c9..b29efd94 100644 --- a/gn2/wqflask/views.py +++ b/gn2/wqflask/views.py @@ -1502,31 +1502,46 @@ def approve_reject_diff() -> Response: diff_id=form["diff_id"])) -@app.route("/wiki//edit") +@app.route("/wiki//edit", methods=["GET", "POST"]) def edit_wiki(comment_id: int): """fetch generif metadata from gn3 and display it""" # FIXME: better error handling - last_wiki_content = ( - monad_requests.get(urljoin(GN3_LOCAL_URL, f"/api/metadata/wiki/{comment_id}")) - .then(lambda res: res) - .either(lambda _: [], lambda x: x.json()) - ) - species_dict = ( - monad_requests.get(urljoin(GN3_LOCAL_URL, "/api/metadata/wiki/species")) - .then(lambda res: res) - .either(lambda _: [], lambda x: x.json()) - ) - categories = ( - monad_requests.get(urljoin(GN3_LOCAL_URL, "/api/metadata/wiki/categories")) - .then(lambda resp: resp) - .either(lambda _: [], lambda x: list(x.json().keys())) - ) + if request.method == "GET": + last_wiki_content = ( + monad_requests.get(urljoin(GN3_LOCAL_URL, f"/api/metadata/wiki/{comment_id}")) + .either(lambda err: err.raise_for_status(), lambda x: x.json()) + ) + + species_dict = ( + monad_requests.get(urljoin(GN3_LOCAL_URL, "/api/metadata/wiki/species")) + .either(lambda err: err.raise_for_status(), lambda x: x.json()) + ) + categories = ( + monad_requests.get(urljoin(GN3_LOCAL_URL, "/api/metadata/wiki/categories")) + .either(lambda err: err.raise_for_status(), lambda x: list(x.json().keys())) + ) - grouped_categories = [categories[i : i + 3] for i in range(0, len(categories), 3)] + grouped_categories = [categories[i : i + 3] for i in range(0, len(categories), 3)] - return render_template( - "wiki/edit_wiki.html", - content=last_wiki_content, - species_dict=species_dict, - grouped_categories=grouped_categories, - ) + return render_template( + "wiki/edit_wiki.html", + content=last_wiki_content, + species_dict=species_dict, + grouped_categories=grouped_categories, + ) + if request.method == "POST": + post_data = request.form + payload = { + "symbol": post_data["symbol"], + "pubmed_ids": [x.strip() for x in post_data["pubmed_ids"].split()], + "species": post_data["species"], + "comment": post_data["comment"], + "email": post_data["email"], + "web_url": post_data["web_url"], + "initial": post_data["initial"], + "categories": post_data.getlist("genecategory"), + "reason": post_data["reason"], + } + post_res = monad_requests.post(urljoin(GN3_LOCAL_URL, f"api/metadata/wiki/{comment_id}/edit"), json=payload).either(lambda err: err.raise_for_status(), lambda success: success.json()) + flash(f"Success: {post_res}", "alert-success") + return redirect(url_for("edit_wiki", comment_id=comment_id)) -- cgit v1.2.3