From 79b4349a51a028ee585158b12a56a44bc88e8208 Mon Sep 17 00:00:00 2001 From: zPlus Date: Thu, 19 Jul 2018 08:45:09 +0200 Subject: [PATCH] IGNORE email update if value already exists. modified: freepost/__init__.py modified: freepost/database.py --- freepost/__init__.py | 2 +- freepost/database.py | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/freepost/__init__.py b/freepost/__init__.py index 08510ddc..24db1774 100755 --- a/freepost/__init__.py +++ b/freepost/__init__.py @@ -418,7 +418,7 @@ def update_user (): if about is None or email is None: redirect (application.get_url ('user')) - # TODO check unique value of emails + # Update user info in the database database.update_user (user['id'], about, email, False) redirect (application.get_url ('user')) diff --git a/freepost/database.py b/freepost/database.py index 09234336..c0ab7f9c 100644 --- a/freepost/database.py +++ b/freepost/database.py @@ -281,21 +281,35 @@ def get_user_replies (user_id): def update_user (user_id, about, email, email_notifications): cursor = db.cursor (MySQLdb.cursors.DictCursor) + # Update user info, but not email address cursor.execute ( """ UPDATE user SET about = %(about)s, - email = %(email)s, email_notifications = %(notifications)s WHERE id = %(user)s """, { 'about': about, - 'email': email, 'notifications': email_notifications, 'user': user_id } ) + + # Update email address + # IGNORE update if the email address is already specified. This is + # necessary to avoid an "duplicate key" exception when updating value. + cursor.execute ( + """ + UPDATE IGNORE user + SET email = %(email)s + WHERE id = %(user)s + """, + { + 'email': email, + 'user': user_id + } + ) # Set user replies as read def set_replies_as_read (user_id):