def email_delete(request, email_pk): """Delete the given email. Must be owned by current user.""" email = get_object_or_404(EmailAddress, pk=int(email_pk)) if email.user == request.user: if not email.is_verified(): email.delete() else: num_verified_emails = len(request.user.emailaddress_set.filter( verified_at__isnull=False)) if num_verified_emails > 1: email.delete() elif num_verified_emails == 1: if MM.ALLOW_REMOVE_LAST_VERIFIED_EMAIL: email.delete() else: messages.error(request, MM.REMOVE_LAST_VERIFIED_EMAIL_ATTEMPT_MSG, extra_tags='alert-error') else: messages.error(request, 'Invalid request.') return redirect(MM.DELETE_EMAIL_REDIRECT)