Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add diff #86

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions isomorphe/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import difflib
import io
import logging
import os
Expand Down Expand Up @@ -178,6 +179,26 @@ def transform_original(job_id: str, uuid: str):
return Response(result.original, mimetype="text/xml", headers={"Content-Type": "text/xml"})


@app.route("/transform/success/<job_id>/diff/<uuid>")
def transform_diff(job_id: str, uuid: str):
job = get_job(job_id)
if not job or not job.result:
abort(404)
result: SuccessTransformBatchRecord | None = next(
(j for j in job.result.records if j.uuid == uuid), None
)
if not result or not result.original or not result.result:
abort(404)
diff = difflib.unified_diff(
result.original.decode("utf-8").splitlines(),
result.result.decode("utf-8").splitlines(),
fromfile=result.uuid,
tofile=result.uuid,
lineterm="",
)
return render_template("diff.html.j2", diff="\n".join(diff))


@app.route("/transform/job_status/<job_id>")
def transform_job_status(job_id: str):
url, _, _ = connection_infos()
Expand Down
30 changes: 30 additions & 0 deletions isomorphe/templates/diff.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Écosphères ISOmorphe">
<meta name="keywords" content="ecospheres, geonetwork, iso-19139">
<title>ISOmorphe diff</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" />
</head>
<body>
<main role="main" id="content">
<div id="diff-content"></div>
</main>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
<script type="text/javascript">
const targetElement = document.getElementById('diff-content');
const configuration = {
drawFileList: false,
matching: 'lines',
inputFormat: 'diff',
outputFormat: 'side-by-side',
synchronisedScroll: true,
fileContentToggle: false,
};
const diff2htmlUi = new Diff2HtmlUI(targetElement, `{{ diff }}`, configuration);
diff2htmlUi.draw();
</script>
</body>
</html>
6 changes: 6 additions & 0 deletions isomorphe/templates/fragments/transform_job_status.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<th scope="col">Fiche originale</th>
<th scope="col">XML original</th>
<th scope="col">XML transformé</th>
<th scope="col">Différences</th>
<th scope="col">Avertissements</th>
</tr>
</thead>
Expand All @@ -159,6 +160,11 @@
target="_blank"
rel="noopener">Voir le XML</a>
</td>
<td>
<a href="{{ url_for('transform_diff', job_id=job.id, uuid=record.uuid) }}"
target="_blank"
rel="noopener">Voir le diff</a>
</td>
<td>
{{ record | record_transform_log }}
</td>
Expand Down
1 change: 1 addition & 0 deletions isomorphe/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@


def xml_to_string(tree: etree._ElementTree, format: dict = XML_FORMAT):
etree.indent(tree, space=" ")
return etree.tostring(tree, **format)
Loading