Skip to content

Commit

Permalink
Add 'Image Upload' Feature
Browse files Browse the repository at this point in the history
Each user can upload image to the system. The images from different users where be separated.
  • Loading branch information
XD-DENG committed Jul 8, 2017
1 parent dad7935 commit 0e73da4
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 2 deletions.
50 changes: 49 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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




Expand All @@ -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)

Expand Down Expand Up @@ -76,13 +93,44 @@ 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")))






@app.route("/login", methods = ["POST"])
def FUN_login():
id_submitted = request.form.get("id").upper()
Expand Down
4 changes: 3 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
SECRET_KEY = "fdsafasd"
SECRET_KEY = "fdsafasd"
UPLOAD_FOLDER = "image_pool"
MAX_CONTENT_LENGTH = 16 * 1024 * 1024
32 changes: 32 additions & 0 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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





Expand Down
Binary file added database_file/images.db
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 6 additions & 0 deletions templates/page_413.html
Original file line number Diff line number Diff line change
@@ -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 %}
28 changes: 28 additions & 0 deletions templates/private_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,32 @@ <h3>Your Notes</h3>
</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 %}

0 comments on commit 0e73da4

Please sign in to comment.