123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- from flask import request, render_template, send_from_directory, jsonify, send_file, abort
- from app import app, forms
- from uuid import uuid4
- import os
- @app.route('/', methods=['GET', 'POST'])
- def index():
- #
- # web view to upload data files
- #
-
- form = forms.UploadForm()
-
- # get validated form
- if form.validate_on_submit():
- # save file
- uuid = str(uuid4())
- form.file.data.save(os.path.join(os.path.dirname(__file__), app.config['UPLOAD_FOLDER'], uuid))
- app.logger.info(f'Uploaded file {uuid}')
- return render_template('upload.html', form=form, uuid=uuid, filename=form.file.data.filename)
- return render_template('upload.html', form=form)
- @app.route('/save', methods=['POST'])
- def save_file():
- #
- # save uploaded file and return corresponding UUID
- #
- # check for file in request
- file = request.files.get('dataFile')
- if file is None:
- app.logger.error('Request does not contain dataFile')
- return jsonify({'error': 'Request does not contain dataFile'}), 400
- # save file
- uuid = str(uuid4())
- file.save(os.path.join(os.path.dirname(__file__), app.config['UPLOAD_FOLDER'], uuid))
- app.logger.info(f'Uploaded file {uuid}')
- return jsonify({'uuid': uuid}), 200
- @app.route('/update/<string:uuid>', methods=['POST'])
- def update_file(uuid):
- #
- # update UUID file by uploaded one and return corresponding UUID
- #
- # check for file in request
- file = request.files.get('dataFile')
- if file is None:
- app.logger.error('Request does not contain dataFile')
- return jsonify({'error': 'Request does not contain dataFile'}), 400
- # update file
- file.save(os.path.join(os.path.dirname(__file__), app.config['UPLOAD_FOLDER'], uuid))
- app.logger.info(f'Updated file {uuid}')
- return jsonify({'uuid': uuid}), 200
- @app.route('/upload/<string:uuid>', methods=['POST'])
- def upload_files(uuid):
- #
- # save uploaded files to directory 'UUID'
- #
- if not request.files:
- app.logger.error('Request does not contain files to upload')
- return jsonify({'error': 'Request does not contain files to upload'}), 400
- # create upload dir
- upload_dir = os.path.join(os.path.dirname(__file__), app.config['UPLOAD_FOLDER'], uuid)
- os.makedirs(upload_dir, exist_ok=True)
- app.logger.info(f'Created directory: {upload_dir}')
- # save files
- upload_response = {}
- for filename, file in request.files.items():
- file.save(os.path.join(upload_dir, filename))
- upload_response['filename'] = 'OK'
- app.logger.info(f'Uploaded file {filename} to {upload_dir}')
- return jsonify({'uuid': uuid, 'files': upload_response}), 200
- @app.route('/get/<string:uuid>/<string:file>')
- def get_logfile(uuid, file):
- #
- # retrieve files from folder UUID
- #
-
- # log file
- if file == app.config['TESTRUN_LOGFILE']:
- app.logger.info(f'Requested logfile from {uuid}')
- return send_file(
- '/'.join((app.config['UPLOAD_FOLDER'], uuid, app.config['TESTRUN_LOGFILE'])),
- mimetype='text/plain',
- attachment_filename=f'{uuid}.log',
- )
- # redults file
- elif file == app.config['TESTRUN_RESULTS']:
- app.logger.info(f'Requested output file from {uuid}')
- return send_file(
- '/'.join((app.config['UPLOAD_FOLDER'], uuid, app.config['TESTRUN_RESULTS'])),
- attachment_filename=f'{uuid}.xlsx',
- as_attachment=True,
- )
- abort(404)
- @app.route('/get/<string:uuid>')
- def get_file(uuid):
- #
- # retrieve a file by UUID
- #
- app.logger.info(f'Requested file {uuid}')
- #return send_from_directory(app.config['UPLOAD_FOLDER'], uuid)
- return send_file(
- '/'.join((app.config['UPLOAD_FOLDER'], uuid)),
- attachment_filename=f'{uuid}.xlsx',
- as_attachment=True,
- )
|