Enhancement: allow signup on first setup

This commit is contained in:
shamoon
2025-03-26 13:28:01 -07:00
parent b4b2a92225
commit 5a834a5eca
7 changed files with 127 additions and 103 deletions

View File

@@ -21,6 +21,12 @@ class CustomAccountAdapter(DefaultAccountAdapter):
Check whether the site is open for signups, which can be
disabled via the ACCOUNT_ALLOW_SIGNUPS setting.
"""
if (
User.objects.exclude(username__in=["consumer", "AnonymousUser"]).count()
== 0
):
# If there are no users, allow signups
return True
allow_signups = super().is_open_for_signup(request)
# Override with setting, otherwise default to super.
return getattr(settings, "ACCOUNT_ALLOW_SIGNUPS", allow_signups)
@@ -73,6 +79,15 @@ class CustomAccountAdapter(DefaultAccountAdapter):
Save the user instance. Default groups are assigned to the user, if
specified in the settings.
"""
if (
User.objects.exclude(username__in=["consumer", "AnonymousUser"]).count()
== 0
):
logger.debug(f"Creating initial superuser `{user}`")
user.is_superuser = True
user.is_staff = True
user: User = super().save_user(request, user, form, commit)
group_names: list[str] = settings.ACCOUNT_DEFAULT_GROUPS
if len(group_names) > 0:

View File

@@ -17,6 +17,11 @@ class TestCustomAccountAdapter(TestCase):
def test_is_open_for_signup(self):
adapter = get_adapter()
# With no accounts, signups should be allowed
self.assertTrue(adapter.is_open_for_signup(None))
User.objects.create_user("testuser")
# Test when ACCOUNT_ALLOW_SIGNUPS is True
settings.ACCOUNT_ALLOW_SIGNUPS = True
self.assertTrue(adapter.is_open_for_signup(None))