about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2025-04-14 09:00:07 -0500
committerFrederick Muriuki Muriithi2025-04-14 09:00:07 -0500
commit520690ddb83b7898ec74bd8c1a26c3e078903d47 (patch)
tree75461814abe745400f5b5c43d51a40979616eb49
parent0d2428737223fd79857457f7986da9c199d9d4fa (diff)
downloadgn-uploader-520690ddb83b7898ec74bd8c1a26c3e078903d47.tar.gz
Move code to save new publications to database.
-rw-r--r--scripts/phenotypes_bulk_edit.py31
-rw-r--r--uploader/publications/models.py32
2 files changed, 32 insertions, 31 deletions
diff --git a/scripts/phenotypes_bulk_edit.py b/scripts/phenotypes_bulk_edit.py
index 4888924..20fa66b 100644
--- a/scripts/phenotypes_bulk_edit.py
+++ b/scripts/phenotypes_bulk_edit.py
@@ -85,37 +85,6 @@ def descriptions_differences(file_data, db_data) -> dict[str, str]:
     return diff
 
 
-def __save_new_publications__(conn, publications, pubmed_ids) -> dict:
-    if len(publications) > 0:
-        with conn.cursor(cursorclass=DictCursor) as cursor:
-            cursor.executemany(
-                ("INSERT INTO "
-                 "Publication( "
-                 "PubMed_ID, Abstract, Authors, Title, Journal, Volume, Pages, "
-                 "Month, Year"
-                 ") "
-                 "VALUES("
-                 "%(pubmed_id)s, %(abstract)s, %(authors)s, %(title)s, "
-                 "%(journal)s, %(volume)s, %(pages)s, %(month)s, %(year)s"
-                 ") "
-                 "ON DUPLICATE KEY UPDATE "
-                 "Abstract=VALUES(Abstract), Authors=VALUES(Authors), "
-                 "Title=VALUES(Title), Journal=VALUES(Journal), "
-                 "Volume=VALUES(Volume), Pages=VALUES(pages), "
-                 "Month=VALUES(Month), Year=VALUES(Year)"),
-                publications)
-
-            paramstr = ", ".join(["%s"] * len(pubmed_ids))
-            cursor.execute(
-                ("SELECT Id, PubMed_ID FROM Publication "
-                 f"WHERE PubMed_ID IN ({paramstr})"),
-                pubmed_ids)
-            return {
-                row["PubMed_ID"]: row["Id"] for row in cursor.fetchall()
-            }
-        return {}
-
-
 def publications_differences(conn, file_data, db_data, pubmed_ids) -> dict:
     """Compute differences in the publications."""
     logger.info("Computing differences in publications.")
diff --git a/uploader/publications/models.py b/uploader/publications/models.py
index 89da06c..e7f197a 100644
--- a/uploader/publications/models.py
+++ b/uploader/publications/models.py
@@ -13,3 +13,35 @@ def fetch_phenotype_publications(
     with conn.cursor(cursorclass=DictCursor) as cursor:
         cursor.execute(query, tuple(item for row in ids for item in row))
         return tuple(dict(row) for row in cursor.fetchall())
+
+
+def create_new_publications(conn, publications) -> dict:
+    if len(publications) > 0:
+        with conn.cursor(cursorclass=DictCursor) as cursor:
+            cursor.executemany(
+                ("INSERT INTO "
+                 "Publication( "
+                 "PubMed_ID, Abstract, Authors, Title, Journal, Volume, Pages, "
+                 "Month, Year"
+                 ") "
+                 "VALUES("
+                 "%(pubmed_id)s, %(abstract)s, %(authors)s, %(title)s, "
+                 "%(journal)s, %(volume)s, %(pages)s, %(month)s, %(year)s"
+                 ") "
+                 "ON DUPLICATE KEY UPDATE "
+                 "Abstract=VALUES(Abstract), Authors=VALUES(Authors), "
+                 "Title=VALUES(Title), Journal=VALUES(Journal), "
+                 "Volume=VALUES(Volume), Pages=VALUES(pages), "
+                 "Month=VALUES(Month), Year=VALUES(Year) "
+                 "RETURNING "),
+                publications)
+
+            paramstr = ", ".join(["%s"] * len(pubmed_ids))
+            cursor.execute(
+                ("SELECT Id, PubMed_ID FROM Publication "
+                 f"WHERE PubMed_ID IN ({paramstr})"),
+                pubmed_ids)
+            return {
+                row["PubMed_ID"]: row["Id"] for row in cursor.fetchall()
+            }
+        return {}