diff --git a/{{cookiecutter.project_slug}}/config/settings/base.py b/{{cookiecutter.project_slug}}/config/settings/base.py index 021dd749..5177c92f 100644 --- a/{{cookiecutter.project_slug}}/config/settings/base.py +++ b/{{cookiecutter.project_slug}}/config/settings/base.py @@ -306,8 +306,12 @@ ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_EMAIL_VERIFICATION = "mandatory" # https://django-allauth.readthedocs.io/en/latest/configuration.html ACCOUNT_ADAPTER = "{{cookiecutter.project_slug}}.users.adapters.AccountAdapter" +# https://django-allauth.readthedocs.io/en/latest/forms.html +ACCOUNT_FORMS = {"signup": "{{cookiecutter.project_slug}}.users.forms.UserSignupForm"} # https://django-allauth.readthedocs.io/en/latest/configuration.html SOCIALACCOUNT_ADAPTER = "{{cookiecutter.project_slug}}.users.adapters.SocialAccountAdapter" +# https://django-allauth.readthedocs.io/en/latest/forms.html +SOCIALACCOUNT_FORMS = {"signup": "{{cookiecutter.project_slug}}.users.forms.UserSocialSignupForm"} {% if cookiecutter.use_compressor == 'y' -%} # django-compressor # ------------------------------------------------------------------------------ diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/admin.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/admin.py index 8e8e3eb2..6675f483 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/admin.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/admin.py @@ -3,7 +3,7 @@ from django.contrib.auth import admin as auth_admin from django.contrib.auth import get_user_model from django.utils.translation import gettext_lazy as _ -from {{ cookiecutter.project_slug }}.users.forms import UserChangeForm, UserCreationForm +from {{ cookiecutter.project_slug }}.users.forms import UserAdminChangeForm, UserAdminCreationForm User = get_user_model() @@ -11,8 +11,8 @@ User = get_user_model() @admin.register(User) class UserAdmin(auth_admin.UserAdmin): - form = UserChangeForm - add_form = UserCreationForm + form = UserAdminChangeForm + add_form = UserAdminCreationForm fieldsets = ( (None, {"fields": ("username", "password")}), (_("Personal info"), {"fields": ("name", "email")}), diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py index 80cd97ae..6e1dd9d3 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py @@ -1,3 +1,5 @@ +from allauth.account.forms import SignupForm +from allauth.socialaccount.forms import SignupForm as SocialSignupForm from django.contrib.auth import forms as admin_forms from django.contrib.auth import get_user_model from django.utils.translation import gettext_lazy as _ @@ -5,15 +7,36 @@ from django.utils.translation import gettext_lazy as _ User = get_user_model() -class UserChangeForm(admin_forms.UserChangeForm): +class UserAdminChangeForm(admin_forms.UserChangeForm): class Meta(admin_forms.UserChangeForm.Meta): model = User -class UserCreationForm(admin_forms.UserCreationForm): +class UserAdminCreationForm(admin_forms.UserCreationForm): + """ + Form for User Creation in the Admin Area. + To change user signup, see UserSignupForm and UserSocialSignupForm. + """ + class Meta(admin_forms.UserCreationForm.Meta): model = User error_messages = { "username": {"unique": _("This username has already been taken.")} } + + +class UserSignupForm(SignupForm): + """ + Form that will be rendered on a user sign up section/screen. + Default fields will be added automatically. + Check UserSocialSignupForm for accounts created from social. + """ + + +class UserSocialSignupForm(SocialSignupForm): + """ + Renders the form when user has signed up using social accounts. + Default fields will be added automatically. + See UserSignupForm otherwise. + """ diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/models.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/models.py index 935eee9a..1f6f61bc 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/models.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/models.py @@ -5,7 +5,11 @@ from django.utils.translation import gettext_lazy as _ class User(AbstractUser): - """Default user for {{cookiecutter.project_name}}.""" + """ + Default custom user model for {{cookiecutter.project_name}}. + If adding fields that need to be filled at user signup, + check forms.SignupForm and forms.SocialSignupForms accordingly. + """ #: First and last name do not cover name patterns around the globe name = CharField(_("Name of User"), blank=True, max_length=255) diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py index 35db91b4..e51bb6bf 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py @@ -4,20 +4,20 @@ Module for all Form Tests. import pytest from django.utils.translation import gettext_lazy as _ -from {{ cookiecutter.project_slug }}.users.forms import UserCreationForm +from {{ cookiecutter.project_slug }}.users.forms import UserAdminCreationForm from {{ cookiecutter.project_slug }}.users.models import User pytestmark = pytest.mark.django_db -class TestUserCreationForm: +class TestUserAdminCreationForm: """ - Test class for all tests related to the UserCreationForm + Test class for all tests related to the UserAdminCreationForm """ def test_username_validation_error_msg(self, user: User): """ - Tests UserCreation Form's unique validator functions correctly by testing: + Tests UserAdminCreation Form's unique validator functions correctly by testing: 1) A new user with an existing username cannot be added. 2) Only 1 error is raised by the UserCreation Form 3) The desired error message is raised @@ -25,7 +25,7 @@ class TestUserCreationForm: # The user already exists, # hence cannot be created. - form = UserCreationForm( + form = UserAdminCreationForm( { "username": user.username, "password1": user.password, diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_views.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_views.py index 7170bfae..944daca5 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_views.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_views.py @@ -8,7 +8,7 @@ from django.http import HttpRequest, HttpResponseRedirect from django.test import RequestFactory from django.urls import reverse -from {{ cookiecutter.project_slug }}.users.forms import UserChangeForm +from {{ cookiecutter.project_slug }}.users.forms import UserAdminChangeForm from {{ cookiecutter.project_slug }}.users.models import User from {{ cookiecutter.project_slug }}.users.tests.factories import UserFactory from {{ cookiecutter.project_slug }}.users.views import ( @@ -62,7 +62,7 @@ class TestUserUpdateView: view.request = request # Initialize the form - form = UserChangeForm() + form = UserAdminChangeForm() form.cleaned_data = [] view.form_valid(form)