Debian Codegraph @

allauth/account/internal/flows/signup.py

raw
1 from __future__ import annotations
2
3 from importlib import import_module
4
5 from django import forms
6 from django.contrib.auth.base_user import AbstractBaseUser
7 from django.core import exceptions
8 from django.http import HttpRequest, HttpResponse
9 from django.urls import reverse
10
11 from allauth.account import app_settings, signals
12 from allauth.account.adapter import get_adapter
13 from allauth.account.internal.flows.login import perform_login
14 from allauth.account.models import Login
15 from allauth.core import context
16 from allauth.core.internal.httpkit import get_frontend_url
17 from allauth.utils import build_absolute_uri
18
19
20 class DummyCustomSignupForm(forms.Form):
21 def signup(self, request: HttpRequest, user: AbstractBaseUser) -> None:
22 """
23 Invoked at signup time to complete the signup of the user.
24 """
25 pass
26
27
28 def base_signup_form_class():
29 """
30 Currently, we inherit from the custom form, if any. This is all
31 not very elegant, though it serves a purpose:
32
33 - There are two signup forms: one for local accounts, and one for
34 social accounts
35 - Both share a common base (BaseSignupForm)
36
37 - Given the above, how to put in a custom signup form? Which form
38 would your custom form derive from, the local or the social one?
39 """
40 if not app_settings.SIGNUP_FORM_CLASS:
41 return DummyCustomSignupForm
42 try:
43 fc_module, fc_classname = app_settings.SIGNUP_FORM_CLASS.rsplit(".", 1)
44 except ValueError:
45 raise exceptions.ImproperlyConfigured(
46 f"{app_settings.SIGNUP_FORM_CLASS} does not point to a form class"
47 )
48 try:
49 mod = import_module(fc_module)
50 except ImportError as e:
51 raise exceptions.ImproperlyConfigured(
52 f'Error importing form class {fc_module}: "{e}"'
53 )
54 try:
55 fc_class = getattr(mod, fc_classname)
56 except AttributeError:
57 raise exceptions.ImproperlyConfigured(
58 f'Module "{fc_module}" does not define a "{fc_classname}" class'
59 )
60 if not hasattr(fc_class, "signup"):
61 raise exceptions.ImproperlyConfigured(
62 "The custom signup form must offer"
63 " a `def signup(self, request, user)` method",
64 )
65 return fc_class
66
67
68 def prevent_enumeration(
69 request: HttpRequest, email: str | None = None, phone: str | None = None
70 ) -> HttpResponse:
71 login = Login(user=None, email=email, phone=phone, signup=True)
72 return perform_login(context.request, login)
73
74
75 def send_unknown_account_mail(request: HttpRequest, email: str) -> None:
76 if not app_settings.EMAIL_UNKNOWN_ACCOUNTS:
77 return None
78 signup_url = get_signup_url(request)
79 context = {
80 "request": request,
81 "signup_url": signup_url,
82 }
83 get_adapter().send_mail("account/email/unknown_account", email, context)
84
85
86 def get_signup_url(request: HttpRequest) -> str:
87 url = get_frontend_url(request, "account_signup")
88 if not url:
89 url = build_absolute_uri(request, reverse("account_signup"))
90 return url
91
92
93 def complete_signup(
94 request: HttpRequest,
95 *,
96 user,
97 email_verification=None,
98 redirect_url=None,
99 signal_kwargs=None,
100 by_passkey=False,
101 ) -> HttpResponse:
102 if signal_kwargs is None:
103 signal_kwargs = {}
104 signals.user_signed_up.send(
105 sender=user.__class__, request=request, user=user, **signal_kwargs
106 )
107 login = Login(
108 user=user,
109 email_verification=email_verification,
110 redirect_url=redirect_url,
111 signal_kwargs=signal_kwargs,
112 signup=True,
113 )
114 if by_passkey:
115 login.state["passkey_signup"] = True
116 return perform_login(request, login)