Skip to content

Commit

Permalink
Use custom error class for email change failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjaclasher committed Oct 23, 2023
1 parent 08e3d9f commit 3ca70be
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions judge/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ def post(self, request, *args, **kwargs):


class EmailChangeActivateView(LoginRequiredMixin, View):
class EmailChangeFailedError(Exception):
pass

def get(self, request, *args, **kwargs):
activation_key = kwargs['activation_key']
signer = signing.TimestampSigner()
Expand All @@ -586,22 +589,24 @@ def get(self, request, *args, **kwargs):
max_age=settings.DMOJ_EMAIL_CHANGE_EXPIRY_MINUTES * 60,
)
except (binascii.Error, signing.BadSignature):
raise ValueError(_('Invalid activation key. Please try again.'))
raise self.EmailChangeFailedError(_('Invalid activation key. Please try again.'))
except signing.SignatureExpired:
raise ValueError(_('This request has expired. Please try again.'))
raise self.EmailChangeFailedError(_('This request has expired. Please try again.'))
if data['id'] != request.user.id:
raise ValueError(
raise self.EmailChangeFailedError(
_('Please try again from the account this email change was originally requested from.'),
)
with revisions.create_revision(atomic=True):
if User.objects.filter(email=data['email']).exists():
raise ValueError(_('The email originally requested is in use. Please try again with a new email.'))
raise EmailChangeFailedError(
_('The email originally requested is in use. Please try again with a new email.'),
)
request.user.email = data['email']
request.user.save()
revisions.set_user(request.user)
revisions.set_comment(_('Changed email address'))
except ValueError as ve:
return generic_message(request, _('Email change failed'), str(ve), status=403)
except self.EmailChangeFailedError as e:
return generic_message(request, _('Email change failed'), str(e), status=403)
else:
return generic_message(
request,
Expand Down

0 comments on commit 3ca70be

Please sign in to comment.