index.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. '''
  2. @author: olivier.massot, sept 218
  3. '''
  4. import importlib
  5. from tempfile import TemporaryDirectory
  6. from flask import request
  7. from flask.app import Flask
  8. from flask.templating import render_template
  9. from path import Path
  10. from werkzeug.utils import secure_filename
  11. from core.constants import MAIN
  12. app = Flask(__name__)
  13. @app.route('/', methods=['GET'])
  14. def index():
  15. return render_template("index.html", validation_error="")
  16. @app.route('/report', methods=['POST'])
  17. def report():
  18. try:
  19. f = request.files['dossier']
  20. except KeyError:
  21. return render_template("index.html", validation_error="Aucun fichier sélectionné")
  22. filename = secure_filename(f.filename)
  23. if Path(filename).ext != ".zip":
  24. return render_template("index.html", validation_error="Le fichier doit être un fichier .ZIP ({})".format(Path(filename).ext))
  25. schema_lib_name = request.form['schema']
  26. schema_lib = importlib.import_module("schemas." + schema_lib_name)
  27. # try:
  28. with TemporaryDirectory(dir=MAIN / "upload") as d:
  29. filename = Path(d) / filename
  30. f.save(filename)
  31. report = schema_lib.validator.submit(filename)
  32. # except Exception as e:
  33. # return render_template("index.html", validation_error=str(e))
  34. return render_template("report.html", report=report)