Commit 8f356fc3 by Usman Khalid Committed by vagrant

student.views.confirm_email_change: commit/rollback transaction after rendering…

student.views.confirm_email_change: commit/rollback transaction after rendering response instead of before
parent 6ddd0ae5
...@@ -1315,8 +1315,9 @@ def confirm_email_change(request, key): ...@@ -1315,8 +1315,9 @@ def confirm_email_change(request, key):
try: try:
pec = PendingEmailChange.objects.get(activation_key=key) pec = PendingEmailChange.objects.get(activation_key=key)
except PendingEmailChange.DoesNotExist: except PendingEmailChange.DoesNotExist:
response = render_to_response("invalid_email_key.html", {})
transaction.rollback() transaction.rollback()
return render_to_response("invalid_email_key.html", {}) return response
user = pec.user user = pec.user
address_context = { address_context = {
...@@ -1325,8 +1326,9 @@ def confirm_email_change(request, key): ...@@ -1325,8 +1326,9 @@ def confirm_email_change(request, key):
} }
if len(User.objects.filter(email=pec.new_email)) != 0: if len(User.objects.filter(email=pec.new_email)) != 0:
response = render_to_response("email_exists.html", {})
transaction.rollback() transaction.rollback()
return render_to_response("email_exists.html", {}) return response
subject = render_to_string('emails/email_change_subject.txt', address_context) subject = render_to_string('emails/email_change_subject.txt', address_context)
subject = ''.join(subject.splitlines()) subject = ''.join(subject.splitlines())
...@@ -1342,9 +1344,10 @@ def confirm_email_change(request, key): ...@@ -1342,9 +1344,10 @@ def confirm_email_change(request, key):
try: try:
user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL) user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
except Exception: except Exception:
transaction.rollback()
log.warning('Unable to send confirmation email to old address', exc_info=True) log.warning('Unable to send confirmation email to old address', exc_info=True)
return render_to_response("email_change_failed.html", {'email': user.email}) response = render_to_response("email_change_failed.html", {'email': user.email})
transaction.rollback()
return response
user.email = pec.new_email user.email = pec.new_email
user.save() user.save()
...@@ -1353,12 +1356,14 @@ def confirm_email_change(request, key): ...@@ -1353,12 +1356,14 @@ def confirm_email_change(request, key):
try: try:
user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL) user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
except Exception: except Exception:
transaction.rollback()
log.warning('Unable to send confirmation email to new address', exc_info=True) log.warning('Unable to send confirmation email to new address', exc_info=True)
return render_to_response("email_change_failed.html", {'email': pec.new_email}) response = render_to_response("email_change_failed.html", {'email': pec.new_email})
transaction.rollback()
return response
response = render_to_response("email_change_successful.html", address_context)
transaction.commit() transaction.commit()
return render_to_response("email_change_successful.html", address_context) return response
except Exception: except Exception:
# If we get an unexpected exception, be sure to rollback the transaction # If we get an unexpected exception, be sure to rollback the transaction
transaction.rollback() transaction.rollback()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment