Skip to content

Commit

Permalink
feat(account): Added prefilling of email field in signup form
Browse files Browse the repository at this point in the history
Co-authored-by: varun kumar <[email protected]>
  • Loading branch information
varunsaral and varun kumar authored Sep 24, 2023
1 parent f98c9a3 commit ddadb93
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions allauth/account/tests/test_signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.test.utils import override_settings
from django.urls import reverse

import pytest
from pytest_django.asserts import assertTemplateUsed

from allauth.account import app_settings
Expand Down Expand Up @@ -371,3 +372,16 @@ def test_prevent_enumeration_on(settings, user_factory):
assert resp.context["form"].errors == {
"email": ["A user is already registered with this email address."]
}


@pytest.mark.django_db
def test_get_initial_with_valid_email():
"""Test that the email field is populated with a valid email."""
request = RequestFactory().get("/signup/[email protected]")
from allauth.account.views import signup

SessionMiddleware(lambda request: None).process_request(request)
request.user = AnonymousUser()
with context.request_context(request):
view = signup(request)
assert view.context_data["view"].get_initial()["email"] == "[email protected]"
15 changes: 15 additions & 0 deletions allauth/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import login_required
from django.contrib.sites.shortcuts import get_current_site
from django.core.validators import validate_email
from django.forms import ValidationError
from django.http import (
Http404,
HttpResponsePermanentRedirect,
Expand Down Expand Up @@ -290,6 +292,19 @@ def get_context_data(self, **kwargs):
)
return ret

def get_initial(self):
initial = super().get_initial()
email = self.request.GET.get("email")
if email:
try:
validate_email(email)
except ValidationError:
return initial
initial["email"] = email
if app_settings.SIGNUP_EMAIL_ENTER_TWICE:
initial["email2"] = email
return initial


signup = SignupView.as_view()

Expand Down

0 comments on commit ddadb93

Please sign in to comment.