diff --git a/freepost/__init__.py b/freepost/__init__.py index 76da281..df1beec 100755 --- a/freepost/__init__.py +++ b/freepost/__init__.py @@ -658,12 +658,22 @@ def submit_check (): # Retrieve link link = request.forms.getunicode ('link').strip () - # 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.') + if len (link) > 0: + # If there is a URL, make sure it has a "scheme" + if urlparse (link).scheme == '': + link = 'http://' + link + + # Check if this link was already posted in order to avoid duplicate posting. + previous_posts = database.link_exists(link) + if previous_posts: + posts_list = ''.join([ + '
  • {title}
  • '.format( + link = application.get_url ('post', hash_id=post['hashId']), + title = post['title']) + for post in previous_posts ]) + + return template ('submit.html', + flash='This link was already submitted:'.format(posts=posts_list)) # Retrieve topics topics = request.forms.getunicode ('topics') diff --git a/freepost/database.py b/freepost/database.py index be03477..83110a7 100644 --- a/freepost/database.py +++ b/freepost/database.py @@ -97,24 +97,26 @@ def username_exists (username, case_sensitive = True): return cursor.fetchone() is not None -# Check if post with same link exists -def post_exists (link): +# Check if post with same link exists. This is used to check for duplicates. +# Returns an empty list if the link wasn't posted before, otherwise returns the posts. +def link_exists (link): if not link: - return None + return [] with db: cursor = db.execute( """ - SELECT * - FROM post - WHERE LOWER(link) = LOWER(:link) + SELECT * + FROM post + WHERE LOWER(link) = LOWER(:link) + ORDER BY created DESC """, { 'link': link } ) - - return cursor.fetchone() is not None + + return cursor.fetchall() # Create new user account def new_user (username, password): diff --git a/freepost/templates/submit.html b/freepost/templates/submit.html index 6b81327..0a3f365 100755 --- a/freepost/templates/submit.html +++ b/freepost/templates/submit.html @@ -8,7 +8,7 @@ {% if flash %}
    - {{ flash }} + {{ flash|safe }}
    {% endif %}