Commit 9bac4454 authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

Ask for email instead of username when login if BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL

parent 73ebb647
Pipeline #14156 passed with stage
in 24 seconds
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UsernameField, UserChangeForm
from django.contrib.auth import get_user_model, forms as auth_forms
from django.db.models import Q
from django.forms import ModelForm
from django.urls import reverse
......@@ -8,24 +7,24 @@ from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
def is_username_is_email():
try:
return settings.BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL
except AttributeError:
return False
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 \
not 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 '
......@@ -33,41 +32,41 @@ class CleanUsernameAndSuggestReset:
return f
class UserCreationFormWithMore(CleanUsernameAndSuggestReset, UserCreationForm):
class UserCreationFormWithMore(CleanUsernameAndSuggestReset, auth_forms.UserCreationForm):
class Meta:
model = get_user_model()
fields = ("username", "email", "first_name", "last_name")
field_classes = {'username': UsernameField}
field_classes = {'username': auth_forms.UsernameField}
def __init__(self, *args, **kwargs):
super(UserCreationFormWithMore, self).__init__(*args, **kwargs)
self.fields['email'].widget.attrs.update({'required': True})
if self.is_username_is_email:
if is_username_is_email():
del self.fields['username']
def save(self, commit=True):
user = super().save(commit=False)
if self.is_username_is_email:
if is_username_is_email():
user.username = user.email
if commit:
user.save()
return user
class MyUserChangeForm(CleanUsernameAndSuggestReset, UserChangeForm):
class MyUserChangeForm(CleanUsernameAndSuggestReset, auth_forms.UserChangeForm):
class Meta:
model = get_user_model()
fields = ("username", "email", "first_name", "last_name", "password")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.is_username_is_email:
if 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:
if is_username_is_email():
user.username = user.email
if commit:
user.save()
......@@ -78,3 +77,10 @@ class UserDeleteForm(ModelForm):
class Meta:
model = get_user_model()
fields = ()
class AuthenticationForm(auth_forms.AuthenticationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if is_username_is_email():
self.fields["username"].label = _("Email")
......@@ -4,6 +4,7 @@ from django.urls import reverse_lazy
from django.views.generic import RedirectView
from basetheme_bootstrap import views
from basetheme_bootstrap.forms import AuthenticationForm
app_name = 'basetheme_bootstrap'
urlpatterns = [
......@@ -12,7 +13,7 @@ urlpatterns = [
################################################################################
url(r'^accounts/$', views.account_detail, name='account'),
url(r'^accounts/profile/$', RedirectView.as_view(url=reverse_lazy('basetheme_bootstrap:account'))),
url(r'^accounts/login/$', auth_views.LoginView.as_view(), name='login'),
url(r'^accounts/login/$', auth_views.LoginView.as_view(form_class=AuthenticationForm), name='login'),
url(r'^accounts/logout/$', auth_views.LogoutView.as_view(next_page='/'), name='logout'),
url(r'^accounts/password/$', views.change_password, name='change_password'),
url(r'^accounts/signup/$', views.signup, name='signup'),
......
......@@ -7,7 +7,7 @@ readme = open('README.rst').read()
setup(
name='django-basetheme-bootstrap',
version='0.2.7',
version='0.2.8',
description='Django Basetheme Bootstrap',
long_description=readme,
author='Bryan Brancotte',
......
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