From 7e6cd434ceebc10d0c61d8d93a351a512ed74108 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Tue, 17 Sep 2024 19:54:03 +0300 Subject: Pass in insert_dict instead of payload in "get_wiki_entries". * gn3/api/metadata_api/wiki.py (edit_wiki) [insert_dict]: Add species, categories fields. [update_wiki_comment]: Pass in insert_dict, and get rid of redundant keys. Directly pass in sparql_auth's conf params instead of a dict. * gn3/db/rdf/wiki.py (get_next_comment_version): Get rid of redundant params. Use fields directly from insert_dict. Directly pass in sparql_auth's conf params instead of a dict. Signed-off-by: Munyoki Kilyungi --- gn3/api/metadata_api/wiki.py | 21 ++++++++---------- gn3/db/rdf/wiki.py | 52 +++++++++++++++++++++----------------------- 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/gn3/api/metadata_api/wiki.py b/gn3/api/metadata_api/wiki.py index 8aa0b37..70045aa 100644 --- a/gn3/api/metadata_api/wiki.py +++ b/gn3/api/metadata_api/wiki.py @@ -25,20 +25,21 @@ def edit_wiki(comment_id: int): # 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", [])] - created = datetime.datetime.now(datetime.timezone.utc).strftime( - "%Y-%m-%d %H:%M:%S" - ) insert_dict = { "Id": comment_id, "symbol": payload["symbol"], "PubMed_ID": " ".join(pubmed_ids), "comment": payload["comment"], "email": payload["email"], - "createtime": created, + "createtime": datetime.datetime.now(datetime.timezone.utc).strftime( + "%Y-%m-%d %H:%M:%S" + ), "user_ip": request.environ.get("HTTP_X_REAL_IP", request.remote_addr), "weburl": payload.get("web_url"), "initial": payload.get("initial"), "reason": payload["reason"], + "species": payload["species"], + "categories": payload["categories"], } insert_query = """ @@ -76,14 +77,10 @@ def edit_wiki(comment_id: int): # Editing RDF: update_wiki_comment( comment_id=comment_id, - payload=payload, - next_version=next_version, - created=created, - sparql_conf={ - "sparql_user": current_app.config["SPARQL_USER"], - "sparql_password": current_app.config["SPARQL_PASSWORD"], - "sparql_auth_uri": current_app.config["SPARQL_AUTH_URI"], - }, + insert_dict=insert_dict, + sparql_user=current_app.config["SPARQL_USER"], + sparql_password=current_app.config["SPARQL_PASSWORD"], + sparql_auth_uri=current_app.config["SPARQL_AUTH_URI"] ) return jsonify({"success": "ok"}) return jsonify(error="Error editing wiki entry, most likely due to DB error!"), 500 diff --git a/gn3/db/rdf/wiki.py b/gn3/db/rdf/wiki.py index f19670a..f2030ca 100644 --- a/gn3/db/rdf/wiki.py +++ b/gn3/db/rdf/wiki.py @@ -186,11 +186,10 @@ CONSTRUCT { def update_wiki_comment( - comment_id: int, - payload: dict, - created: str, - next_version: int, - sparql_conf: dict, + insert_dict: dict, + sparql_user: str, + sparql_password: str, + sparql_auth_uri: str, graph: str = "", ) -> tuple[str, int]: """Update a wiki comment by inserting a comment with the same @@ -220,7 +219,7 @@ looks like: } } """ - name = f"gn:wiki-{comment_id}-{next_version}" + name = f"gn:wiki-{insert_dict['Id']}-{insert_dict['versionId']}" comment_triple = Template("""$name rdf:label '''$comment'''@en ; rdf:type gnc:GNWikiEntry ; gnt:symbol "$symbol" ; @@ -228,31 +227,31 @@ dct:identifier "$comment_id"^^xsd:integer ; dct:hasVersion "$next_version"^^xsd:integer ; dct:created "$created"^^xsd:datetime . """).substitute( - comment=payload["comment"], - name=name, symbol=payload['symbol'], - comment_id=comment_id, next_version=next_version, - created=created) + comment=insert_dict["comment"], + name=name, symbol=insert_dict['symbol'], + comment_id=insert_dict["Id"], next_version=insert_dict["versionId"], + created=insert_dict["createtime"]) using = "" - if payload["email"]: - comment_triple += f"{name} foaf:mbox <{payload['email']}> .\n" - if payload["initial"]: - comment_triple += f"{name} gnt:initial \"{payload['initial']}\" .\n" - if payload["species"]: + if insert_dict["email"]: + comment_triple += f"{name} foaf:mbox <{insert_dict['email']}> .\n" + if insert_dict["initial"]: + comment_triple += f"{name} gnt:initial \"{insert_dict['initial']}\" .\n" + if insert_dict["species"]: comment_triple += f"{name} gnt:belongsToSpecies ?speciesId .\n" using = Template( """ USING $graph WHERE { ?speciesId gnt:shortName "$species" . } """).substitute( - graph=graph, species=payload["species"] + graph=graph, species=insert_dict["species"] ) - if payload["reason"]: - comment_triple += f"{name} gnt:reason \"{payload['reason']}\" .\n" - if payload["web_url"]: - comment_triple += f"{name} foaf:homepage <{payload['web_url']}> .\n" - for pmid in payload["pubmed_ids"]: + if insert_dict["reason"]: + comment_triple += f"{name} gnt:reason \"{insert_dict['reason']}\" .\n" + if insert_dict["weburl"]: + comment_triple += f"{name} foaf:homepage <{insert_dict['weburl']}> .\n" + for pmid in insert_dict["PubMed_ID"].split(): comment_triple += f"{name} dct:references pubmed:{pmid} .\n" - for category in payload["categories"]: + for category in insert_dict["categories"]: comment_triple += f'{name} gnt:belongsToCategory "{category}".\n' - res = update_rdf( + return update_rdf( query=Template( """ $prefix @@ -265,8 +264,7 @@ $comment_triple} graph=graph, comment_triple=comment_triple, using=using), - sparql_user=sparql_conf["sparql_user"], - sparql_password=sparql_conf["sparql_password"], - sparql_auth_uri=sparql_conf["sparql_auth_uri"], + sparql_user=sparql_user, + sparql_password=sparql_password, + sparql_auth_uri=sparql_auth_uri, ) - return (res, 200) -- cgit v1.2.3