Commit fe070ddf authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

Allow to set first and last name required

parent 9bac4454
Pipeline #14158 passed with stage
in 28 seconds
...@@ -54,6 +54,7 @@ BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_ENABLED = True ...@@ -54,6 +54,7 @@ BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_ENABLED = True
BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_LOCATION_APP = "test_app_1" BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_LOCATION_APP = "test_app_1"
BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_NAME = "MyUserPreferences" BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_NAME = "MyUserPreferences"
BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL = False BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL = False
BASETHEME_BOOTSTRAP_FIRST_LAST_NAME_REQUIRED = False
################################################################################ ################################################################################
``` ```
......
...@@ -50,6 +50,8 @@ Quick start ...@@ -50,6 +50,8 @@ Quick start
BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_ENABLED = True BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_ENABLED = True
BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_LOCATION_APP = "test_app_1" BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_LOCATION_APP = "test_app_1"
BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_NAME = "MyUserPreferences" BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_NAME = "MyUserPreferences"
BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL = False
BASETHEME_BOOTSTRAP_FIRST_LAST_NAME_REQUIRED = False
################################################################################ ################################################################################
......
...@@ -13,6 +13,12 @@ def is_username_is_email(): ...@@ -13,6 +13,12 @@ def is_username_is_email():
except AttributeError: except AttributeError:
return False return False
def is_first_last_name_required():
try:
return settings.BASETHEME_BOOTSTRAP_FIRST_LAST_NAME_REQUIRED
except AttributeError:
return False
class CleanUsernameAndSuggestReset: class CleanUsernameAndSuggestReset:
class Meta: class Meta:
...@@ -29,6 +35,11 @@ class CleanUsernameAndSuggestReset: ...@@ -29,6 +35,11 @@ class CleanUsernameAndSuggestReset:
self.add_error("email", mark_safe(_( self.add_error("email", mark_safe(_(
'The email already exists, if you have lost your password you can reset it ' 'The email already exists, if you have lost your password you can reset it '
'<a href="%s">here</a>.') % (reverse('basetheme_bootstrap:password_reset')))) '<a href="%s">here</a>.') % (reverse('basetheme_bootstrap:password_reset'))))
if is_first_last_name_required():
if len(f.get("first_name", ""))==0:
self.add_error("email", _("First name is required"))
if len(f.get("last_name", ""))==0:
self.add_error("email", _("Last name is required"))
return f return f
...@@ -43,6 +54,9 @@ class UserCreationFormWithMore(CleanUsernameAndSuggestReset, auth_forms.UserCrea ...@@ -43,6 +54,9 @@ class UserCreationFormWithMore(CleanUsernameAndSuggestReset, auth_forms.UserCrea
self.fields['email'].widget.attrs.update({'required': True}) self.fields['email'].widget.attrs.update({'required': True})
if is_username_is_email(): if is_username_is_email():
del self.fields['username'] del self.fields['username']
if is_first_last_name_required():
self.fields['first_name'].widget.attrs.update({'required': True})
self.fields['last_name'].widget.attrs.update({'required': True})
def save(self, commit=True): def save(self, commit=True):
user = super().save(commit=False) user = super().save(commit=False)
...@@ -63,6 +77,9 @@ class MyUserChangeForm(CleanUsernameAndSuggestReset, auth_forms.UserChangeForm): ...@@ -63,6 +77,9 @@ class MyUserChangeForm(CleanUsernameAndSuggestReset, auth_forms.UserChangeForm):
if is_username_is_email(): if is_username_is_email():
del self.fields['username'] del self.fields['username']
self.fields['email'].widget.attrs.update({'required': True}) self.fields['email'].widget.attrs.update({'required': True})
if is_first_last_name_required():
self.fields['first_name'].widget.attrs.update({'required': True})
self.fields['last_name'].widget.attrs.update({'required': True})
def save(self, commit=True): def save(self, commit=True):
user = super().save(commit=False) user = super().save(commit=False)
......
...@@ -76,7 +76,7 @@ class SignUpWithoutUsernameTests(TestCase): ...@@ -76,7 +76,7 @@ class SignUpWithoutUsernameTests(TestCase):
self.assertEqual(user.email, data["email"]) self.assertEqual(user.email, data["email"])
def test_sign_up_detecte_duplicate_email(self): def test_sign_up_detecte_duplicate_email(self):
get_user_model().objects.create(username="toto",email="userAAA@mp.com") get_user_model().objects.create(username="toto", email="userAAA@mp.com")
user_count = get_user_model().objects.count() user_count = get_user_model().objects.count()
data = { data = {
'email': "userAAA@mp.com", 'email': "userAAA@mp.com",
...@@ -88,6 +88,56 @@ class SignUpWithoutUsernameTests(TestCase): ...@@ -88,6 +88,56 @@ class SignUpWithoutUsernameTests(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@override_settings(
BASETHEME_BOOTSTRAP_FIRST_LAST_NAME_REQUIRED=True,
)
class SignUpWithFirstLastNameRequiredTests(TestCase):
def test_sign_up_form_view(self):
user_count = get_user_model().objects.count()
data = {
'username': "userAAA@mp.com",
'email': "userAAA@mp.com",
'password1': "user@mp.comuser@mp.comuser@mp.comuser@mp.com",
'password2': "user@mp.comuser@mp.comuser@mp.comuser@mp.com",
'first_name': "my_first_name",
'last_name': "my_last_name",
}
response = self.client.post(reverse('basetheme_bootstrap:signup'), data)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, expected_url=reverse('home'), )
self.assertEqual(get_user_model().objects.count(), user_count + 1)
user = get_user_model().objects.last()
self.assertEqual(user.username, data["email"])
self.assertEqual(user.email, data["email"])
self.assertEqual(user.first_name, data["first_name"])
self.assertEqual(user.last_name, data["last_name"])
def test_sign_up_detect_missing_last_name(self):
data = {
'username': "userAAA@mp.com",
'email': "userAAA@mp.com",
'password1': "user@mp.comuser@mp.comuser@mp.comuser@mp.com",
'password2': "user@mp.comuser@mp.comuser@mp.comuser@mp.com",
'first_name': "my_first_name",
'last_name': "",
}
response = self.client.post(reverse('basetheme_bootstrap:signup'), data)
self.assertEqual(response.status_code, 200)
def test_sign_up_detect_missing_first_name(self):
data = {
'username': "userAAA@mp.com",
'email': "userAAA@mp.com",
'password1': "user@mp.comuser@mp.comuser@mp.comuser@mp.com",
'password2': "user@mp.comuser@mp.comuser@mp.comuser@mp.com",
'first_name': "",
'last_name': "my_last_name",
}
response = self.client.post(reverse('basetheme_bootstrap:signup'), data)
self.assertEqual(response.status_code, 200)
class TestWithTemplatesInPlace(SignUpTests): class TestWithTemplatesInPlace(SignUpTests):
def setUp(self): def setUp(self):
......
...@@ -7,7 +7,7 @@ readme = open('README.rst').read() ...@@ -7,7 +7,7 @@ readme = open('README.rst').read()
setup( setup(
name='django-basetheme-bootstrap', name='django-basetheme-bootstrap',
version='0.2.8', version='0.2.9',
description='Django Basetheme Bootstrap', description='Django Basetheme Bootstrap',
long_description=readme, long_description=readme,
author='Bryan Brancotte', author='Bryan Brancotte',
......
...@@ -137,6 +137,7 @@ BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_ENABLED = True ...@@ -137,6 +137,7 @@ BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_ENABLED = True
BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_LOCATION_APP = "test_app_1" BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_LOCATION_APP = "test_app_1"
BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_NAME = "MyUserPreferences" BASETHEME_BOOTSTRAP_USER_PREFERENCE_MODEL_NAME = "MyUserPreferences"
# BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL = True # default is False # BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL = True # default is False
# BASETHEME_BOOTSTRAP_FIRST_LAST_NAME_REQUIRED = True # default is False
################################################################################ ################################################################################
# Various debug stuff # Various debug stuff
......
Supports Markdown
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