Major refactor

Refactor the templates to include a header, use csv-metadata-quality
in a subshell instead of trying to import and pass args to it (which
I actually managed to do, but then trying to capture the output was
an issue), and use ansi2html to prepare the CLI output as the HTML.
This commit is contained in:
Alan Orth 2021-03-12 19:14:49 +02:00
parent ed747b2cef
commit e13d63bf6b
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9
6 changed files with 50 additions and 6 deletions

29
main.py
View File

@ -1,5 +1,8 @@
import os
import subprocess
import sys
from ansi2html import Ansi2HTMLConverter
from csv_metadata_quality.version import VERSION as cli_version
from flask import Flask, abort, redirect, render_template, request, url_for
from werkzeug.utils import secure_filename
@ -32,9 +35,33 @@ def upload_file():
return "No file selected"
# TODO: probably use a base64- and URL-encoded version of the filename here so
# we can allow results to be saved and shared?
@app.route("/process/<filename>")
def process_file(filename):
return render_template("process.html", cli_version=cli_version, filename=filename)
# do we need to use secure_filename again here?
input_file = os.path.join(app.config["UPLOAD_PATH"], filename)
# TODO: write an output file based on the input file name
output_file = os.path.join(app.config["UPLOAD_PATH"], "test.csv")
sys.argv = ["", "-i", input_file, "-o", output_file]
# run subprocess and capture output as UTF-8 so we get a string instead of
# bytes for ansi2html
results = subprocess.run(
["csv-metadata-quality", "-i", input_file, "-o", output_file],
capture_output=True,
encoding="UTF-8",
)
# convert the output to HTML using ansi2html
conv = Ansi2HTMLConverter()
html = conv.convert(results.stdout)
return render_template(
"process.html", cli_version=cli_version, filename=filename, stdout=html
)
# I should remember this Flask-specific way to send files to the client
# return send_from_directory(app.config["UPLOAD_FOLDER"], filename)
if __name__ == "__main__":

View File

@ -9,6 +9,7 @@ license = "AGPL-3.0-only"
python = "^3.7.1"
csv-metadata-quality = {git = "https://github.com/ilri/csv-metadata-quality.git", rev = "web"}
Flask = "^1.1.2"
ansi2html = "^1.6.0"
[tool.poetry.dev-dependencies]
black = "^20.8b1"

View File

@ -26,7 +26,7 @@
//@import "../../node_modules/bootstrap/scss/dropdown";
//@import "../../node_modules/bootstrap/scss/button-group";
//@import "../../node_modules/bootstrap/scss/nav";
//@import "../../node_modules/bootstrap/scss/navbar";
@import "../../node_modules/bootstrap/scss/navbar";
//@import "../../node_modules/bootstrap/scss/card";
//@import "../../node_modules/bootstrap/scss/accordion";
//@import "../../node_modules/bootstrap/scss/breadcrumb";

7
templates/header.html Normal file
View File

@ -0,0 +1,7 @@
<header>
<div class="navbar navbar-dark bg-dark shadow-sm">
<div class="container">
<span class="navbar-brand text-white text-decoration-none">DSpace CSV Metadata Quality Checker</span>
</div>
</div>
</header>

View File

@ -2,9 +2,9 @@
<html lang="en" class="h-100">
{% include 'head.html' %}
<body class="d-flex flex-column h-100">
{% include 'header.html' %}
<main class="flex-shrink-0">
<div class="container">
<h1 class="mt-3 fs-2">DSpace CSV Metadata Quality Checker</h1>
<div class="container py-3">
<p class="lead">The DSpace CSV Metadata Quality Checker is a pipeline of sanity checks and automated fixes for a number of common issues in metadata files.</p>
<p>Select a CSV file to process.</p>
<form method="POST" action="" enctype="multipart/form-data">

View File

@ -2,13 +2,22 @@
<html lang="en" class="h-100">
{% include 'head.html' %}
<body class="d-flex flex-column h-100">
{% include 'header.html' %}
<main class="flex-shrink-0">
<div class="container">
<h1 class="mt-3 fs-2">DSpace CSV Metadata Quality Checker</h1>
<p>Processing {{ filename }}</p>
<h1 class="mt-3 fs-2">Results</h1>
<p>Processing <code>{{ filename }}</code>.</p>
</div>
</main>
<div class="row justify-content-center">
<div class="col-md-10">
<blockquote>
{{ stdout | safe }}
</blockquote>
</div>
</div>
{% include 'footer.html' %}
</body>
</html>