about summary refs log tree commit diff
path: root/wqflask
diff options
context:
space:
mode:
authorBonfaceKilz2021-10-26 17:10:33 +0300
committerBonfaceKilz2021-10-28 08:34:35 +0300
commitca23d4ed6943d25c14ffac767b64fd60bded515e (patch)
tree1e49e6ebd480be2425c48ab6c2941c6a08f6cbe4 /wqflask
parent3181bd6261b09a5e9b027256057c21c49792bd32 (diff)
downloadgenenetwork2-ca23d4ed6943d25c14ffac767b64fd60bded515e.tar.gz
Move endpoints for viewing metadata to own module
Diffstat (limited to 'wqflask')
-rw-r--r--wqflask/wqflask/__init__.py3
-rw-r--r--wqflask/wqflask/metadata_edits.py147
-rw-r--r--wqflask/wqflask/views.py115
3 files changed, 153 insertions, 112 deletions
diff --git a/wqflask/wqflask/__init__.py b/wqflask/wqflask/__init__.py
index 5b2d05d1..a5097287 100644
--- a/wqflask/wqflask/__init__.py
+++ b/wqflask/wqflask/__init__.py
@@ -11,6 +11,8 @@ from utility import formatting
 
 from wqflask.resource_manager import resource_management
 
+from wqflask.metadata_edits import metadata_edit
+
 from wqflask.api.markdown import glossary_blueprint
 from wqflask.api.markdown import references_blueprint
 from wqflask.api.markdown import links_blueprint
@@ -60,6 +62,7 @@ app.register_blueprint(news_blueprint, url_prefix="/news")
 
 app.register_blueprint(resource_management, url_prefix="/resource-management")
 
+app.register_blueprint(metadata_edit, url_prefix="/datasets/")
 
 @app.before_request
 def before_request():
diff --git a/wqflask/wqflask/metadata_edits.py b/wqflask/wqflask/metadata_edits.py
new file mode 100644
index 00000000..94e2710b
--- /dev/null
+++ b/wqflask/wqflask/metadata_edits.py
@@ -0,0 +1,147 @@
+import MySQLdb
+import os
+import json
+import difflib
+
+
+from collections import namedtuple
+from flask import Blueprint, current_app, render_template, request
+from itertools import groupby
+
+from wqflask.decorators import edit_access_required
+
+from gn3.db import diff_from_dict
+from gn3.db import fetchall
+from gn3.db import fetchone
+from gn3.db import insert
+from gn3.db import update
+from gn3.db.metadata_audit import MetadataAudit
+from gn3.db.phenotypes import Phenotype
+from gn3.db.phenotypes import Probeset
+from gn3.db.phenotypes import Publication
+from gn3.db.phenotypes import PublishXRef
+from gn3.db.phenotypes import probeset_mapping
+
+
+metadata_edit = Blueprint('metadata_edit', __name__)
+
+
+def edit_phenotype(conn, name, dataset_id):
+    publish_xref = fetchone(
+        conn=conn,
+        table="PublishXRef",
+        where=PublishXRef(id_=name,
+                          inbred_set_id=dataset_id))
+    phenotype_ = fetchone(
+        conn=conn,
+        table="Phenotype",
+        where=Phenotype(id_=publish_xref.phenotype_id))
+    publication_ = fetchone(
+        conn=conn,
+        table="Publication",
+        where=Publication(id_=publish_xref.publication_id))
+    json_data = fetchall(
+        conn,
+        "metadata_audit",
+        where=MetadataAudit(dataset_id=publish_xref.id_))
+    Edit = namedtuple("Edit", ["field", "old", "new", "diff"])
+    Diff = namedtuple("Diff", ["author", "diff", "timestamp"])
+    diff_data = []
+    for data in json_data:
+        json_ = json.loads(data.json_data)
+        timestamp = json_.get("timestamp")
+        author = json_.get("author")
+        for key, value in json_.items():
+            if isinstance(value, dict):
+                for field, data_ in value.items():
+                    diff_data.append(
+                        Diff(author=author,
+                             diff=Edit(field,
+                                       data_.get("old"),
+                                       data_.get("new"),
+                                       "\n".join(difflib.ndiff(
+                                           [data_.get("old")],
+                                           [data_.get("new")]))),
+                             timestamp=timestamp))
+    diff_data_ = None
+    if len(diff_data) > 0:
+        diff_data_ = groupby(diff_data, lambda x: x.timestamp)
+    return {
+        "diff": diff_data_,
+        "publish_xref": publish_xref,
+        "phenotype": phenotype_,
+        "publication": publication_,
+    }
+
+
+def edit_probeset(conn, name):
+    probeset_ = fetchone(conn=conn,
+                         table="ProbeSet",
+                         columns=list(probeset_mapping.values()),
+                         where=Probeset(name=name))
+    json_data = fetchall(
+        conn,
+        "metadata_audit",
+        where=MetadataAudit(dataset_id=probeset_.id_))
+    Edit = namedtuple("Edit", ["field", "old", "new", "diff"])
+    Diff = namedtuple("Diff", ["author", "diff", "timestamp"])
+    diff_data = []
+    for data in json_data:
+        json_ = json.loads(data.json_data)
+        timestamp = json_.get("timestamp")
+        author = json_.get("author")
+        for key, value in json_.items():
+            if isinstance(value, dict):
+                for field, data_ in value.items():
+                    diff_data.append(
+                        Diff(author=author,
+                             diff=Edit(field,
+                                       data_.get("old"),
+                                       data_.get("new"),
+                                       "\n".join(difflib.ndiff(
+                                           [data_.get("old")],
+                                           [data_.get("new")]))),
+                             timestamp=timestamp))
+    diff_data_ = None
+    if len(diff_data) > 0:
+        diff_data_ = groupby(diff_data, lambda x: x.timestamp)
+    return {
+        "diff": diff_data_,
+        "probeset": probeset_,
+    }
+
+
+@metadata_edit.route("/<dataset_id>/traits/<name>/edit")
+@edit_access_required
+def display_phenotype_metadata(dataset_id: str, name: str):
+    conn = MySQLdb.Connect(db=current_app.config.get("DB_NAME"),
+                           user=current_app.config.get("DB_USER"),
+                           passwd=current_app.config.get("DB_PASS"),
+                           host=current_app.config.get("DB_HOST"))
+    _d = edit_phenotype(conn=conn, name=name, dataset_id=dataset_id)
+    return render_template(
+        "edit_phenotype.html",
+        diff=_d.get("diff"),
+        publish_xref=_d.get("publish_xref"),
+        phenotype=_d.get("phenotype"),
+        publication=_d.get("publication"),
+        resource_id=request.args.get("resource-id"),
+        version=os.environ.get("GN_VERSION"),
+    )
+
+
+@metadata_edit.route("/traits/<name>/edit")
+@edit_access_required
+def display_probeset_metadata(name: str):
+    conn = MySQLdb.Connect(db=current_app.config.get("DB_NAME"),
+                           user=current_app.config.get("DB_USER"),
+                           passwd=current_app.config.get("DB_PASS"),
+                           host=current_app.config.get("DB_HOST"))
+    _d = edit_probeset(conn=conn, name=name)
+    return render_template(
+        "edit_probeset.html",
+        diff=_d.get("diff"),
+        probeset=_d.get("probeset"),
+        resource_id=request.args.get("resource-id"),
+        version=os.environ.get("GN_VERSION"),
+    )
diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py
index b0da1f21..463b7c3a 100644
--- a/wqflask/wqflask/views.py
+++ b/wqflask/wqflask/views.py
@@ -4,7 +4,6 @@ import MySQLdb
 import array
 import base64
 import csv
-import difflib
 import datetime
 import flask
 import io  # Todo: Use cStringIO?
@@ -20,8 +19,6 @@ import traceback
 import uuid
 import xlsxwriter
 
-from itertools import groupby
-from collections import namedtuple
 from zipfile import ZipFile
 from zipfile import ZIP_DEFLATED
 
@@ -30,19 +27,12 @@ from wqflask import app
 from gn3.commands import run_cmd
 from gn3.computations.gemma import generate_hash_of_string
 from gn3.db import diff_from_dict
-from gn3.db import fetchall
-from gn3.db import fetchone
 from gn3.db import insert
 from gn3.db import update
 from gn3.db.metadata_audit import MetadataAudit
 from gn3.db.phenotypes import Phenotype
 from gn3.db.phenotypes import Probeset
 from gn3.db.phenotypes import Publication
-from gn3.db.phenotypes import PublishXRef
-from gn3.db.phenotypes import probeset_mapping
-# from gn3.db.traits import get_trait_csv_sample_data
-# from gn3.db.traits import update_sample_data
-
 
 from flask import current_app
 from flask import g
@@ -426,106 +416,6 @@ def submit_trait_form():
         version=GN_VERSION)
 
 
-@app.route("/trait/<name>/edit/inbredset-id/<inbredset_id>")
-@edit_access_required
-def edit_phenotype(name, inbredset_id):
-    conn = MySQLdb.Connect(db=current_app.config.get("DB_NAME"),
-                           user=current_app.config.get("DB_USER"),
-                           passwd=current_app.config.get("DB_PASS"),
-                           host=current_app.config.get("DB_HOST"))
-    publish_xref = fetchone(
-        conn=conn,
-        table="PublishXRef",
-        where=PublishXRef(id_=name,
-                          inbred_set_id=inbredset_id))
-    phenotype_ = fetchone(
-        conn=conn,
-        table="Phenotype",
-        where=Phenotype(id_=publish_xref.phenotype_id))
-    publication_ = fetchone(
-        conn=conn,
-        table="Publication",
-        where=Publication(id_=publish_xref.publication_id))
-    json_data = fetchall(
-        conn,
-        "metadata_audit",
-        where=MetadataAudit(dataset_id=publish_xref.id_))
-
-    Edit = namedtuple("Edit", ["field", "old", "new", "diff"])
-    Diff = namedtuple("Diff", ["author", "diff", "timestamp"])
-    diff_data = []
-    for data in json_data:
-        json_ = json.loads(data.json_data)
-        timestamp = json_.get("timestamp")
-        author = json_.get("author")
-        for key, value in json_.items():
-            if isinstance(value, dict):
-                for field, data_ in value.items():
-                    diff_data.append(
-                        Diff(author=author,
-                             diff=Edit(field,
-                                       data_.get("old"),
-                                       data_.get("new"),
-                                       "\n".join(difflib.ndiff(
-                                           [data_.get("old")],
-                                           [data_.get("new")]))),
-                             timestamp=timestamp))
-    diff_data_ = None
-    if len(diff_data) > 0:
-        diff_data_ = groupby(diff_data, lambda x: x.timestamp)
-    return render_template(
-        "edit_phenotype.html",
-        diff=diff_data_,
-        publish_xref=publish_xref,
-        phenotype=phenotype_,
-        publication=publication_,
-        version=GN_VERSION,
-    )
-
-
-@app.route("/trait/edit/probeset-name/<dataset_name>")
-@edit_access_required
-def edit_probeset(dataset_name):
-    conn = MySQLdb.Connect(db=current_app.config.get("DB_NAME"),
-                           user=current_app.config.get("DB_USER"),
-                           passwd=current_app.config.get("DB_PASS"),
-                           host=current_app.config.get("DB_HOST"))
-    probeset_ = fetchone(conn=conn,
-                         table="ProbeSet",
-                         columns=list(probeset_mapping.values()),
-                         where=Probeset(name=dataset_name))
-    json_data = fetchall(
-        conn,
-        "metadata_audit",
-        where=MetadataAudit(dataset_id=probeset_.id_))
-    Edit = namedtuple("Edit", ["field", "old", "new", "diff"])
-    Diff = namedtuple("Diff", ["author", "diff", "timestamp"])
-    diff_data = []
-    for data in json_data:
-        json_ = json.loads(data.json_data)
-        timestamp = json_.get("timestamp")
-        author = json_.get("author")
-        for key, value in json_.items():
-            if isinstance(value, dict):
-                for field, data_ in value.items():
-                    diff_data.append(
-                        Diff(author=author,
-                             diff=Edit(field,
-                                       data_.get("old"),
-                                       data_.get("new"),
-                                       "\n".join(difflib.ndiff(
-                                           [data_.get("old")],
-                                           [data_.get("new")]))),
-                             timestamp=timestamp))
-    diff_data_ = None
-    if len(diff_data) > 0:
-        diff_data_ = groupby(diff_data, lambda x: x.timestamp)
-    return render_template(
-        "edit_probeset.html",
-        diff=diff_data_,
-        probeset=probeset_)
-
-
 @app.route("/trait/update", methods=["POST"])
 @edit_access_required
 def update_phenotype():
@@ -653,7 +543,6 @@ def update_phenotype():
 
 
 @app.route("/probeset/update", methods=["POST"])
-@edit_access_required
 def update_probeset():
     conn = MySQLdb.Connect(db=current_app.config.get("DB_NAME"),
                            user=current_app.config.get("DB_USER"),
@@ -691,7 +580,9 @@ def update_probeset():
         where=Probeset(id_=data_.get("id")))
 
     diff_data = {}
-    author = g.user_session.record.get(b'user_name')
+    author = (g.user_session.record.get(b"user_id",
+                                         b"").decode("utf-8") or
+              g.user_session.record.get("user_id", ""))
     if updated_probeset:
         diff_data.update({"Probeset": diff_from_dict(old={
             k: data_.get(f"old_{k}") for k, v in probeset_.items()