diff options
author | John Nduli | 2024-08-22 13:36:28 +0300 |
---|---|---|
committer | BonfaceKilz | 2024-08-26 13:06:47 +0300 |
commit | 5102d2d9a733992535fa5effe9bc0ddc47923b32 (patch) | |
tree | 001e09e53f8705d8f01a1bdee324ccb92fe39d62 /gn3 | |
parent | 8964a3c711ae8fb0d87923dce243f47a37e7d294 (diff) | |
download | genenetwork3-5102d2d9a733992535fa5effe9bc0ddc47923b32.tar.gz |
feat: add support for categories in edit query and clearer errors
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/api/wiki.py | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/gn3/api/wiki.py b/gn3/api/wiki.py index 133d7c8..f96a3dc 100644 --- a/gn3/api/wiki.py +++ b/gn3/api/wiki.py @@ -7,13 +7,14 @@ wiki = Blueprint("wiki", __name__) @wiki.route("/comments/<int:comment_id>/edit", methods=["POST"]) def edit_wiki(comment_id: int): + # FIXME: attempt to check and fix for types here with relevant errors payload = request.json + pubmed_ids = [str(x) for x in payload.get("pubmed_ids", [])] + insert_dict = { "Id": comment_id, - "versionId": payload["version_id"], "symbol": payload["symbol"], - "PubMed_ID": payload.get("pubmed_id"), - "SpeciesID": payload["species_id"], + "PubMed_ID": " ".join(pubmed_ids), "comment": payload["comment"], # does this need to be part of the payload or can we get this from session information # e.g. https://github.com/genenetwork/genenetwork2/blob/0998033d0a7ea26ed96b00a360a334bae6de8c55/gn2/wqflask/oauth2/session.py#L22-L23 @@ -28,15 +29,6 @@ def edit_wiki(comment_id: int): "reason": payload["reason"], } - if not isinstance(insert_dict["versionId"], int): - return jsonify( - error=f"Error editting wiki entry, expected versionId as int but got {insert_dict['versionId']}!" - ), 500 - if not isinstance(insert_dict["SpeciesID"], int): - return jsonify( - error=f"Error editting wiki entry, expected versionId as int but got {insert_dict['SpeciesID']}!" - ), 500 - insert_query = """ INSERT INTO GeneRIF (Id, versionId, symbol, PubMed_ID, SpeciesID, comment, email, createtime, user_ip, weburl, initial, reason) @@ -44,8 +36,37 @@ def edit_wiki(comment_id: int): """ with db_utils.database_connection(current_app.config["SQL_URI"]) as conn: cursor = conn.cursor() - current_app.logger.error(f"Inserting: {insert_dict}") - current_app.logger.error(f"wiht query: {insert_query}") + categories = get_categories(cursor) + category_ids = [] + for category in payload["categories"]: + cat_id = categories.get(category.strip()) + if cat_id is None: + return jsonify(error=f"Error editting wiki entry, category with Name={category} not found"), 500 + category_ids.append(cat_id) + cursor.execute("SELECT SpeciesID from Species WHERE Name = %s", (payload["species"],)) + species_ids = cursor.fetchall() + if len(species_ids) != 1: + return jsonify(error=f"Error editting wiki entry, expected 1 species with Name={payload['species']} but found {len(species_ids)}!"), 500 + insert_dict["SpeciesID"] = species_ids[0][0] + + cursor.execute("SELECT MAX(versionId) as version_id from GeneRIF WHERE Id = %s", (comment_id,)) + latest_version = cursor.fetchone()[0] + if latest_version is None: + return jsonify(error=f"Error editting wiki entry, No comments found with comment_id={comment_id}"), 500 + insert_dict["versionId"] = latest_version + 1 + current_app.logger.debug(f"Running query: {insert_query}") cursor.execute(insert_query, insert_dict) + + category_addition_query = "INSERT INTO GeneRIFXRef (GeneRIFId, versionId, GeneCategoryId) VALUES (%s, %s, %s)" + + for cat_id in category_ids: + current_app.logger.debug(f"Running query: {category_addition_query}") + cursor.execute(category_addition_query, (comment_id, insert_dict["versionId"], cat_id)) return jsonify({"success": "ok"}) return jsonify(error="Error editting wiki entry, most likely due to DB error!"), 500 + + +def get_categories(cursor) -> dict: + cursor.execute("SELECT Name, Id from GeneCategory") + raw_categories = cursor.fetchall() + return dict(raw_categories) |