Commit 73ebb647 authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

Ability to have hidden and always equal to email

parent 1620012c
Pipeline #14154 passed with stage
in 25 seconds
...@@ -53,6 +53,7 @@ BASETHEME_BOOTSTRAP_TEMPLATE_LOCATION_PROJECT = "test_app_1" ...@@ -53,6 +53,7 @@ BASETHEME_BOOTSTRAP_TEMPLATE_LOCATION_PROJECT = "test_app_1"
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
################################################################################ ################################################################################
``` ```
......
import itertools from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UsernameField, UserChangeForm from django.contrib.auth.forms import UserCreationForm, UsernameField, UserChangeForm
from django.db.models.query_utils import Q from django.db.models import Q
from django.forms import ModelForm from django.forms import ModelForm
from django.utils.translation import ugettext as _ from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
class CleanUsernameAndSuggestReset:
class Meta:
abstract = True
@property
def is_username_is_email(self):
try:
return settings.BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL
except AttributeError:
return False
def clean(self):
f = super().clean()
qs = get_user_model().objects
if hasattr(self, 'instance'):
qs = qs.filter(~Q(id=self.instance.id))
if qs.filter(email=f["email"]).exists() or \
not self.is_username_is_email and \
qs.filter(username=f.get("username", "")).exists():
self.add_error("email", mark_safe(_(
'The email already exists, if you have lost your password you can reset it '
'<a href="%s">here</a>.') % (reverse('basetheme_bootstrap:password_reset'))))
return f
class UserCreationFormWithMore(UserCreationForm):
class UserCreationFormWithMore(CleanUsernameAndSuggestReset, UserCreationForm):
class Meta: class Meta:
model = get_user_model() model = get_user_model()
fields = ("username", "email", "first_name", "last_name") fields = ("username", "email", "first_name", "last_name")
...@@ -16,15 +42,39 @@ class UserCreationFormWithMore(UserCreationForm): ...@@ -16,15 +42,39 @@ class UserCreationFormWithMore(UserCreationForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(UserCreationFormWithMore, self).__init__(*args, **kwargs) super(UserCreationFormWithMore, self).__init__(*args, **kwargs)
self.fields['email'].widget.attrs.update({'required': True}) self.fields['email'].widget.attrs.update({'required': True})
if self.is_username_is_email:
del self.fields['username']
def save(self, commit=True):
user = super().save(commit=False)
if self.is_username_is_email:
user.username = user.email
if commit:
user.save()
return user
class MyUserChangeForm(UserChangeForm): class MyUserChangeForm(CleanUsernameAndSuggestReset, UserChangeForm):
class Meta: class Meta:
model = get_user_model() model = get_user_model()
fields = ("username", "email", "first_name", "last_name", "password") fields = ("username", "email", "first_name", "last_name", "password")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.is_username_is_email:
del self.fields['username']
self.fields['email'].widget.attrs.update({'required': True})
def save(self, commit=True):
user = super().save(commit=False)
if self.is_username_is_email:
user.username = user.email
if commit:
user.save()
return user
class UserDeleteForm(ModelForm): class UserDeleteForm(ModelForm):
class Meta: class Meta:
model = get_user_model() model = get_user_model()
fields = () fields = ()
\ No newline at end of file
...@@ -38,16 +38,54 @@ class SignUpTests(TestCase): ...@@ -38,16 +38,54 @@ class SignUpTests(TestCase):
def test_sign_up_form_view(self): def test_sign_up_form_view(self):
user_count = get_user_model().objects.count() user_count = get_user_model().objects.count()
response = self.client.post(reverse('basetheme_bootstrap:signup'), { data = {
'username': "userAAA", 'username': "userAAA",
'email': "userAAA@mp.com", 'email': "userAAA@mp.com",
'password1': "user@mp.comuser@mp.comuser@mp.comuser@mp.com", 'password1': "user@mp.comuser@mp.comuser@mp.comuser@mp.com",
'password2': "user@mp.comuser@mp.comuser@mp.comuser@mp.com", 'password2': "user@mp.comuser@mp.comuser@mp.comuser@mp.com",
'first_name': "user" 'first_name': "user"
}) }
response = self.client.post(reverse('basetheme_bootstrap:signup'), data)
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertRedirects(response, expected_url=reverse('home'), ) self.assertRedirects(response, expected_url=reverse('home'), )
self.assertEqual(get_user_model().objects.count(), user_count + 1) self.assertEqual(get_user_model().objects.count(), user_count + 1)
user = get_user_model().objects.last()
self.assertEqual(user.username, data["username"])
self.assertEqual(user.email, data["email"])
@override_settings(
BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL=True,
)
class SignUpWithoutUsernameTests(TestCase):
def test_sign_up_form_view(self):
user_count = get_user_model().objects.count()
data = {
'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': "user"
}
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"])
def test_sign_up_detecte_duplicate_email(self):
get_user_model().objects.create(username="toto",email="userAAA@mp.com")
user_count = get_user_model().objects.count()
data = {
'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': "user"
}
response = self.client.post(reverse('basetheme_bootstrap:signup'), data)
self.assertEqual(response.status_code, 200)
class TestWithTemplatesInPlace(SignUpTests): class TestWithTemplatesInPlace(SignUpTests):
......
...@@ -57,7 +57,7 @@ def signup(request): ...@@ -57,7 +57,7 @@ def signup(request):
user.is_superuser = True user.is_superuser = True
user.is_staff = True user.is_staff = True
user.save() user.save()
username = form.cleaned_data.get('username') username = user.username
raw_password = form.cleaned_data.get('password1') raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password) user = authenticate(username=username, password=raw_password)
request.user = user request.user = user
......
...@@ -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.6', version='0.2.7',
description='Django Basetheme Bootstrap', description='Django Basetheme Bootstrap',
long_description=readme, long_description=readme,
author='Bryan Brancotte', author='Bryan Brancotte',
......
...@@ -136,6 +136,7 @@ BASETHEME_BOOTSTRAP_TEMPLATE_LOCATION_PROJECT = "test_app_1" ...@@ -136,6 +136,7 @@ BASETHEME_BOOTSTRAP_TEMPLATE_LOCATION_PROJECT = "test_app_1"
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 = 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