about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMunyoki Kilyungi2025-01-20 19:58:44 +0300
committerBonfaceKilz2025-01-21 08:46:02 +0300
commita5095460a2ea2c2d9ec9e26930acd79edbea3679 (patch)
treee966c94278fc5e0f9f07a75f208000708ae4f1e1
parent5d2aebb8c1e3af7751a7b23d3467c63006395c58 (diff)
downloadgenenetwork3-a5095460a2ea2c2d9ec9e26930acd79edbea3679.tar.gz
Enable new inserts of new wiki entries.
* gn3/api/metadata_api/wiki.py: Import Optional
(edit_wiki): Add "/edit" route.  Add new comment id.
* gn3/db/wiki.py (get_next_comment_id): New function for fetching the
getting the next comment_id.

Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r--gn3/api/metadata_api/wiki.py10
-rw-r--r--gn3/db/wiki.py8
2 files changed, 15 insertions, 3 deletions
diff --git a/gn3/api/metadata_api/wiki.py b/gn3/api/metadata_api/wiki.py
index ba01b19..49b7802 100644
--- a/gn3/api/metadata_api/wiki.py
+++ b/gn3/api/metadata_api/wiki.py
@@ -3,6 +3,7 @@
 import datetime
 from typing import Any, Dict
 
+from typing import Optional
 from flask import Blueprint, request, jsonify, current_app, make_response
 
 from gn3 import db_utils
@@ -20,10 +21,11 @@ wiki_blueprint = Blueprint("wiki", __name__, url_prefix="wiki")
 rif_blueprint = Blueprint("rif", __name__, url_prefix="rif")
 
 
+@wiki_blueprint.route("/edit", methods=["POST"], defaults={'comment_id': None})
 @wiki_blueprint.route("/<int:comment_id>/edit", methods=["POST"])
 @require_token
-def edit_wiki(comment_id: int, **kwargs):# pylint: disable=[unused-argument]
-    """Edit wiki comment. This is achieved by adding another entry with a new VersionId"""
+def edit_wiki(comment_id: Optional[int], **kwargs):  # pylint: disable=[unused-argument]
+    """Edit/Insert wiki comment. This is achieved by adding another entry with a new VersionId"""
     # FIXME: attempt to check and fix for types here with relevant errors
     payload: Dict[str, Any] = request.json  # type: ignore
     pubmed_ids = [str(x) for x in payload.get("pubmed_ids", [])]
@@ -51,12 +53,14 @@ def edit_wiki(comment_id: int, **kwargs):# pylint: disable=[unused-argument]
     """
     with db_utils.database_connection(current_app.config["SQL_URI"]) as conn:
         cursor = conn.cursor()
+        if not comment_id:
+            comment_id = wiki.get_next_comment_id(cursor)
+            insert_dict["Id"] = comment_id
         next_version = 0
         try:
             category_ids = wiki.get_categories_ids(
                 cursor, payload["categories"])
             species_id = wiki.get_species_id(cursor, payload["species"])
-            next_version = wiki.get_next_comment_version(cursor, comment_id)
         except wiki.MissingDBDataException as missing_exc:
             return jsonify(error=f"Error editing wiki entry, {missing_exc}"), 500
         insert_dict["SpeciesID"] = species_id
diff --git a/gn3/db/wiki.py b/gn3/db/wiki.py
index 0f46855..2e12230 100644
--- a/gn3/db/wiki.py
+++ b/gn3/db/wiki.py
@@ -70,6 +70,14 @@ def get_next_comment_version(cursor, comment_id: int) -> int:
     return latest_version + 1
 
 
+def get_next_comment_id(cursor) -> int:
+    """Get the next GeneRIF.Id"""
+    cursor.execute(
+        "SELECT MAX(Id) from GeneRIF"
+    )
+    return cursor.fetchone()[0] + 1
+
+
 def get_categories_ids(cursor, categories: List[str]) -> List[int]:
     """Get the categories_ids from a list of category strings"""
     dict_cats = get_categories(cursor)