diff options
Diffstat (limited to 'uploader/publications/models.py')
| -rw-r--r-- | uploader/publications/models.py | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/uploader/publications/models.py b/uploader/publications/models.py index 7d2862d..dcfa02b 100644 --- a/uploader/publications/models.py +++ b/uploader/publications/models.py @@ -30,6 +30,7 @@ def create_new_publications( conn: Connection, publications: tuple[dict, ...] ) -> tuple[dict, ...]: + """Create new publications in the database.""" if len(publications) > 0: with conn.cursor(cursorclass=DictCursor) as cursor: cursor.executemany( @@ -42,17 +43,13 @@ def create_new_publications( "%(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) return tuple({ - **row, "PublicationId": row["Id"] + **row, "publication_id": row["Id"] } for row in cursor.fetchall()) - return tuple() + + return tuple() def update_publications(conn: Connection , publications: tuple[dict, ...]) -> tuple[dict, ...]: @@ -74,12 +71,25 @@ def update_publications(conn: Connection , publications: tuple[dict, ...]) -> tu return tuple() -def fetch_publications(conn: Connection) -> Iterable[dict]: - """Fetch publications from the database.""" - with conn.cursor(cursorclass=DictCursor) as cursor: - cursor.execute("SELECT * FROM Publication") - for row in cursor.fetchall(): - yield dict(row) +def delete_publications(conn: Connection , publications: tuple[dict, ...]): + """Delete multiple publications""" + publications = tuple(pub for pub in publications if bool(pub)) + if len(publications) > 0: + _pub_ids = tuple(pub["Id"] for pub in publications) + _paramstr = ", ".join(["%s"] * len(_pub_ids)) + _phenos_query = ( + "SELECT PublicationId, COUNT(PhenotypeId) FROM PublishXRef " + f"WHERE PublicationId IN ({_paramstr}) GROUP BY PublicationId;") + + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute(_phenos_query, _pub_ids) + _linked_phenos = cursor.fetchall() + if len(_linked_phenos) > 0: + raise Exception(# pylint: disable=[broad-exception-raised] + "Cannot delete publications with linked phenotypes.") + + cursor.execute( + f"DELETE FROM Publication WHERE Id IN ({_paramstr})", _pub_ids) def fetch_publication_by_id(conn: Connection, publication_id: int) -> dict: @@ -87,7 +97,8 @@ def fetch_publication_by_id(conn: Connection, publication_id: int) -> dict: with conn.cursor(cursorclass=DictCursor) as cursor: cursor.execute("SELECT * FROM Publication WHERE Id=%s", (publication_id,)) - return dict(cursor.fetchone()) + _res = cursor.fetchone() + return dict(_res) if _res else {} def fetch_publication_phenotypes( |
