This is a basic file upload through Flask. I have used this in my F5 Dashboard project to upload a CSV file that contains the database that is used as the source for what domains require SSL profile updates.
The Process
There are two views for the CSV upload.
- The file uploader
- The database refresh
The file uploader takes CSV and saves it as “updated_db.csv”. The user is then redirected to the next page for the database refresh question.
If the user refreshes the database then the database table for domains is cleared and the new CSV data added. They are redirected to the list view to show all the domains. If the user doesn’t want to refresh the database then they are returned to the home page. The database is not changed.
Upload DB
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
@profiles_blueprint.route('/db_uploader', methods = ['GET', 'POST']) def db_uploader(): # Upload and rename file to updated_db.csv if request.method == 'POST': f = request.files['file'] f.filename = "updated_db.csv" f.save(secure_filename(f.filename)) return redirect(url_for('profiles.db_refresh')) return render_template('db_upload.html') |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
{% extends "base.html" %} {% block content %} <div class="jumbotron"> <h1>Upload the Database</h1> </div> <br> <br> <div class="container"> <div class="jumbotron"> <form action = "http://localhost:5000/profiles/db_uploader" method = "POST" enctype = "multipart/form-data"> <label for="formFile" class="form-label">Browse for a csv file to upload and update the database</label> <input class="form-control" type = "file" name = "file" /> <br> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> {% endblock %} |
Refresh DB
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@profiles_blueprint.route('/db_refresh', methods = ['GET', 'POST']) def db_refresh(): form = DBButton() if form.validate_on_submit(): session['updatedb'] = form.updatedb.data if session['updatedb'] == "yes_update_db": # OOP update DB updatedb1 = DBcontroller() updatedb1.delete_all_f5_domains() updatedb1.update_f5_domains() updatedb1.close_db_conn() return redirect(url_for('profiles.list_domains')) else: return redirect('/') return render_template('db_refresh.html', form=form) |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
{% extends "base.html" %} {% block content %} <div class="jumbotron"> <h1>Updated the Database</h1> </div> <br> <br> <div class="container"> <div class="jumbotron"> <h1>DB uploaded Successfully</h1> </div> <div class="jumbotron"> <form method="POST"> {{ form.hidden_tag() }} <div class="form-floating"></div> {{ form.updatedb.label }} {{ form.updatedb }} {{ form.submit(class="btn btn-primary") }} </div> </div> {% endblock %} |