def get(self, request): # pylint: disable=unused-argument """Return a description of the password reset form. This decouples clients from the API definition: if the API decides to modify the form, clients won't need to be updated. See `user_api.helpers.FormDescription` for examples of the JSON-encoded form description. Returns: HttpResponse """ form_desc = FormDescription("post", reverse("password_change_request")) # Translators: This label appears above a field on the password reset # form meant to hold the user's email address. email_label = _(u"Email") # Translators: This example email address is used as a placeholder in # a field on the password reset form meant to hold the user's email address. email_placeholder = _(u"*****@*****.**") # Translators: These instructions appear on the password reset 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( "email", field_type="email", label=email_label, placeholder=email_placeholder, instructions=email_instructions, restrictions={ "min_length": account_api.EMAIL_MIN_LENGTH, "max_length": account_api.EMAIL_MAX_LENGTH, } ) return HttpResponse(form_desc.to_json(), content_type="application/json")
def get(self, request): # pylint: disable=unused-argument """Return a description of the password reset form. This decouples clients from the API definition: if the API decides to modify the form, clients won't need to be updated. See `user_api.helpers.FormDescription` for examples of the JSON-encoded form description. Returns: HttpResponse """ form_desc = FormDescription("post", reverse("password_change_request")) # Translators: This label appears above a field on the password reset # form meant to hold the user's email address. email_label = _(u"Email") # Translators: This example email address is used as a placeholder in # a field on the password reset form meant to hold the user's email address. email_placeholder = _(u"*****@*****.**") # Translators: These instructions appear on the password reset 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("email", field_type="email", label=email_label, placeholder=email_placeholder, instructions=email_instructions, restrictions={ "min_length": account_api.EMAIL_MIN_LENGTH, "max_length": account_api.EMAIL_MAX_LENGTH, }) return HttpResponse(form_desc.to_json(), content_type="application/json")
def test_to_json(self): desc = FormDescription("post", "/submit") desc.add_field( "name", label="label", field_type="text", default="default", placeholder="placeholder", instructions="instructions", required=True, restrictions={ "min_length": 2, "max_length": 10 }, error_messages={ "required": "You must provide a value!" } ) self.assertEqual(desc.to_json(), json.dumps({ "method": "post", "submit_url": "/submit", "fields": [ { "name": "name", "label": "label", "type": "text", "defaultValue": "default", "placeholder": "placeholder", "instructions": "instructions", "required": True, "restrictions": { "min_length": 2, "max_length": 10, }, "errorMessages": { "required": "You must provide a value!" } } ] }))
def get(self, request): # pylint: disable=unused-argument """Return a description of the login form. This decouples clients from the API definition: if the API decides to modify the form, clients won't need to be updated. See `user_api.helpers.FormDescription` for examples of the JSON-encoded form description. Returns: HttpResponse """ form_desc = FormDescription("post", reverse("user_api_login_session")) # Translators: This label appears above a field on the login form # meant to hold the user's email address. email_label = _(u"Email") # Translators: This example email address is used as a placeholder in # a field on the login form meant to hold the user's email address. email_placeholder = _(u"*****@*****.**") # Translators: These instructions appear on the login 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( "email", field_type="email", label=email_label, placeholder=email_placeholder, instructions=email_instructions, restrictions={"min_length": account_api.EMAIL_MIN_LENGTH, "max_length": account_api.EMAIL_MAX_LENGTH}, ) # Translators: This label appears above a field on the login form # meant to hold the user's password. password_label = _(u"Password") form_desc.add_field( "password", label=password_label, field_type="password", restrictions={"min_length": account_api.PASSWORD_MIN_LENGTH, "max_length": account_api.PASSWORD_MAX_LENGTH}, ) # Translators: This phrase appears next to a checkbox on the login form # which the user can check in order to remain logged in after their # session ends. remember_label = _(u"Remember me") form_desc.add_field("remember", field_type="checkbox", label=remember_label, default=False, required=False) return HttpResponse(form_desc.to_json(), content_type="application/json")
def get(self, request): # pylint: disable=unused-argument """Return a description of the login form. This decouples clients from the API definition: if the API decides to modify the form, clients won't need to be updated. See `user_api.helpers.FormDescription` for examples of the JSON-encoded form description. Returns: HttpResponse """ form_desc = FormDescription("post", reverse("user_api_login_session")) # Translators: This label appears above a field on the login form # meant to hold the user's email address. email_label = _(u"Email") # Translators: This example email address is used as a placeholder in # a field on the login form meant to hold the user's email address. email_placeholder = _(u"*****@*****.**") # Translators: These instructions appear on the login 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("email", field_type="email", label=email_label, placeholder=email_placeholder, instructions=email_instructions, restrictions={ "min_length": account_api.EMAIL_MIN_LENGTH, "max_length": account_api.EMAIL_MAX_LENGTH, }) # Translators: This label appears above a field on the login form # meant to hold the user's password. password_label = _(u"Password") form_desc.add_field("password", label=password_label, field_type="password", restrictions={ "min_length": account_api.PASSWORD_MIN_LENGTH, "max_length": account_api.PASSWORD_MAX_LENGTH, }) # Translators: This phrase appears next to a checkbox on the login form # which the user can check in order to remain logged in after their # session ends. remember_label = _(u"Remember me") form_desc.add_field( "remember", field_type="checkbox", label=remember_label, default=False, required=False, ) return HttpResponse(form_desc.to_json(), content_type="application/json")
def test_invalid_restriction(self): desc = FormDescription("post", "/submit") with self.assertRaises(InvalidFieldError): desc.add_field("name", field_type="text", restrictions={"invalid": 0})
def test_missing_options(self): desc = FormDescription("post", "/submit") with self.assertRaises(InvalidFieldError): desc.add_field("name", field_type="select")
def test_invalid_field_type(self): desc = FormDescription("post", "/submit") with self.assertRaises(InvalidFieldError): desc.add_field("invalid", field_type="invalid")