diff options
Diffstat (limited to 'uploader/publications/views.py')
| -rw-r--r-- | uploader/publications/views.py | 98 |
1 files changed, 88 insertions, 10 deletions
diff --git a/uploader/publications/views.py b/uploader/publications/views.py index 0608a35..4ec832f 100644 --- a/uploader/publications/views.py +++ b/uploader/publications/views.py @@ -1,28 +1,28 @@ """Endpoints for publications""" import json -from MySQLdb.cursors import DictCursor from gn_libs.mysqldb import database_connection from flask import ( flash, request, - url_for, redirect, Blueprint, render_template, current_app as app) +from uploader.flask_extensions import url_for from uploader.authorisation import require_login +from uploader.route_utils import redirect_to_next from .models import ( + delete_publications, + update_publications, fetch_publication_by_id, create_new_publications, fetch_publication_phenotypes) from .datatables import fetch_publications -from gn_libs.debug import __pk__ - pubbp = Blueprint("publications", __name__) @@ -30,21 +30,21 @@ pubbp = Blueprint("publications", __name__) @require_login def index(): """Index page for publications.""" - with database_connection(app.config["SQL_URI"]) as conn: - return render_template("publications/index.html") + return render_template("publications/index.html") @pubbp.route("/list", methods=["GET"]) @require_login def list_publications(): + """Fetch publications that fulfill a specific search, or all of them, if + there is no search term.""" # request breakdown: # https://datatables.net/manual/server-side _page = int(request.args.get("draw")) _length = int(request.args.get("length") or '-1') _start = int(request.args.get("start") or '0') _search = request.args["search[value]"] - with (database_connection(app.config["SQL_URI"]) as conn, - conn.cursor(cursorclass=DictCursor) as cursor): + with database_connection(app.config["SQL_URI"]) as conn: _publications, _current_rows, _totalfiltered, _totalrows = fetch_publications( conn, _search, @@ -65,9 +65,15 @@ def list_publications(): def view_publication(publication_id: int): """View more details on a particular publication.""" with database_connection(app.config["SQL_URI"]) as conn: + publication = fetch_publication_by_id(conn, publication_id) + + if not bool(publication): + flash("Requested publication was not found!", "alert-warning") + return redirect(url_for('publications.index')) + return render_template( "publications/view-publication.html", - publication=fetch_publication_by_id(conn, publication_id), + publication=publication, linked_phenotypes=tuple(fetch_publication_phenotypes( conn, publication_id))) @@ -76,7 +82,7 @@ def view_publication(publication_id: int): @require_login def create_publication(): """Create a new publication.""" - if(request.method == "GET"): + if request.method == "GET": return render_template("publications/create-publication.html") form = request.form authors = form.get("publication-authors").encode("utf8") @@ -105,3 +111,75 @@ def create_publication(): 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")) + + +@pubbp.route("/edit/<int:publication_id>", methods=["GET", "POST"]) +@require_login +def edit_publication(publication_id: int): + """Edit a publication's details.""" + with database_connection(app.config["SQL_URI"]) as conn: + if request.method == "GET": + return render_template( + "publications/edit-publication.html", + publication=fetch_publication_by_id(conn, publication_id), + linked_phenotypes=tuple(fetch_publication_phenotypes( + conn, publication_id)), + publication_id=publication_id) + + form = request.form + _pub = update_publications(conn, ({ + "publication_id": publication_id, + "pubmed_id": form.get("pubmed-id") or None, + "abstract": form.get("publication-abstract").encode("utf8") or None, + "authors": form.get("publication-authors").encode("utf8"), + "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 + },)) + + if not _pub: + flash("There was an error updating the publication details.", + "alert-danger") + return redirect(url_for( + "publications.edit_publication", publication_id=publication_id)) + + flash("Successfully updated the publication details.", + "alert-success") + return redirect_to_next({ + "uri": "publications.view_publication", + "publication_id": publication_id + }) + + +@pubbp.route("/delete/<int:publication_id>", methods=["GET", "POST"]) +@require_login +def delete_publication(publication_id: int): + """Delete a particular publication.""" + with database_connection(app.config["SQL_URI"]) as conn: + publication = fetch_publication_by_id(conn, publication_id) + linked_phenotypes=tuple(fetch_publication_phenotypes( + conn, publication_id)) + + if not bool(publication): + flash("Requested publication was not found!", "alert-warning") + return redirect(url_for('publications.index')) + + if len(linked_phenotypes) > 0: + flash("Cannot delete publication with linked phenotypes!", + "alert-warning") + return redirect(url_for( + "publications.view_publication", publication_id=publication_id)) + + if request.method == "GET": + return render_template( + "publications/delete-publication.html", + publication=publication, + linked_phenotypes=linked_phenotypes, + publication_id=publication_id) + + delete_publications(conn, (publication,)) + flash("Deleted the publication successfully.", "alert-success") + return render_template("publications/delete-publication-success.html") |
