diff --git a/freepost/__init__.py b/freepost/__init__.py index 08510dd..24db177 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 0923433..c0ab7f9 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):