Skip to content

Commit

Permalink
Use name property instead full_name for user's extra details
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Aug 16, 2023
1 parent 8b4b4b7 commit 5a39ce4
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 53 deletions.
4 changes: 2 additions & 2 deletions hub/migrations/0013_alter_constance_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ def alter_constance_config(apps, schema_editor):
user_metadata = to_python_object(config.USER_METADATA_FIELDS)
name_set = False
for field in user_metadata:
if field['name'] == 'full_name':
if field['name'] == 'name':
name_set = True
break
if not name_set:
user_metadata.insert(0, {
'name': 'full_name',
'name': 'name',
'required': False,
})
user_metadata_json = LazyJSONSerializable(user_metadata)
Expand Down
22 changes: 12 additions & 10 deletions hub/tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ def test_welcome_message(self):
self.assertEqual(welcome_message, 'Global welcome message')
self.assertEqual(welcome_message_es, welcome_message)

# TODO validate whethere the tests below are necessary.
# Kinda redundant with kobo/apps/accounts/tests/test_forms.py::AccountFormsTestCase
@override_config(USER_METADATA_FIELDS=LazyJSONSerializable([
{
'name': 'full_name',
'name': 'name',
'required': False,
'label': {
'default': 'Full name',
Expand All @@ -40,23 +42,23 @@ def test_welcome_message(self):
def test_user_metadata_fields_with_custom_label(self):
# Languages exist - return related label
assert (
I18nUtils.get_metadata_field_label('full_name', 'user', 'fr')
I18nUtils.get_metadata_field_label('name', 'user', 'fr')
== 'Prénom et nom'
)
assert (
I18nUtils.get_metadata_field_label('full_name', 'user', 'es')
I18nUtils.get_metadata_field_label('name', 'user', 'es')
== 'Nombre y apellido'
)
# No matching languages - return default
assert (
I18nUtils.get_metadata_field_label('full_name', 'user', 'it')
I18nUtils.get_metadata_field_label('name', 'user', 'it')
== 'Full name'
)
assert I18nUtils.get_metadata_field_label('full_name', 'user') == 'Full name'
assert I18nUtils.get_metadata_field_label('name', 'user') == 'Full name'

@override_config(USER_METADATA_FIELDS=LazyJSONSerializable([
{
'name': 'full_name',
'name': 'name',
'required': False
}
]))
Expand All @@ -66,18 +68,18 @@ def test_user_metadata_fields_no_label_field(self):

with mock.patch.dict(
USER_METADATA_DEFAULT_LABELS,
{'full_name': mock_t('My Full name')},
{'name': mock_t('My Full name')},
) as mock_dict:
assert (
I18nUtils.get_metadata_field_label('full_name', 'user', 'fr')
I18nUtils.get_metadata_field_label('name', 'user', 'fr')
== MOCK_TRANSLATION_STRING
)
assert (
I18nUtils.get_metadata_field_label('full_name', 'user')
I18nUtils.get_metadata_field_label('name', 'user')
== MOCK_TRANSLATION_STRING
)
assert (
USER_METADATA_DEFAULT_LABELS['full_name']
USER_METADATA_DEFAULT_LABELS['name']
== MOCK_TRANSLATION_STRING
)
assert mock_t.call_args.args[0] == 'My Full name'
Expand Down
18 changes: 9 additions & 9 deletions kobo/apps/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# Only these fields can be controlled by constance.config.USER_METADATA_FIELDS
CONFIGURABLE_METADATA_FIELDS = (
'full_name',
'name',
'organization',
'gender',
'sector',
Expand All @@ -23,14 +23,14 @@
class LoginForm(BaseLoginForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["login"].widget.attrs["placeholder"] = ""
self.fields["password"].widget.attrs["placeholder"] = ""
self.label_suffix = ""
self.fields['login'].widget.attrs['placeholder'] = ''
self.fields['password'].widget.attrs['placeholder'] = ''
self.label_suffix = ''


class KoboSignupMixin(forms.Form):
full_name = forms.CharField(
label=USER_METADATA_DEFAULT_LABELS['full_name'],
name = forms.CharField(
label=USER_METADATA_DEFAULT_LABELS['name'],
required=False,
)
organization = forms.CharField(
Expand Down Expand Up @@ -120,7 +120,7 @@ class SocialSignupForm(KoboSignupMixin, BaseSocialSignupForm):
field_order = [
'username',
'email',
'full_name',
'name',
'gender',
'sector',
'country',
Expand All @@ -131,12 +131,12 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if settings.UNSAFE_SSO_REGISTRATION_EMAIL_DISABLE:
self.fields['email'].widget.attrs['readonly'] = True
self.label_suffix = ""
self.label_suffix = ''


class SignupForm(KoboSignupMixin, BaseSignupForm):
field_order = [
'full_name',
'name',
'organization',
'username',
'email',
Expand Down
53 changes: 27 additions & 26 deletions kobo/apps/accounts/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import json

import pytest
from constance.test import override_config
from django.test import TestCase, override_settings
from django.utils import translation
from model_bakery import baker
from pyquery import PyQuery

from kobo.apps.accounts.forms import SocialSignupForm
from kpi.utils.json import LazyJSONSerializable


class AccountFormsTestCase(TestCase):
Expand Down Expand Up @@ -40,36 +38,36 @@ def test_only_configurable_fields_can_be_removed(self):

def test_field_without_custom_label_can_be_required(self):
with override_config(
USER_METADATA_FIELDS=json.dumps(
[{'name': 'full_name', 'required': True}]
USER_METADATA_FIELDS=LazyJSONSerializable(
[{'name': 'name', 'required': True}]
)
):
form = SocialSignupForm(sociallogin=self.sociallogin)
assert form.fields['full_name'].required
assert form.fields['full_name'].label == 'Full name'
assert form.fields['name'].required
assert form.fields['name'].label == 'Full name'

def test_field_with_only_default_custom_label(self):
with override_config(
USER_METADATA_FIELDS=json.dumps(
USER_METADATA_FIELDS=LazyJSONSerializable(
[
{
'name': 'full_name',
'name': 'name',
'required': True,
'label': {'default': 'Secret Agent ID'},
}
]
)
):
form = SocialSignupForm(sociallogin=self.sociallogin)
assert form.fields['full_name'].required
assert form.fields['full_name'].label == 'Secret Agent ID'
assert form.fields['name'].required
assert form.fields['name'].label == 'Secret Agent ID'

def test_field_with_specific_and_default_custom_labels(self):
with override_config(
USER_METADATA_FIELDS=json.dumps(
USER_METADATA_FIELDS=LazyJSONSerializable(
[
{
'name': 'full_name',
'name': 'name',
'required': True,
'label': {
'default': 'Secret Agent ID',
Expand All @@ -81,20 +79,24 @@ def test_field_with_specific_and_default_custom_labels(self):
):
with translation.override('es'):
form = SocialSignupForm(sociallogin=self.sociallogin)
assert form.fields['full_name'].required
assert form.fields['full_name'].label == 'ID de agente secreto'
assert form.fields['name'].required
assert form.fields['name'].label == 'ID de agente secreto'
with translation.override('en'):
form = SocialSignupForm(sociallogin=self.sociallogin)
assert form.fields['full_name'].required
assert form.fields['full_name'].label == 'Secret Agent ID'
assert form.fields['name'].required
assert form.fields['name'].label == 'Secret Agent ID'
with translation.override('fr'):
form = SocialSignupForm(sociallogin=self.sociallogin)
assert form.fields['name'].required
assert form.fields['name'].label == 'Secret Agent ID'

def test_field_with_custom_label_without_default(self):
"""
The JSON schema should always require a default label, but the form
should render labels properly even if the default is missing
"""
with override_config(
USER_METADATA_FIELDS=json.dumps(
USER_METADATA_FIELDS=LazyJSONSerializable(
[
{
'name': 'organization',
Expand All @@ -111,10 +113,9 @@ def test_field_with_custom_label_without_default(self):
assert form.fields['organization'].required
assert form.fields['organization'].label == 'Organisation secrète'


def test_field_without_custom_label_can_be_optional(self):
with override_config(
USER_METADATA_FIELDS=json.dumps(
USER_METADATA_FIELDS=LazyJSONSerializable(
[
{
'name': 'organization',
Expand All @@ -128,7 +129,7 @@ def test_field_without_custom_label_can_be_optional(self):

def test_field_with_custom_label_can_be_optional(self):
with override_config(
USER_METADATA_FIELDS=json.dumps(
USER_METADATA_FIELDS=LazyJSONSerializable(
[
{
'name': 'organization',
Expand All @@ -147,16 +148,16 @@ def test_field_with_custom_label_can_be_optional(self):
assert form.fields['organization'].label == 'Organization'
with translation.override('fr'):
form = SocialSignupForm(sociallogin=self.sociallogin)
assert form.fields['organization'].required == False
assert form.fields['organization'].required is False
assert form.fields['organization'].label == 'Organisation'
with translation.override('es'):
form = SocialSignupForm(sociallogin=self.sociallogin)
assert form.fields['organization'].required == False
assert form.fields['organization'].required is False
assert form.fields['organization'].label == 'Organización'

def test_not_supported_translation(self):
with override_config(
USER_METADATA_FIELDS=json.dumps(
USER_METADATA_FIELDS=LazyJSONSerializable(
[
{
'name': 'organization',
Expand All @@ -171,9 +172,9 @@ def test_not_supported_translation(self):
):
with translation.override('es'):
form = SocialSignupForm(sociallogin=self.sociallogin)
assert form.fields['organization'].required == False
assert form.fields['organization'].required is False
assert form.fields['organization'].label == 'Organization'
with translation.override('ar'):
form = SocialSignupForm(sociallogin=self.sociallogin)
assert form.fields['organization'].required == False
assert form.fields['organization'].required is False
assert form.fields['organization'].label == 'Organization'
6 changes: 3 additions & 3 deletions kobo/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
),
'USER_METADATA_FIELDS': (
LazyJSONSerializable([
{'name': 'full_name', 'required': False},
{'name': 'name', 'required': False},
{'name': 'organization', 'required': False},
{'name': 'organization_website', 'required': False},
{'name': 'sector', 'required': False},
Expand All @@ -289,11 +289,11 @@
# The available fields are hard-coded in the front end
'Modify if the fields are required and labels for these metadata '
"fields for users. Possible fields are:\n"
" 'full_name', 'organization', 'organization_website', "
" 'name', 'organization', 'organization_website', "
"'sector', 'gender', 'bio', 'city', 'country', 'twitter', 'linkedin', "
"and 'instagram'.\n\n"
'To add another language, follow the example below.\n\n'
"{'name': 'full_name', 'required': False, 'label': "
"{'name': 'name', 'required': False, 'label': "
"{default: 'Full Name', 'fr': 'Nom Complet'}}\n",
# Use custom field for schema validation
'long_metadata_fields_jsonschema'
Expand Down
2 changes: 1 addition & 1 deletion kobo/static_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@
}

USER_METADATA_DEFAULT_LABELS = {
'full_name': t('Full name'),
'name': t('Full name'),
'organization': t('Organization'),
'organization_website': t('Organization website'),
'sector': t('Sector'),
Expand Down
4 changes: 2 additions & 2 deletions kpi/fields/jsonschema_form_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def clean(self, value):

if set(self.REQUIRED_FIELDS) - set(d['name'] for d in instance):
raise ValidationError(
t('`##place_holder##` cannot be removed.').replace(
t('`##place_holder##` field cannot be hidden.').replace(
'##place_holder##',
'`, `'.join(self.REQUIRED_FIELDS)
)
Expand All @@ -146,7 +146,7 @@ def clean(self, value):

class UserMetadataFieldsListField(MetadataFieldsListField):

REQUIRED_FIELDS = ['full_name']
REQUIRED_FIELDS = ['name']


class I18nTextJSONField(JsonSchemaFormField):
Expand Down

0 comments on commit 5a39ce4

Please sign in to comment.