diff options
Diffstat (limited to 'uploader/publications')
-rw-r--r-- | uploader/publications/__init__.py | 1 | ||||
-rw-r--r-- | uploader/publications/models.py | 42 | ||||
-rw-r--r-- | uploader/publications/views.py | 92 |
3 files changed, 129 insertions, 6 deletions
diff --git a/uploader/publications/__init__.py b/uploader/publications/__init__.py index 57c0cbb..7efcabb 100644 --- a/uploader/publications/__init__.py +++ b/uploader/publications/__init__.py @@ -1 +1,2 @@ """Package for handling publications.""" +from .views import pubbp diff --git a/uploader/publications/models.py b/uploader/publications/models.py index 3fc9542..8dd62f3 100644 --- a/uploader/publications/models.py +++ b/uploader/publications/models.py @@ -1,5 +1,6 @@ """Module to handle persistence and retrieval of publication to/from MariaDB""" import logging +from typing import Iterable from MySQLdb.cursors import DictCursor @@ -41,15 +42,10 @@ 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() @@ -71,3 +67,37 @@ def update_publications(conn: Connection , publications: tuple[dict, ...]) -> tu return publications return tuple() 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 fetch_publication_by_id(conn: Connection, publication_id: int) -> dict: + """Fetch a specific publication from the database.""" + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute("SELECT * FROM Publication WHERE Id=%s", + (publication_id,)) + return dict(cursor.fetchone()) + + +def fetch_publication_phenotypes( + conn: Connection, publication_id: int) -> Iterable[dict]: + """Fetch all phenotypes linked to this publication.""" + with conn.cursor(cursorclass=DictCursor) as cursor: + cursor.execute( + "SELECT pxr.Id AS xref_id, pxr.PublicationId, phe.* " + "FROM PublishXRef AS pxr INNER JOIN Phenotype AS phe " + "ON pxr.PhenotypeId=phe.Id " + "WHERE pxr.PublicationId=%s", + (publication_id,)) + while True: + row = cursor.fetchone() + if row: + yield row + else: + break diff --git a/uploader/publications/views.py b/uploader/publications/views.py new file mode 100644 index 0000000..ebb8740 --- /dev/null +++ b/uploader/publications/views.py @@ -0,0 +1,92 @@ +"""Endpoints for publications""" +import json + +from gn_libs.mysqldb import database_connection +from flask import ( + flash, + request, + url_for, + redirect, + Blueprint, + render_template, + current_app as app) + +from uploader.authorisation import require_login + +from .models import ( + fetch_publications, + fetch_publication_by_id, + create_new_publications, + fetch_publication_phenotypes) + +from gn_libs.debug import __pk__ + +pubbp = Blueprint("publications", __name__) + + +@pubbp.route("/", methods=["GET"]) +@require_login +def index(): + """Index page for publications.""" + with database_connection(app.config["SQL_URI"]) as conn: + return render_template("publications/index.html") + + +@pubbp.route("/list", methods=["GET"]) +@require_login +def list_publications(): + with database_connection(app.config["SQL_URI"]) as conn: + return json.dumps({ + "publications": tuple({ + **row, "index": idx + } for idx,row in enumerate( + fetch_publications(conn), start=1)), + "status": "success" + }) + + +@pubbp.route("/view/<int:publication_id>", methods=["GET"]) +@require_login +def view_publication(publication_id: int): + """View more details on a particular publication.""" + with database_connection(app.config["SQL_URI"]) as conn: + return render_template( + "publications/view-publication.html", + publication=fetch_publication_by_id(conn, publication_id), + linked_phenotypes=tuple(fetch_publication_phenotypes( + conn, publication_id))) + + +@pubbp.route("/create", methods=["GET", "POST"]) +@require_login +def create_publication(): + """Create a new publication.""" + if(request.method == "GET"): + return render_template("publications/create-publication.html") + form = request.form + authors = form.get("publication-authors").encode("utf8") + if authors is None or authors == "": + flash("The publication's author(s) MUST be provided!", "alert alert-danger") + return redirect(url_for("publications.create", **request.args)) + + with database_connection(app.config["SQL_URI"]) as conn: + publications = create_new_publications(conn, ({ + "pubmed_id": form.get("pubmed-id"), + "abstract": form.get("publication-abstract").encode("utf8") or None, + "authors": authors, + "title": form.get("publication-title").encode("utf8") or None, + "journal": form.get("publication-journal").encode("utf8") or None, + "volume": form.get("publication-volume").encode("utf8") or None, + "pages": form.get("publication-pages").encode("utf8") or None, + "month": (form.get("publication-month") or "").encode("utf8").capitalize() or None, + "year": form.get("publication-year").encode("utf8") or None + },)) + flash("New publication created!", "alert alert-success") + return redirect(url_for( + request.args.get("return_to") or "publications.view_publication", + publication_id=publications[0]["publication_id"], + **request.args)) + + flash("Publication creation failed!", "alert alert-danger") + app.logger.debug("Failed to create the new publication.", exc_info=True) + return redirect(url_for("publications.create_publication")) |