from flask import (Flask, render_template, flash, redirect, url_for, request, send_from_directory) from flask_bootstrap import Bootstrap from flask_wtf import FlaskForm from wtforms import DateTimeField, DecimalField, SubmitField from wtforms.validators import DataRequired, NumberRange from datetime import datetime, timedelta from config import Config app = Flask(__name__) app.config.from_object(Config) Bootstrap(app) class DownloadForm(FlaskForm): start_time = DateTimeField('Start time', validators=[DataRequired()]) length = DecimalField('Length', validators=[DataRequired(), NumberRange(min=1, max=app.config['MAX_LENGTH'])]) submit = SubmitField('Download') @app.route('/', methods=['GET', 'POST']) def download(): form = DownloadForm() if request.method == 'GET': form.start_time.data = datetime.now() - timedelta(hours=1) form.length.data = 60 elif form.validate_on_submit(): output_filename = prepare_download(form) if output_filename: flash('The download should start immediately.', 'success') return render_template('download.html', form=form, filename=output_filename) else: flash('Error preparing download!', 'warning') else: flash('Error in form!', 'warning') return render_template('download.html', form=form) @app.route('/download_file/') def download_file(filename): return send_from_directory(app.config['DATA_DIR'], filename, as_attachment=True) def prepare_download(form): return 'test.mp3' if __name__ == '__main__': app.run(debug=True)