From 2d26ac9ca30b33856030abd13663180fd23f45cf Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Fri, 6 May 2022 12:24:50 +0300 Subject: Move "Update History" to it's own page * wqflask/wqflask/metadata_edits.py (edit_phenotype): Move the logic for fetching the diff data from here to... (show_history): ... here. * wqflask/wqflask/templates/edit_phenotype.html: Move html section for displaying recent edits from here to ... * wqflask/wqflask/templates/edit_history.html: ... here. --- wqflask/wqflask/metadata_edits.py | 109 ++++++++++++++------------ wqflask/wqflask/templates/edit_history.html | 67 ++++++++++++++++ wqflask/wqflask/templates/edit_phenotype.html | 1 + 3 files changed, 129 insertions(+), 48 deletions(-) create mode 100644 wqflask/wqflask/templates/edit_history.html (limited to 'wqflask') diff --git a/wqflask/wqflask/metadata_edits.py b/wqflask/wqflask/metadata_edits.py index 10022ae1..00e289eb 100644 --- a/wqflask/wqflask/metadata_edits.py +++ b/wqflask/wqflask/metadata_edits.py @@ -96,55 +96,18 @@ def edit_phenotype(conn, name, dataset_id): 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") or ""], [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_, + "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), + ), } @@ -201,11 +164,11 @@ def display_phenotype_metadata(dataset_id: str, name: str): _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"), dataset_id=dataset_id, + name=name, resource_id=request.args.get("resource-id"), headers=get_case_attributes(conn).keys(), version=os.environ.get("GN_VERSION"), @@ -567,6 +530,56 @@ def show_diff(name): return render_template("display_diffs.html", diff=content) +@metadata_edit.route("//traits//history") +def show_history(dataset_id: str, name: str): + diff_data_ = None + with database_connection() as conn: + publish_xref = fetchone( + conn=conn, + table="PublishXRef", + where=PublishXRef(id_=name, inbred_set_id=dataset_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") or ""], + [data_.get("new")] + ) + ), + ), + timestamp=timestamp, + ) + ) + if len(diff_data) > 0: + diff_data_ = groupby(diff_data, lambda x: x.timestamp) + return render_template( + "edit_history.html", + diff=diff_data_, + version=os.environ.get("GN_VERSION")) + + @metadata_edit.route("/diffs//reject") @edit_admins_access_required @login_required diff --git a/wqflask/wqflask/templates/edit_history.html b/wqflask/wqflask/templates/edit_history.html new file mode 100644 index 00000000..f181fd87 --- /dev/null +++ b/wqflask/wqflask/templates/edit_history.html @@ -0,0 +1,67 @@ +{% extends "base.html" %} +{% block title %}Trait Submission{% endblock %} + +{% block css %} + +{% endblock %} + +{% block content %} + +
+ +

Edit history: {{}}

+ {% if diff %} +
+ + + + + + + + + {% set ns = namespace(display_cell=True) %} + + {% for timestamp, group in diff %} + {% set ns.display_cell = True %} + {% for i in group %} + + {% if ns.display_cell and i.timestamp == timestamp %} + + {% set author = i.author %} + {% set timestamp_ = i.timestamp %} + + {% else %} + + {% set author = "" %} + {% set timestamp_ = "" %} + + {% endif %} + + + + + {% set ns.display_cell = False %} + + {% endfor %} + {% endfor %} + +
TimestampEditorFieldDiff
{{ timestamp_ }}{{ author }}{{ i.diff.field }}
{{ i.diff.diff }}
+
+ + {% endif %} + +
+{%endblock%} + +{% block js %} + + + +{% endblock %} diff --git a/wqflask/wqflask/templates/edit_phenotype.html b/wqflask/wqflask/templates/edit_phenotype.html index e7a44bd9..3c4bbf71 100644 --- a/wqflask/wqflask/templates/edit_phenotype.html +++ b/wqflask/wqflask/templates/edit_phenotype.html @@ -14,6 +14,7 @@
{% if diff %} -- cgit v1.2.3