aboutsummaryrefslogtreecommitdiff
path: root/gn3/db
diff options
context:
space:
mode:
authorMunyoki Kilyungi2024-09-11 13:16:22 +0300
committerJohn Nduli Kilyungi2024-09-18 08:32:34 +0300
commit471de9ace138eba6eeeceff018f729491e650212 (patch)
tree338990d562182810d55254540f7c76d69b1945a1 /gn3/db
parent47859daf988ff05173801f46b997bae759d1c48d (diff)
downloadgenenetwork3-471de9ace138eba6eeeceff018f729491e650212.tar.gz
Fetch the next comment version in RDF.
* gn3/db/rdf/wiki.py (get_next_comment_version): New function. Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn3/db')
-rw-r--r--gn3/db/rdf/wiki.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/gn3/db/rdf/wiki.py b/gn3/db/rdf/wiki.py
index feda5bd..2864b2b 100644
--- a/gn3/db/rdf/wiki.py
+++ b/gn3/db/rdf/wiki.py
@@ -10,7 +10,8 @@ NOTE: In the CONSTRUCT queries below, we manually sort the arrays from
<https://www.w3.org/TR/rdf-sparql-query/#modOrderBy>
"""
from string import Template
-from gn3.db.rdf import BASE_CONTEXT, RDF_PREFIXES, query_frame_and_compact
+from gn3.db.rdf import BASE_CONTEXT, RDF_PREFIXES, query_frame_and_compact, sparql_query
+from gn3.db.wiki import MissingDBDataException
WIKI_CONTEXT = BASE_CONTEXT | {
@@ -180,3 +181,25 @@ CONSTRUCT {
# See note above in the doc-string
results["data"] = sorted(data, key=lambda d: d["version"], reverse=True)
return results
+
+
+def get_next_comment_version(
+ comment_id: int, sparql_uri: str, graph: str = "<http://genenetwork.org>"
+) -> int:
+ "Find the next version to add"
+ query = Template(
+ """
+$prefix
+
+SELECT MAX(?version) as ?max_version FROM $graph WHERE {
+ ?comment rdf:type gnc:GNWikiEntry ;
+ dct:identifier "$comment_id"^^xsd:integer ;
+ dct:hasVersion ?version .
+}
+"""
+ ).substitute(prefix=RDF_PREFIXES, graph=graph, comment_id=comment_id)
+ results = sparql_query(
+ query=query, endpoint=sparql_uri, format_type="json")[0]
+ if not results:
+ raise MissingDBDataException
+ return int(results["max_version"]["value"]) + 1