From d8fa6d066d9c37d65bf6e3388b5bb9583b6a9436 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Mon, 7 Jun 2021 18:37:59 +0300 Subject: wqflask: views: Pass diffs to template for further processing --- wqflask/wqflask/views.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index 933dc507..ebe5303a 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -4,9 +4,11 @@ import MySQLdb import array import base64 import csv +import difflib import datetime import flask import io # Todo: Use cStringIO? + import json import numpy as np import os @@ -18,12 +20,15 @@ import traceback import uuid import xlsxwriter +from itertools import groupby +from collections import namedtuple from zipfile import ZipFile from zipfile import ZIP_DEFLATED from wqflask import app 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 @@ -449,12 +454,41 @@ def edit_trait(name, inbred_set_id): 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_trait.html", + diff=diff_data_, publish_xref=publish_xref, phenotype=phenotype_, publication=publication_, - version=GN_VERSION) + version=GN_VERSION, + ) @app.route("/trait/update", methods=["POST"]) -- cgit v1.2.3