mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2025-08-15 09:24:52 +03:00
Add conditions for fields in UserAdminCreationForm
This commit is contained in:
parent
8c8c9f52d1
commit
466a9d3c24
|
@ -2,7 +2,10 @@ from allauth.account.forms import SignupForm
|
||||||
from allauth.socialaccount.forms import SignupForm as SocialSignupForm
|
from allauth.socialaccount.forms import SignupForm as SocialSignupForm
|
||||||
from django.contrib.auth import forms as admin_forms
|
from django.contrib.auth import forms as admin_forms
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
{%- if cookiecutter.username_type == "email" %}
|
||||||
from django.forms import EmailField
|
from django.forms import EmailField
|
||||||
|
{%- endif %}
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
|
@ -10,7 +13,9 @@ User = get_user_model()
|
||||||
class UserAdminChangeForm(admin_forms.UserChangeForm):
|
class UserAdminChangeForm(admin_forms.UserChangeForm):
|
||||||
class Meta(admin_forms.UserChangeForm.Meta):
|
class Meta(admin_forms.UserChangeForm.Meta):
|
||||||
model = User
|
model = User
|
||||||
|
{%- if cookiecutter.username_type == "email" %}
|
||||||
field_classes = {"email": EmailField}
|
field_classes = {"email": EmailField}
|
||||||
|
{%- endif %}
|
||||||
|
|
||||||
|
|
||||||
class UserAdminCreationForm(admin_forms.UserCreationForm):
|
class UserAdminCreationForm(admin_forms.UserCreationForm):
|
||||||
|
@ -21,8 +26,17 @@ class UserAdminCreationForm(admin_forms.UserCreationForm):
|
||||||
|
|
||||||
class Meta(admin_forms.UserCreationForm.Meta):
|
class Meta(admin_forms.UserCreationForm.Meta):
|
||||||
model = User
|
model = User
|
||||||
|
{%- if cookiecutter.username_type == "email" %}
|
||||||
fields = ("email",)
|
fields = ("email",)
|
||||||
field_classes = {"email": EmailField}
|
field_classes = {"email": EmailField}
|
||||||
|
error_messages = {
|
||||||
|
"username": {"unique": _("This email has already been taken.")}
|
||||||
|
}
|
||||||
|
{%- else %}
|
||||||
|
error_messages = {
|
||||||
|
"username": {"unique": _("This username has already been taken.")}
|
||||||
|
}
|
||||||
|
{%- endif %}
|
||||||
|
|
||||||
|
|
||||||
class UserSignupForm(SignupForm):
|
class UserSignupForm(SignupForm):
|
||||||
|
|
|
@ -24,7 +24,7 @@ class TestUserAdminCreationForm:
|
||||||
# hence cannot be created.
|
# hence cannot be created.
|
||||||
form = UserAdminCreationForm(
|
form = UserAdminCreationForm(
|
||||||
{
|
{
|
||||||
{%- if cookiecutter.username_type == "email" -%}
|
{%- if cookiecutter.username_type == "email" %}
|
||||||
"email": user.email,
|
"email": user.email,
|
||||||
{%- else %}
|
{%- else %}
|
||||||
"username": user.username,
|
"username": user.username,
|
||||||
|
@ -36,7 +36,7 @@ class TestUserAdminCreationForm:
|
||||||
|
|
||||||
assert not form.is_valid()
|
assert not form.is_valid()
|
||||||
assert len(form.errors) == 1
|
assert len(form.errors) == 1
|
||||||
{%- if cookiecutter.username_type == "email" -%}
|
{%- if cookiecutter.username_type == "email" %}
|
||||||
assert "email" in form.errors
|
assert "email" in form.errors
|
||||||
assert form.errors["email"][0] == _("This email has already been taken.")
|
assert form.errors["email"][0] == _("This email has already been taken.")
|
||||||
{%- else %}
|
{%- else %}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user