aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Nduli2024-08-26 12:40:15 +0300
committerBonfaceKilz2024-08-26 13:06:47 +0300
commit7b2a1794c1b7e7c589b1abe6d307b2a7e4cd2c42 (patch)
tree5dcff41e762419771a4448955281f3f6cce60781
parent3156f84055502035620a0279e62eb383a5f7d665 (diff)
downloadgenenetwork3-7b2a1794c1b7e7c589b1abe6d307b2a7e4cd2c42.tar.gz
refactor: split out utlities into their modules and change api to use metadata
-rw-r--r--gn3/api/metadata.py3
-rw-r--r--gn3/api/metadata_api/wiki.py (renamed from gn3/api/wiki.py)44
-rw-r--r--gn3/db/wiki.py38
3 files changed, 47 insertions, 38 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py
index 9d82dba..53daf67 100644
--- a/gn3/api/metadata.py
+++ b/gn3/api/metadata.py
@@ -18,6 +18,8 @@ from gn3.db.rdf import RDF_PREFIXES
from gn3.db.rdf import (query_frame_and_compact,
query_and_compact)
+from gn3.api.metadata_api import wiki
+
BASE_CONTEXT = {
"data": "@graph",
@@ -142,6 +144,7 @@ PHENOTYPE_CONTEXT = BASE_CONTEXT | PUBLICATION_CONTEXT | {
}
metadata = Blueprint("metadata", __name__)
+metadata.register_blueprint(wiki.wiki_blueprint)
@metadata.route("/datasets/<name>", methods=["GET"])
diff --git a/gn3/api/wiki.py b/gn3/api/metadata_api/wiki.py
index 2b8369f..7e22c94 100644
--- a/gn3/api/wiki.py
+++ b/gn3/api/metadata_api/wiki.py
@@ -2,16 +2,17 @@ import datetime
from typing import Any, Dict, List
from flask import Blueprint, request, jsonify, current_app
from gn3 import db_utils
+from gn3.db import wiki
-wiki = Blueprint("wiki", __name__)
+wiki_blueprint = Blueprint("wiki", __name__, url_prefix="wiki")
class MissingDBDataException(Exception):
pass
-@wiki.route("/<int:comment_id>/edit", methods=["POST"])
+@wiki_blueprint.route("/<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: Dict[str, Any] = request.json
@@ -40,9 +41,9 @@ def edit_wiki(comment_id: int):
with db_utils.database_connection(current_app.config["SQL_URI"]) as conn:
cursor = conn.cursor()
try:
- category_ids = get_categories_ids(cursor, payload["categories"])
- species_id = get_species_id(cursor, payload["species"])
- next_version = get_next_comment_version(cursor, comment_id)
+ 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 MissingDBDataException as missing_exc:
return jsonify(error=f"Error editting wiki entry, {missing_exc}"), 500
insert_dict["SpeciesID"] = species_id
@@ -58,36 +59,3 @@ def edit_wiki(comment_id: int):
)
return jsonify({"success": "ok"})
return jsonify(error="Error editting wiki entry, most likely due to DB error!"), 500
-
-
-def get_species_id(cursor, species_name: str) -> int:
- cursor.execute("SELECT SpeciesID from Species WHERE Name = %s", (species_name,))
- species_ids = cursor.fetchall()
- if len(species_ids) != 1:
- raise MissingDBDataException(
- f"expected 1 species with Name={species_name} but found {len(species_ids)}!"
- )
- return species_ids[0][0]
-
-
-def get_next_comment_version(cursor, comment_id: int) -> int:
- 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:
- raise MissingDBDataException(f"No comment found with comment_id={comment_id}")
- return latest_version + 1
-
-
-def get_categories_ids(cursor, categories: List[str]) -> List[int]:
- cursor.execute("SELECT Name, Id from GeneCategory")
- raw_categories = cursor.fetchall()
- dict_cats = dict(raw_categories)
- category_ids = []
- for category in set(categories):
- cat_id = dict_cats.get(category.strip())
- if cat_id is None:
- raise MissingDBDataException(f"Category with Name={category} not found")
- category_ids.append(cat_id)
- return category_ids
diff --git a/gn3/db/wiki.py b/gn3/db/wiki.py
new file mode 100644
index 0000000..a56f26c
--- /dev/null
+++ b/gn3/db/wiki.py
@@ -0,0 +1,38 @@
+from typing import List
+
+
+class MissingDBDataException(Exception):
+ pass
+
+
+def get_species_id(cursor, species_name: str) -> int:
+ cursor.execute("SELECT SpeciesID from Species WHERE Name = %s", (species_name,))
+ species_ids = cursor.fetchall()
+ if len(species_ids) != 1:
+ raise MissingDBDataException(
+ f"expected 1 species with Name={species_name} but found {len(species_ids)}!"
+ )
+ return species_ids[0][0]
+
+
+def get_next_comment_version(cursor, comment_id: int) -> int:
+ 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:
+ raise MissingDBDataException(f"No comment found with comment_id={comment_id}")
+ return latest_version + 1
+
+
+def get_categories_ids(cursor, categories: List[str]) -> List[int]:
+ cursor.execute("SELECT Name, Id from GeneCategory")
+ raw_categories = cursor.fetchall()
+ dict_cats = dict(raw_categories)
+ category_ids = []
+ for category in set(categories):
+ cat_id = dict_cats.get(category.strip())
+ if cat_id is None:
+ raise MissingDBDataException(f"Category with Name={category} not found")
+ category_ids.append(cat_id)
+ return category_ids