Add 'Image Upload' Feature
Each user can upload image to the system. The images from different users where be separated.
This commit is contained in:
50
app.py
50
app.py
@@ -1,6 +1,13 @@
|
||||
import os
|
||||
import datetime
|
||||
import hashlib
|
||||
from flask import Flask, session, url_for, redirect, render_template, request, abort, flash
|
||||
from database import list_users, verify, delete_user_from_db, add_user
|
||||
from database import read_note_from_db, write_note_into_db, delete_note_from_db, match_user_id_with_note_id
|
||||
from database import image_upload_record, list_images_for_user
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object('config')
|
||||
@@ -23,6 +30,10 @@ def FUN_404(error):
|
||||
def FUN_405(error):
|
||||
return render_template("page_405.html"), 405
|
||||
|
||||
@app.errorhandler(413)
|
||||
def FUN_413(error):
|
||||
return render_template("page_413.html"), 413
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -43,7 +54,13 @@ def FUN_private():
|
||||
[x[1] for x in notes_list],\
|
||||
[x[2] for x in notes_list],\
|
||||
["/delete_note/" + x[0] for x in notes_list])
|
||||
return render_template("private_page.html", notes = notes_table)
|
||||
|
||||
images_list = list_images_for_user(session['current_user'])
|
||||
images_table = zip([x[0] for x in images_list],\
|
||||
[x[1] for x in images_list],\
|
||||
[x[2] for x in images_list])
|
||||
|
||||
return render_template("private_page.html", notes = notes_table, images = images_table)
|
||||
else:
|
||||
return abort(401)
|
||||
|
||||
@@ -76,6 +93,36 @@ def FUN_delete_note(note_id):
|
||||
delete_note_from_db(note_id)
|
||||
else:
|
||||
return abort(401)
|
||||
return(redirect(url_for("FUN_private")))
|
||||
|
||||
|
||||
# Reference: http://flask.pocoo.org/docs/0.12/patterns/fileuploads/
|
||||
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and \
|
||||
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
@app.route("/upload_image", methods = ['POST'])
|
||||
def FUN_upload_image():
|
||||
if request.method == 'POST':
|
||||
# check if the post request has the file part
|
||||
if 'file' not in request.files:
|
||||
flash('No file part')
|
||||
return(redirect(url_for("FUN_private")))
|
||||
file = request.files['file']
|
||||
# if user does not select file, browser also submit a empty part without filename
|
||||
if file.filename == '':
|
||||
flash('No selected file')
|
||||
return(redirect(url_for("FUN_private")))
|
||||
if file and allowed_file(file.filename):
|
||||
filename = secure_filename(file.filename)
|
||||
upload_time = str(datetime.datetime.now())
|
||||
image_uid = hashlib.sha1(upload_time + filename).hexdigest()
|
||||
# Save the image into UPLOAD_FOLDER
|
||||
file.save(os.path.join(app.config['UPLOAD_FOLDER'], image_uid + "-" + filename))
|
||||
# Record this uploading in database
|
||||
image_upload_record(image_uid, session['current_user'], filename, upload_time)
|
||||
return(redirect(url_for("FUN_private")))
|
||||
|
||||
return(redirect(url_for("FUN_private")))
|
||||
|
||||
@@ -83,6 +130,7 @@ def FUN_delete_note(note_id):
|
||||
|
||||
|
||||
|
||||
|
||||
@app.route("/login", methods = ["POST"])
|
||||
def FUN_login():
|
||||
id_submitted = request.form.get("id").upper()
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
SECRET_KEY = "fdsafasd"
|
||||
UPLOAD_FOLDER = "image_pool"
|
||||
MAX_CONTENT_LENGTH = 16 * 1024 * 1024
|
||||
32
database.py
32
database.py
@@ -4,6 +4,7 @@ import datetime
|
||||
|
||||
user_db_file_location = "database_file/users.db"
|
||||
note_db_file_location = "database_file/notes.db"
|
||||
image_db_file_location = "database_file/images.db"
|
||||
|
||||
def list_users():
|
||||
_conn = sqlite3.connect(user_db_file_location)
|
||||
@@ -41,6 +42,13 @@ def delete_user_from_db(id):
|
||||
_conn.commit()
|
||||
_conn.close()
|
||||
|
||||
# when we delete a user from database USERS, we also need to delete all his or her images data from database IMAGES
|
||||
_conn = sqlite3.connect(image_db_file_location)
|
||||
_c = _conn.cursor()
|
||||
_c.execute("delete from images where owner = '" + id + "';")
|
||||
_conn.commit()
|
||||
_conn.close()
|
||||
|
||||
def add_user(id, pw):
|
||||
_conn = sqlite3.connect(user_db_file_location)
|
||||
_c = _conn.cursor()
|
||||
@@ -101,6 +109,30 @@ def delete_note_from_db(note_id):
|
||||
_conn.commit()
|
||||
_conn.close()
|
||||
|
||||
def image_upload_record(uid, owner, image_name, timestamp):
|
||||
_conn = sqlite3.connect(image_db_file_location)
|
||||
_c = _conn.cursor()
|
||||
|
||||
command = "insert into images values ('{0}', '{1}', '{2}', '{3}');".format(uid, owner, image_name, timestamp)
|
||||
_c.execute(command)
|
||||
|
||||
_conn.commit()
|
||||
_conn.close()
|
||||
|
||||
def list_images_for_user(owner):
|
||||
_conn = sqlite3.connect(image_db_file_location)
|
||||
_c = _conn.cursor()
|
||||
|
||||
command = "select uid, timestamp, name from images where owner = '{0}'".format(owner)
|
||||
_c.execute(command)
|
||||
result = _c.fetchall()
|
||||
|
||||
_conn.commit()
|
||||
_conn.close()
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
BIN
database_file/images.db
Normal file
BIN
database_file/images.db
Normal file
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 2.1 KiB |
BIN
image_pool/e7c589f2627e1dac3fb9d57ad9ccd6066e67ce31-flask.png
Normal file
BIN
image_pool/e7c589f2627e1dac3fb9d57ad9ccd6066e67ce31-flask.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 208 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -13,6 +13,7 @@
|
||||
<li>Integrating with Bootstrap</li>
|
||||
<li>Interaction with Database (SQLite)</li>
|
||||
<li>Invoking static resources</li>
|
||||
<li>Upload files</li>
|
||||
</ul>
|
||||
|
||||
<p>For more basic knowledge of Flask, you can refer to <a href="https://www.tutorialspoint.com/flask/">a tutorial on Tutorialspoint</a>.</p>
|
||||
|
||||
6
templates/page_413.html
Normal file
6
templates/page_413.html
Normal file
@@ -0,0 +1,6 @@
|
||||
{% extends "layout.html" %}
|
||||
{% block page_title %}Request if too big(413){% endblock %}
|
||||
{% block body %}
|
||||
{{ super() }}
|
||||
Please check the file size you're uploading.
|
||||
{% endblock %}
|
||||
@@ -43,4 +43,32 @@
|
||||
</table>
|
||||
{% endif %}
|
||||
|
||||
<hr>
|
||||
<h3>Upload Image</h3>
|
||||
<form method='post' action='/upload_image' enctype=multipart/form-data>
|
||||
<p><input type=file name=file>
|
||||
<input type=submit value=Upload>
|
||||
</form>
|
||||
|
||||
{% if images %}
|
||||
<h3>Your Images</h3>
|
||||
<table class="table small">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Image ID</th>
|
||||
<th>Timestamp</th>
|
||||
<th>Image Name</th>
|
||||
</tr>
|
||||
</thead>
|
||||
{% for image_id, timestamp, image_name in images %}
|
||||
<tr>
|
||||
<td> {{ image_id }} </td>
|
||||
<td> {{ timestamp }} </td>
|
||||
<td> {{ image_name }} </td>
|
||||
</tr>
|
||||
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user