Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
django-basetheme-bootstrap
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bryan BRANCOTTE
django-basetheme-bootstrap
Commits
9bac4454
Commit
9bac4454
authored
5 years ago
by
Bryan BRANCOTTE
Browse files
Options
Downloads
Patches
Plain Diff
Ask for email instead of username when login if BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL
parent
73ebb647
No related branches found
No related tags found
No related merge requests found
Pipeline
#14156
passed
5 years ago
Stage: test
Changes
3
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
basetheme_bootstrap/forms.py
+23
-17
23 additions, 17 deletions
basetheme_bootstrap/forms.py
basetheme_bootstrap/urls.py
+2
-1
2 additions, 1 deletion
basetheme_bootstrap/urls.py
setup.py
+1
-1
1 addition, 1 deletion
setup.py
with
26 additions
and
19 deletions
basetheme_bootstrap/forms.py
+
23
−
17
View file @
9bac4454
from
django.conf
import
settings
from
django.conf
import
settings
from
django.contrib.auth
import
get_user_model
from
django.contrib.auth
import
get_user_model
,
forms
as
auth_forms
from
django.contrib.auth.forms
import
UserCreationForm
,
UsernameField
,
UserChangeForm
from
django.db.models
import
Q
from
django.db.models
import
Q
from
django.forms
import
ModelForm
from
django.forms
import
ModelForm
from
django.urls
import
reverse
from
django.urls
import
reverse
...
@@ -8,24 +7,24 @@ from django.utils.safestring import mark_safe
...
@@ -8,24 +7,24 @@ from django.utils.safestring import mark_safe
from
django.utils.translation
import
ugettext_lazy
as
_
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
CleanUsernameAndSuggestReset
:
class
Meta
:
class
Meta
:
abstract
=
True
abstract
=
True
@property
def
is_username_is_email
(
self
):
try
:
return
settings
.
BASETHEME_BOOTSTRAP_USERNAME_IS_EMAIL
except
AttributeError
:
return
False
def
clean
(
self
):
def
clean
(
self
):
f
=
super
().
clean
()
f
=
super
().
clean
()
qs
=
get_user_model
().
objects
qs
=
get_user_model
().
objects
if
hasattr
(
self
,
'
instance
'
):
if
hasattr
(
self
,
'
instance
'
):
qs
=
qs
.
filter
(
~
Q
(
id
=
self
.
instance
.
id
))
qs
=
qs
.
filter
(
~
Q
(
id
=
self
.
instance
.
id
))
if
qs
.
filter
(
email
=
f
[
"
email
"
]).
exists
()
or
\
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
():
qs
.
filter
(
username
=
f
.
get
(
"
username
"
,
""
)).
exists
():
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
'
...
@@ -33,41 +32,41 @@ class CleanUsernameAndSuggestReset:
...
@@ -33,41 +32,41 @@ class CleanUsernameAndSuggestReset:
return
f
return
f
class
UserCreationFormWithMore
(
CleanUsernameAndSuggestReset
,
UserCreationForm
):
class
UserCreationFormWithMore
(
CleanUsernameAndSuggestReset
,
auth_forms
.
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
"
)
field_classes
=
{
'
username
'
:
UsernameField
}
field_classes
=
{
'
username
'
:
auth_forms
.
UsernameField
}
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
:
if
is_username_is_email
()
:
del
self
.
fields
[
'
username
'
]
del
self
.
fields
[
'
username
'
]
def
save
(
self
,
commit
=
True
):
def
save
(
self
,
commit
=
True
):
user
=
super
().
save
(
commit
=
False
)
user
=
super
().
save
(
commit
=
False
)
if
self
.
is_username_is_email
:
if
is_username_is_email
()
:
user
.
username
=
user
.
email
user
.
username
=
user
.
email
if
commit
:
if
commit
:
user
.
save
()
user
.
save
()
return
user
return
user
class
MyUserChangeForm
(
CleanUsernameAndSuggestReset
,
UserChangeForm
):
class
MyUserChangeForm
(
CleanUsernameAndSuggestReset
,
auth_forms
.
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
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
super
().
__init__
(
*
args
,
**
kwargs
)
if
self
.
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
})
def
save
(
self
,
commit
=
True
):
def
save
(
self
,
commit
=
True
):
user
=
super
().
save
(
commit
=
False
)
user
=
super
().
save
(
commit
=
False
)
if
self
.
is_username_is_email
:
if
is_username_is_email
()
:
user
.
username
=
user
.
email
user
.
username
=
user
.
email
if
commit
:
if
commit
:
user
.
save
()
user
.
save
()
...
@@ -78,3 +77,10 @@ class UserDeleteForm(ModelForm):
...
@@ -78,3 +77,10 @@ class UserDeleteForm(ModelForm):
class
Meta
:
class
Meta
:
model
=
get_user_model
()
model
=
get_user_model
()
fields
=
()
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
"
)
This diff is collapsed.
Click to expand it.
basetheme_bootstrap/urls.py
+
2
−
1
View file @
9bac4454
...
@@ -4,6 +4,7 @@ from django.urls import reverse_lazy
...
@@ -4,6 +4,7 @@ from django.urls import reverse_lazy
from
django.views.generic
import
RedirectView
from
django.views.generic
import
RedirectView
from
basetheme_bootstrap
import
views
from
basetheme_bootstrap
import
views
from
basetheme_bootstrap.forms
import
AuthenticationForm
app_name
=
'
basetheme_bootstrap
'
app_name
=
'
basetheme_bootstrap
'
urlpatterns
=
[
urlpatterns
=
[
...
@@ -12,7 +13,7 @@ urlpatterns = [
...
@@ -12,7 +13,7 @@ urlpatterns = [
################################################################################
################################################################################
url
(
r
'
^accounts/$
'
,
views
.
account_detail
,
name
=
'
account
'
),
url
(
r
'
^accounts/$
'
,
views
.
account_detail
,
name
=
'
account
'
),
url
(
r
'
^accounts/profile/$
'
,
RedirectView
.
as_view
(
url
=
reverse_lazy
(
'
basetheme_bootstrap: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/logout/$
'
,
auth_views
.
LogoutView
.
as_view
(
next_page
=
'
/
'
),
name
=
'
logout
'
),
url
(
r
'
^accounts/password/$
'
,
views
.
change_password
,
name
=
'
change_password
'
),
url
(
r
'
^accounts/password/$
'
,
views
.
change_password
,
name
=
'
change_password
'
),
url
(
r
'
^accounts/signup/$
'
,
views
.
signup
,
name
=
'
signup
'
),
url
(
r
'
^accounts/signup/$
'
,
views
.
signup
,
name
=
'
signup
'
),
...
...
This diff is collapsed.
Click to expand it.
setup.py
+
1
−
1
View file @
9bac4454
...
@@ -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.
7
'
,
version
=
'
0.2.
8
'
,
description
=
'
Django Basetheme Bootstrap
'
,
description
=
'
Django Basetheme Bootstrap
'
,
long_description
=
readme
,
long_description
=
readme
,
author
=
'
Bryan Brancotte
'
,
author
=
'
Bryan Brancotte
'
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment