From 3281c40e6cbb412db7bf224ce800d4b25efb13ae Mon Sep 17 00:00:00 2001 From: Nichlas Severinsen Date: Thu, 4 Jun 2020 21:53:33 +0200 Subject: [PATCH] Add naive link duplication prevention --- freepost/__init__.py | 3 +++ freepost/database.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/freepost/__init__.py b/freepost/__init__.py index 29c8dfb0..157c763c 100755 --- a/freepost/__init__.py +++ b/freepost/__init__.py @@ -661,6 +661,9 @@ def submit_check (): # If there is a URL, make sure it has a "scheme" if len (link) > 0 and urlparse (link).scheme == '': link = 'http://' + link + + if database.post_exists(link): + return template ('submit.html', flash='Link has already been submitted.') # Retrieve topics topics = request.forms.getunicode ('topics') diff --git a/freepost/database.py b/freepost/database.py index 1a883526..be034770 100644 --- a/freepost/database.py +++ b/freepost/database.py @@ -97,6 +97,25 @@ def username_exists (username, case_sensitive = True): return cursor.fetchone() is not None +# Check if post with same link exists +def post_exists (link): + if not link: + return None + + with db: + cursor = db.execute( + """ + SELECT * + FROM post + WHERE LOWER(link) = LOWER(:link) + """, + { + 'link': link + } + ) + + return cursor.fetchone() is not None + # Create new user account def new_user (username, password): # Create a hash_id for the new post