Commit 5d1143ad by Renzo Lucioni

Merge pull request #5858 from edx/renzo/resource-conflict-error-messages

Return more helpful error messages when a resource conflict occurs
parents 8a0507c9 edba397a
...@@ -832,9 +832,6 @@ class RegistrationViewTest(ApiTestCase): ...@@ -832,9 +832,6 @@ class RegistrationViewTest(ApiTestCase):
u"required": True, u"required": True,
u"label": u"Email", u"label": u"Email",
u"placeholder": u"username@domain.com", u"placeholder": u"username@domain.com",
u"instructions": u"The email address you used to register with {platform_name}".format(
platform_name=settings.PLATFORM_NAME
),
u"restrictions": { u"restrictions": {
"min_length": account_api.EMAIL_MIN_LENGTH, "min_length": account_api.EMAIL_MIN_LENGTH,
"max_length": account_api.EMAIL_MAX_LENGTH "max_length": account_api.EMAIL_MAX_LENGTH
...@@ -913,9 +910,6 @@ class RegistrationViewTest(ApiTestCase): ...@@ -913,9 +910,6 @@ class RegistrationViewTest(ApiTestCase):
u"required": True, u"required": True,
u"label": u"Email", u"label": u"Email",
u"placeholder": u"username@domain.com", u"placeholder": u"username@domain.com",
"instructions": "The email address you used to register with {platform_name}".format(
platform_name=settings.PLATFORM_NAME
),
u"restrictions": { u"restrictions": {
"min_length": account_api.EMAIL_MIN_LENGTH, "min_length": account_api.EMAIL_MIN_LENGTH,
"max_length": account_api.EMAIL_MAX_LENGTH "max_length": account_api.EMAIL_MAX_LENGTH
...@@ -1354,7 +1348,10 @@ class RegistrationViewTest(ApiTestCase): ...@@ -1354,7 +1348,10 @@ class RegistrationViewTest(ApiTestCase):
"honor_code": "true", "honor_code": "true",
}) })
self.assertEqual(response.status_code, 409) self.assertEqual(response.status_code, 409)
self.assertEqual(response.content, json.dumps(["email"])) self.assertEqual(
response.content,
"It looks like {} belongs to an existing account.".format(self.EMAIL)
)
def test_register_duplicate_username(self): def test_register_duplicate_username(self):
# Register the first user # Register the first user
...@@ -1376,7 +1373,10 @@ class RegistrationViewTest(ApiTestCase): ...@@ -1376,7 +1373,10 @@ class RegistrationViewTest(ApiTestCase):
"honor_code": "true", "honor_code": "true",
}) })
self.assertEqual(response.status_code, 409) self.assertEqual(response.status_code, 409)
self.assertEqual(response.content, json.dumps(["username"])) self.assertEqual(
response.content,
"It looks like {} belongs to an existing account.".format(self.USERNAME)
)
def test_register_duplicate_username_and_email(self): def test_register_duplicate_username_and_email(self):
# Register the first user # Register the first user
...@@ -1398,7 +1398,12 @@ class RegistrationViewTest(ApiTestCase): ...@@ -1398,7 +1398,12 @@ class RegistrationViewTest(ApiTestCase):
"honor_code": "true", "honor_code": "true",
}) })
self.assertEqual(response.status_code, 409) self.assertEqual(response.status_code, 409)
self.assertEqual(response.content, json.dumps(["email", "username"])) self.assertEqual(
response.content,
"It looks like {} and {} belong to an existing account.".format(
self.EMAIL, self.USERNAME
)
)
def _assert_reg_field(self, extra_fields_setting, expected_field): def _assert_reg_field(self, extra_fields_setting, expected_field):
"""Retrieve the registration form description from the server and """Retrieve the registration form description from the server and
......
...@@ -255,16 +255,36 @@ class RegistrationView(APIView): ...@@ -255,16 +255,36 @@ class RegistrationView(APIView):
HttpResponse: 302 if redirecting to another page. HttpResponse: 302 if redirecting to another page.
""" """
# Handle duplicate username/email email = request.POST.get('email')
conflicts = account_api.check_account_exists( username = request.POST.get('username')
username=request.POST.get('username'),
email=request.POST.get('email') # Handle duplicate email/username
) conflicts = account_api.check_account_exists(email=email, username=username)
if conflicts: if conflicts:
if all(conflict in conflicts for conflict in ['email', 'username']):
# Translators: This message is shown to users who attempt to create a new
# account using both an email address and a username associated with an
# existing account.
error_msg = _(
u"It looks like {email_address} and {username} belong to an existing account."
).format(email_address=email, username=username)
elif 'email' in conflicts:
# Translators: This message is shown to users who attempt to create a new
# account using an email address associated with an existing account.
error_msg = _(
u"It looks like {email_address} belongs to an existing account."
).format(email_address=email)
else:
# Translators: This message is shown to users who attempt to create a new
# account using a username associated with an existing account.
error_msg = _(
u"It looks like {username} belongs to an existing account."
).format(username=username)
return HttpResponse( return HttpResponse(
status=409, status=409,
content=json.dumps(conflicts), content=error_msg,
content_type="application/json" content_type="text/plain"
) )
# For the initial implementation, shim the existing login view # For the initial implementation, shim the existing login view
...@@ -281,18 +301,11 @@ class RegistrationView(APIView): ...@@ -281,18 +301,11 @@ class RegistrationView(APIView):
# a field on the registration form meant to hold the user's email address. # a field on the registration form meant to hold the user's email address.
email_placeholder = _(u"username@domain.com") email_placeholder = _(u"username@domain.com")
# Translators: These instructions appear on the registration form, immediately
# below a field meant to hold the user's email address.
email_instructions = _(
u"The email address you used to register with {platform_name}"
).format(platform_name=settings.PLATFORM_NAME)
form_desc.add_field( form_desc.add_field(
"email", "email",
field_type="email", field_type="email",
label=email_label, label=email_label,
placeholder=email_placeholder, placeholder=email_placeholder,
instructions=email_instructions,
restrictions={ restrictions={
"min_length": account_api.EMAIL_MIN_LENGTH, "min_length": account_api.EMAIL_MIN_LENGTH,
"max_length": account_api.EMAIL_MAX_LENGTH, "max_length": account_api.EMAIL_MAX_LENGTH,
......
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