Skip to content

Commit

Permalink
refactor(headless): Extract frontend URL handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed May 12, 2024
1 parent dc21e54 commit ac67c56
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 64 deletions.
23 changes: 6 additions & 17 deletions allauth/account/internal/flows/manage_email.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from django.contrib import messages
from django.core.exceptions import ImproperlyConfigured
from django.urls import reverse

from allauth import app_settings as allauth_settings
from allauth.account import app_settings, signals
from allauth.account.adapter import get_adapter
from allauth.account.models import EmailAddress
from allauth.account.reauthentication import raise_if_reauthentication_required
from allauth.core.internal.httpkit import render_url
from allauth.core.internal.httpkit import get_frontend_url
from allauth.utils import build_absolute_uri


Expand Down Expand Up @@ -146,17 +144,8 @@ def get_email_verification_url(request, emailconfirmation):
confirmations are sent outside of the request context `request`
can be `None` here.
"""
if allauth_settings.HEADLESS_ENABLED:
from allauth.headless import app_settings as headless_settings

url = headless_settings.FRONTEND_URLS.get("account_confirm_email")
if allauth_settings.HEADLESS_ONLY and not url:
raise ImproperlyConfigured(
"settings.HEADLESS_FRONTEND_URLS['account_confirm_email']"
)
if url:
return render_url(request, url, key=emailconfirmation.key)

url = reverse("account_confirm_email", args=[emailconfirmation.key])
ret = build_absolute_uri(request, url)
return ret
url = get_frontend_url(request, "account_confirm_email", key=emailconfirmation.key)
if not url:
url = reverse("account_confirm_email", args=[emailconfirmation.key])
url = build_absolute_uri(request, url)
return url
49 changes: 16 additions & 33 deletions allauth/account/internal/flows/password_reset.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from urllib.parse import quote

from django.contrib import messages
from django.core.exceptions import ImproperlyConfigured
from django.urls import reverse

from allauth import app_settings as allauth_settings
from allauth.account import signals
from allauth.account.adapter import get_adapter
from allauth.core.internal.httpkit import render_url
from allauth.core.internal.httpkit import get_frontend_url
from allauth.utils import build_absolute_uri


Expand Down Expand Up @@ -37,40 +35,25 @@ def finalize_password_reset(request, user):


def get_reset_password_url(request):
if allauth_settings.HEADLESS_ENABLED:
from allauth.headless import app_settings as headless_settings

url = headless_settings.FRONTEND_URLS.get("account_reset_password")
if allauth_settings.HEADLESS_ONLY and not url:
raise ImproperlyConfigured(
"settings.HEADLESS_FRONTEND_URLS['account_reset_password']"
)
if url:
return render_url(request, url)
return build_absolute_uri(request, reverse("account_reset_password"))
url = get_frontend_url(request, "account_reset_password")
if not url:
url = build_absolute_uri(request, reverse("account_reset_password"))
return url


def get_reset_password_from_key_url(request, key):
"""
Method intented to be overriden in case the password reset email
needs to point to your frontend/SPA.
"""
if allauth_settings.HEADLESS_ENABLED:
from allauth.headless import app_settings as headless_settings

url = headless_settings.FRONTEND_URLS.get("account_reset_password_from_key")
if allauth_settings.HEADLESS_ONLY and not url:
raise ImproperlyConfigured(
"settings.HEADLESS_FRONTEND_URLS['account_reset_password_from_key']"
)
if url:
return render_url(request, url, key=key)

# We intentionally accept an opaque `key` on the interface here, and not
# implementation details such as a separate `uidb36` and `key. Ideally,
# this should have done on `urls` level as well.
path = reverse(
"account_reset_password_from_key", kwargs={"uidb36": "UID", "key": "KEY"}
)
path = path.replace("UID-KEY", quote(key))
return build_absolute_uri(request, path)
url = get_frontend_url(request, "account_reset_password_from_key", key=key)
if not url:
# We intentionally accept an opaque `key` on the interface here, and not
# implementation details such as a separate `uidb36` and `key. Ideally,
# this should have done on `urls` level as well.
path = reverse(
"account_reset_password_from_key", kwargs={"uidb36": "UID", "key": "KEY"}
)
path = path.replace("UID-KEY", quote(key))
url = build_absolute_uri(request, path)
return url
19 changes: 5 additions & 14 deletions allauth/account/internal/flows/signup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.urls import reverse

from allauth import app_settings as allauth_settings
from allauth.account.adapter import get_adapter
from allauth.core.internal.httpkit import render_url
from allauth.core.internal.httpkit import get_frontend_url
from allauth.utils import build_absolute_uri


Expand All @@ -17,14 +15,7 @@ def send_unknown_account_mail(request, email):


def get_signup_url(request):
if allauth_settings.HEADLESS_ENABLED:
from allauth.headless import app_settings as headless_settings

url = headless_settings.FRONTEND_URLS.get("account_signup")
if allauth_settings.HEADLESS_ONLY and not url:
raise ImproperlyConfigured(
"settings.HEADLESS_FRONTEND_URLS['account_signup']"
)
if url:
return render_url(request, url)
return build_absolute_uri(request, reverse("account_signup"))
url = get_frontend_url(request, "account_signup")
if not url:
url = build_absolute_uri(request, reverse("account_signup"))
return url
14 changes: 14 additions & 0 deletions allauth/core/internal/httpkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,17 @@ def render_url(request, url_template, **kwargs):
if not p.netloc:
url = request.build_absolute_uri(url)
return url


def get_frontend_url(request, urlname, **kwargs):
from allauth import app_settings as allauth_settings

if allauth_settings.HEADLESS_ENABLED:
from allauth.headless import app_settings as headless_settings

url = headless_settings.FRONTEND_URLS.get(urlname)
if allauth_settings.HEADLESS_ONLY and not url:
raise ImproperlyConfigured(f"settings.HEADLESS_FRONTEND_URLS['{urlname}']")
if url:
return render_url(request, url, **kwargs)
return None

0 comments on commit ac67c56

Please sign in to comment.