routes.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from flask import request, render_template, send_from_directory, jsonify, send_file, abort
  2. from app import app, forms
  3. from uuid import uuid4
  4. import os
  5. @app.route('/', methods=['GET', 'POST'])
  6. def index():
  7. #
  8. # web view to upload data files
  9. #
  10. form = forms.UploadForm()
  11. # get validated form
  12. if form.validate_on_submit():
  13. # save file
  14. uuid = str(uuid4())
  15. form.file.data.save(os.path.join(os.path.dirname(__file__), app.config['UPLOAD_FOLDER'], uuid))
  16. app.logger.info(f'Uploaded file {uuid}')
  17. return render_template('upload.html', form=form, uuid=uuid, filename=form.file.data.filename)
  18. return render_template('upload.html', form=form)
  19. @app.route('/save', methods=['POST'])
  20. def save_file():
  21. #
  22. # save uploaded file and return corresponding UUID
  23. #
  24. # check for file in request
  25. file = request.files.get('dataFile')
  26. if file is None:
  27. app.logger.error('Request does not contain dataFile')
  28. return jsonify({'error': 'Request does not contain dataFile'}), 400
  29. # save file
  30. uuid = str(uuid4())
  31. file.save(os.path.join(os.path.dirname(__file__), app.config['UPLOAD_FOLDER'], uuid))
  32. app.logger.info(f'Uploaded file {uuid}')
  33. return jsonify({'uuid': uuid}), 200
  34. @app.route('/update/<string:uuid>', methods=['POST'])
  35. def update_file(uuid):
  36. #
  37. # update UUID file by uploaded one and return corresponding UUID
  38. #
  39. # check for file in request
  40. file = request.files.get('dataFile')
  41. if file is None:
  42. app.logger.error('Request does not contain dataFile')
  43. return jsonify({'error': 'Request does not contain dataFile'}), 400
  44. # update file
  45. file.save(os.path.join(os.path.dirname(__file__), app.config['UPLOAD_FOLDER'], uuid))
  46. app.logger.info(f'Updated file {uuid}')
  47. return jsonify({'uuid': uuid}), 200
  48. @app.route('/upload/<string:uuid>', methods=['POST'])
  49. def upload_files(uuid):
  50. #
  51. # save uploaded files to directory 'UUID'
  52. #
  53. if not request.files:
  54. app.logger.error('Request does not contain files to upload')
  55. return jsonify({'error': 'Request does not contain files to upload'}), 400
  56. # create upload dir
  57. upload_dir = os.path.join(os.path.dirname(__file__), app.config['UPLOAD_FOLDER'], uuid)
  58. os.makedirs(upload_dir, exist_ok=True)
  59. app.logger.info(f'Created directory: {upload_dir}')
  60. # save files
  61. upload_response = {}
  62. for filename, file in request.files.items():
  63. file.save(os.path.join(upload_dir, filename))
  64. upload_response['filename'] = 'OK'
  65. app.logger.info(f'Uploaded file {filename} to {upload_dir}')
  66. return jsonify({'uuid': uuid, 'files': upload_response}), 200
  67. @app.route('/get/<string:uuid>/<string:file>')
  68. def get_logfile(uuid, file):
  69. #
  70. # retrieve files from folder UUID
  71. #
  72. # log file
  73. if file == app.config['TESTRUN_LOGFILE']:
  74. app.logger.info(f'Requested logfile from {uuid}')
  75. return send_file(
  76. '/'.join((app.config['UPLOAD_FOLDER'], uuid, app.config['TESTRUN_LOGFILE'])),
  77. mimetype='text/plain',
  78. attachment_filename=f'{uuid}.log',
  79. )
  80. # redults file
  81. elif file == app.config['TESTRUN_RESULTS']:
  82. app.logger.info(f'Requested output file from {uuid}')
  83. return send_file(
  84. '/'.join((app.config['UPLOAD_FOLDER'], uuid, app.config['TESTRUN_RESULTS'])),
  85. attachment_filename=f'{uuid}.xlsx',
  86. as_attachment=True,
  87. )
  88. abort(404)
  89. @app.route('/get/<string:uuid>')
  90. def get_file(uuid):
  91. #
  92. # retrieve a file by UUID
  93. #
  94. app.logger.info(f'Requested file {uuid}')
  95. #return send_from_directory(app.config['UPLOAD_FOLDER'], uuid)
  96. return send_file(
  97. '/'.join((app.config['UPLOAD_FOLDER'], uuid)),
  98. attachment_filename=f'{uuid}.xlsx',
  99. as_attachment=True,
  100. )