From 92936384b42448871eedce2b7ef8a2d2afb18e01 Mon Sep 17 00:00:00 2001 From: Dan Muckerman Date: Sun, 17 Jan 2021 13:12:30 -0500 Subject: [PATCH] Add duplicate checking --- app.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 8075d07..5c52be1 100644 --- a/app.py +++ b/app.py @@ -172,11 +172,14 @@ def image(): c = conn.cursor() filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) + c.execute("SELECT * FROM images WHERE filename=? AND user_id=?", (filename, current_user.get_user_dict()['dn'])) + row = c.fetchone() - c.execute("INSERT INTO images (filename, user_id) VALUES (?, ?)", (filename, current_user.get_user_dict()['dn'])) + if row == None: + c.execute("INSERT INTO images (filename, user_id) VALUES (?, ?)", (filename, current_user.get_user_dict()['dn'])) - c.execute("SELECT * FROM images WHERE filename=?", (filename,)) - row = c.fetchone() + c.execute("SELECT * FROM images WHERE filename=? AND user_id=?", (filename, current_user.get_user_dict()['dn'])) + row = c.fetchone() print(row[0]) conn.commit() conn.close() @@ -237,10 +240,13 @@ def shorten_url(): c = conn.cursor() if url is not None and len(url) > 0: - c.execute("INSERT INTO links (url, user_id) VALUES (?, ?)", (url, current_user.get_user_dict()['dn'])) - - c.execute("SELECT * FROM links WHERE url=?", (url,)) + c.execute("SELECT * FROM links WHERE url=? AND user_id=?", (url, current_user.get_user_dict()['dn'])) row = c.fetchone() + + if row == None: + c.execute("INSERT INTO links (url, user_id) VALUES (?, ?)", (url, current_user.get_user_dict()['dn'])) + c.execute("SELECT * FROM links WHERE url=? AND user_id=?", (url, current_user.get_user_dict()['dn'])) + row = c.fetchone() print(row[0]) conn.commit() conn.close() @@ -260,10 +266,14 @@ def save_paste(): c = conn.cursor() if paste is not None and len(paste) > 0: - c.execute("INSERT INTO pastes (paste, user_id) VALUES (?, ?)", (paste, current_user.get_user_dict()['dn'])) - - c.execute("SELECT * FROM pastes WHERE paste=?", (paste,)) + c.execute("SELECT * FROM pastes WHERE paste=? AND user_id=?", (paste, current_user.get_user_dict()['dn'])) row = c.fetchone() + + if row == None: + c.execute("INSERT INTO pastes (paste, user_id) VALUES (?, ?)", (paste, current_user.get_user_dict()['dn'])) + + c.execute("SELECT * FROM pastes WHERE paste=? AND user_id=?", (paste, current_user.get_user_dict()['dn'])) + row = c.fetchone() print(row[0]) conn.commit() conn.close()