Skip to content

Commit

Permalink
feat: add diff
Browse files Browse the repository at this point in the history
  • Loading branch information
abulte committed Oct 31, 2024
1 parent 5c6b0b1 commit df8ac21
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
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)

0 comments on commit df8ac21

Please sign in to comment.