feat: add optional-exposed extra field type to registration form
This defines optional extra fields that are not hidden under the toggle on the registration page.
This commit is contained in:
@@ -135,7 +135,7 @@ class FormDescription:
|
||||
|
||||
def add_field(
|
||||
self, name, label="", field_type="text", default="",
|
||||
placeholder="", instructions="", required=True, restrictions=None,
|
||||
placeholder="", instructions="", exposed=None, required=True, restrictions=None,
|
||||
options=None, include_default_option=False, error_messages=None,
|
||||
supplementalLink="", supplementalText=""
|
||||
):
|
||||
@@ -159,6 +159,9 @@ class FormDescription:
|
||||
instructions (unicode): Short instructions for using the field
|
||||
(e.g. "This is the email address you used when you registered.")
|
||||
|
||||
exposed (boolean): Whether the field is shown if not required.
|
||||
If the field is not set, the field will be visible if it's required.
|
||||
|
||||
required (boolean): Whether the field is required or optional.
|
||||
|
||||
restrictions (dict): Validation restrictions for the field.
|
||||
@@ -195,6 +198,9 @@ class FormDescription:
|
||||
)
|
||||
raise InvalidFieldError(msg)
|
||||
|
||||
if exposed is None:
|
||||
exposed = required
|
||||
|
||||
field_dict = {
|
||||
"name": name,
|
||||
"label": label,
|
||||
@@ -202,6 +208,7 @@ class FormDescription:
|
||||
"defaultValue": default,
|
||||
"placeholder": placeholder,
|
||||
"instructions": instructions,
|
||||
"exposed": exposed,
|
||||
"required": required,
|
||||
"restrictions": {},
|
||||
"errorMessages": {},
|
||||
@@ -268,6 +275,7 @@ class FormDescription:
|
||||
"label": "Cheese or Wine?",
|
||||
"defaultValue": "cheese",
|
||||
"type": "select",
|
||||
"exposed": True,
|
||||
"required": True,
|
||||
"placeholder": "",
|
||||
"instructions": "",
|
||||
@@ -283,6 +291,7 @@ class FormDescription:
|
||||
"label": "comments",
|
||||
"defaultValue": "",
|
||||
"type": "text",
|
||||
"exposed": False,
|
||||
"required": False,
|
||||
"placeholder": "Any comments?",
|
||||
"instructions": "Please enter additional comments here."
|
||||
|
||||
@@ -95,6 +95,7 @@ class FormDescriptionTest(TestCase):
|
||||
placeholder="placeholder",
|
||||
instructions="instructions",
|
||||
required=True,
|
||||
exposed=True,
|
||||
restrictions={
|
||||
"min_length": 2,
|
||||
"max_length": 10
|
||||
@@ -110,8 +111,8 @@ class FormDescriptionTest(TestCase):
|
||||
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},
|
||||
'placeholder': 'placeholder', 'instructions': 'instructions', 'exposed': True,
|
||||
'required': True, 'restrictions': {'min_length': 2, 'max_length': 10},
|
||||
'errorMessages': {'required': 'You must provide a value!'},
|
||||
'supplementalLink': '', 'supplementalText': '',
|
||||
'loginIssueSupportLink': 'https://support.example.com/login-issue-help.html'}]})
|
||||
|
||||
@@ -328,12 +328,16 @@ class RegistrationFormFactory:
|
||||
|
||||
def _is_field_visible(self, field_name):
|
||||
"""Check whether a field is visible based on Django settings. """
|
||||
return self._extra_fields_setting.get(field_name) in ["required", "optional"]
|
||||
return self._extra_fields_setting.get(field_name) in ["required", "optional", "optional-exposed"]
|
||||
|
||||
def _is_field_required(self, field_name):
|
||||
"""Check whether a field is required based on Django settings. """
|
||||
return self._extra_fields_setting.get(field_name) == "required"
|
||||
|
||||
def _is_field_exposed(self, field_name):
|
||||
"""Check whether a field is optional and should be toggled. """
|
||||
return self._extra_fields_setting.get(field_name) in ["required", "optional-exposed"]
|
||||
|
||||
def __init__(self):
|
||||
|
||||
if settings.ENABLE_COPPA_COMPLIANCE and 'year_of_birth' in self.EXTRA_FIELDS:
|
||||
@@ -441,6 +445,7 @@ class RegistrationFormFactory:
|
||||
FormDescription.FIELD_TYPE_MAP.get(field.__class__)),
|
||||
placeholder=field.initial,
|
||||
instructions=field.help_text,
|
||||
exposed=self._is_field_exposed(field_name),
|
||||
required=(self._is_field_required(field_name) or field.required),
|
||||
restrictions=restrictions,
|
||||
options=getattr(field, 'choices', None), error_messages=field.error_messages,
|
||||
|
||||
@@ -1020,8 +1020,8 @@ class LoginSessionViewTest(ApiTestCase, OpenEdxEventsTestMixin):
|
||||
form_desc = json.loads(response.content.decode('utf-8'))
|
||||
assert form_desc['method'] == 'post'
|
||||
assert form_desc['submit_url'] == reverse('user_api_login_session', kwargs={'api_version': 'v1'})
|
||||
assert form_desc['fields'] == [{'name': 'email', 'defaultValue': '', 'type': 'email', 'required': True,
|
||||
'label': 'Email', 'placeholder': '',
|
||||
assert form_desc['fields'] == [{'name': 'email', 'defaultValue': '', 'type': 'email', 'exposed': True,
|
||||
'required': True, 'label': 'Email', 'placeholder': '',
|
||||
'instructions': 'The email address you used to register with {platform_name}'
|
||||
.format(platform_name=settings.PLATFORM_NAME),
|
||||
'restrictions': {'min_length': EMAIL_MIN_LENGTH,
|
||||
@@ -1033,6 +1033,7 @@ class LoginSessionViewTest(ApiTestCase, OpenEdxEventsTestMixin):
|
||||
{'name': 'password',
|
||||
'defaultValue': '',
|
||||
'type': 'password',
|
||||
'exposed': True,
|
||||
'required': True,
|
||||
'label': 'Password',
|
||||
'placeholder': '',
|
||||
|
||||
@@ -710,8 +710,8 @@ class PasswordResetViewTest(UserAPITestCase):
|
||||
assert form_desc['method'] == 'post'
|
||||
assert form_desc['submit_url'] == reverse('password_change_request')
|
||||
assert form_desc['fields'] ==\
|
||||
[{'name': 'email', 'defaultValue': '', 'type': 'email', 'required': True,
|
||||
'label': 'Email', 'placeholder': 'username@domain.com',
|
||||
[{'name': 'email', 'defaultValue': '', 'type': 'email', 'exposed': True,
|
||||
'required': True, 'label': 'Email', 'placeholder': 'username@domain.com',
|
||||
'instructions': 'The email address you used to register with {platform_name}'
|
||||
.format(platform_name=settings.PLATFORM_NAME),
|
||||
'restrictions': {'min_length': EMAIL_MIN_LENGTH,
|
||||
|
||||
Reference in New Issue
Block a user