Skip to content

Commit e43bd94

Browse files
committed
refactor(account): move _base_signup_form_class() to internal
1 parent 8a51e04 commit e43bd94

File tree

2 files changed

+54
-50
lines changed

2 files changed

+54
-50
lines changed

allauth/account/forms.py

+2-50
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from importlib import import_module
21
from typing import Optional
32

43
from django import forms
@@ -17,6 +16,7 @@
1716

1817
from allauth.account.app_settings import LoginMethod
1918
from allauth.account.internal import flows
19+
from allauth.account.internal.flows.signup import base_signup_form_class
2020
from allauth.account.internal.stagekit import LOGIN_SESSION_KEY
2121
from allauth.account.internal.textkit import compare_code
2222
from allauth.account.stages import EmailVerificationStage
@@ -279,55 +279,7 @@ def _login_with_password(self, request, redirect_url, credentials):
279279
return ret
280280

281281

282-
class _DummyCustomSignupForm(forms.Form):
283-
def signup(self, request, user):
284-
"""
285-
Invoked at signup time to complete the signup of the user.
286-
"""
287-
pass
288-
289-
290-
def _base_signup_form_class():
291-
"""
292-
Currently, we inherit from the custom form, if any. This is all
293-
not very elegant, though it serves a purpose:
294-
295-
- There are two signup forms: one for local accounts, and one for
296-
social accounts
297-
- Both share a common base (BaseSignupForm)
298-
299-
- Given the above, how to put in a custom signup form? Which form
300-
would your custom form derive from, the local or the social one?
301-
"""
302-
if not app_settings.SIGNUP_FORM_CLASS:
303-
return _DummyCustomSignupForm
304-
try:
305-
fc_module, fc_classname = app_settings.SIGNUP_FORM_CLASS.rsplit(".", 1)
306-
except ValueError:
307-
raise exceptions.ImproperlyConfigured(
308-
"%s does not point to a form class" % app_settings.SIGNUP_FORM_CLASS
309-
)
310-
try:
311-
mod = import_module(fc_module)
312-
except ImportError as e:
313-
raise exceptions.ImproperlyConfigured(
314-
"Error importing form class %s:" ' "%s"' % (fc_module, e)
315-
)
316-
try:
317-
fc_class = getattr(mod, fc_classname)
318-
except AttributeError:
319-
raise exceptions.ImproperlyConfigured(
320-
'Module "%s" does not define a' ' "%s" class' % (fc_module, fc_classname)
321-
)
322-
if not hasattr(fc_class, "signup"):
323-
raise exceptions.ImproperlyConfigured(
324-
"The custom signup form must offer"
325-
" a `def signup(self, request, user)` method",
326-
)
327-
return fc_class
328-
329-
330-
class BaseSignupForm(_base_signup_form_class()): # type: ignore[misc]
282+
class BaseSignupForm(base_signup_form_class()): # type: ignore[misc]
331283
username = forms.CharField(
332284
label=_("Username"),
333285
min_length=app_settings.USERNAME_MIN_LENGTH,

allauth/account/internal/flows/signup.py

+52
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
from importlib import import_module
2+
3+
from django import forms
14
from django.contrib import messages
5+
from django.core import exceptions
26
from django.http import HttpRequest, HttpResponse
37
from django.urls import reverse
48

@@ -11,6 +15,54 @@
1115
from allauth.utils import build_absolute_uri
1216

1317

18+
class DummyCustomSignupForm(forms.Form):
19+
def signup(self, request, user):
20+
"""
21+
Invoked at signup time to complete the signup of the user.
22+
"""
23+
pass
24+
25+
26+
def base_signup_form_class():
27+
"""
28+
Currently, we inherit from the custom form, if any. This is all
29+
not very elegant, though it serves a purpose:
30+
31+
- There are two signup forms: one for local accounts, and one for
32+
social accounts
33+
- Both share a common base (BaseSignupForm)
34+
35+
- Given the above, how to put in a custom signup form? Which form
36+
would your custom form derive from, the local or the social one?
37+
"""
38+
if not app_settings.SIGNUP_FORM_CLASS:
39+
return DummyCustomSignupForm
40+
try:
41+
fc_module, fc_classname = app_settings.SIGNUP_FORM_CLASS.rsplit(".", 1)
42+
except ValueError:
43+
raise exceptions.ImproperlyConfigured(
44+
"%s does not point to a form class" % app_settings.SIGNUP_FORM_CLASS
45+
)
46+
try:
47+
mod = import_module(fc_module)
48+
except ImportError as e:
49+
raise exceptions.ImproperlyConfigured(
50+
"Error importing form class %s:" ' "%s"' % (fc_module, e)
51+
)
52+
try:
53+
fc_class = getattr(mod, fc_classname)
54+
except AttributeError:
55+
raise exceptions.ImproperlyConfigured(
56+
'Module "%s" does not define a' ' "%s" class' % (fc_module, fc_classname)
57+
)
58+
if not hasattr(fc_class, "signup"):
59+
raise exceptions.ImproperlyConfigured(
60+
"The custom signup form must offer"
61+
" a `def signup(self, request, user)` method",
62+
)
63+
return fc_class
64+
65+
1466
def prevent_enumeration(request: HttpRequest, email: str) -> HttpResponse:
1567
adapter = get_adapter(request)
1668
adapter.send_account_already_exists_mail(email)

0 commit comments

Comments
 (0)