Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(accounts): Added prefilling of forms with GET query strings #3410

Closed
wants to merge 0 commits into from

Conversation

varunsaral
Copy link
Contributor

This pr adds email prefilling suport in signup form, closes #2861

allauth/account/views.py Outdated Show resolved Hide resolved
@@ -290,6 +290,13 @@ def get_context_data(self, **kwargs):
)
return ret

def get_initial(self):
if self.request.GET.get("email", None):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self.request.GET.get("email", None):
if "email" in self.request.GET:

allauth/account/views.py Outdated Show resolved Hide resolved
initial = super().get_initial()
initial["email"] = self.request.GET.get("email")
return initial
return super().get_initial()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would also be nice to make this a bit more DRY. You're invoking super().get_initial twice. How about:

initial = super().get_initial():
if ...:
  prefill
return initial

Copy link
Contributor Author

@varunsaral varunsaral Sep 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted, will do

twice_email = app_settings.SIGNUP_EMAIL_ENTER_TWICE
request_get = self.request.GET

def validate_and_set_email(email_key):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a little bit complex to me, e.g. there is no need to validate the email twice. How about simplifying into:

from django.core.validators import validate_email

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

@varunsaral
Copy link
Contributor Author

I think this should be fine @pennersr, don't know why black,isort are failing i applied them before pushing

Copy link
Contributor

@fdobrovolny fdobrovolny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why your black and isort, are not working. But the following changes seem like the reason the tests are failing.

Comment on lines 49 to 50
from django.core.validators import EmailValidator
from django.forms import ValidationError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isort is failing on these two lines if you take a look at the logs:

--- /home/runner/work/django-allauth/django-allauth/allauth/account/views.py:before	2023-09-03 10:56:32.162313
+++ /home/runner/work/django-allauth/django-allauth/allauth/account/views.py:after	2023-09-03 10:56:51.994464
@@ -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 EmailValidator
+from django.forms import ValidationError
 from django.http import (
     Http404,
     HttpResponsePermanentRedirect,
@@ -46,8 +48,7 @@
     sync_user_email_addresses,
     url_str_to_user_pk,
 )
-from django.core.validators import EmailValidator
-from django.forms import ValidationError
+

There should be two lines between the first code line and imports. You removed one.

The imports should be moved to be sorted alphabetically, as can be seen in the patch by isort.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fdobrovolny for the suggestion, turns out the isort and black installation were messed up.

@@ -290,6 +291,19 @@ def get_context_data(self, **kwargs):
)
return ret

def get_initial(self):
intial = super(SignupView,self).get_initial()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
intial = super(SignupView,self).get_initial()
intial = super(SignupView, self).get_initial()

To make black happy you need a space here.

@coveralls
Copy link

Coverage Status

coverage: 91.483% (-0.007%) from 91.49% when pulling 8fac170 on varunsaral:prefilled_forms into ba92ef3 on pennersr:main.

@varunsaral
Copy link
Contributor Author

@pennersr this can be closed. Everything changed according to the suggestions

@varunsaral
Copy link
Contributor Author

Along with #3003

@@ -315,3 +315,17 @@ def test_django_password_validation(self):
"password1",
["This password is too short. It must contain at least 9 characters."],
)

def test_get_initial_with_valid_email(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The codebase is transitioning towards using pytest based tests. Can you turn this into a regular (pytest) function? As in, not make it a mapper of a class, let it accept rf (request factory) as a parameter, and so on...

@@ -290,6 +292,19 @@ def get_context_data(self, **kwargs):
)
return ret

def get_initial(self):
intial = super(SignupView, self).get_initial()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typ-o: in-i-tial

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the spelling is correct, it's in-i-tial

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it says intial = ...

email = self.request.GET.get("email")
if email:
try:
EmailValidator()(email)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to spawn a validator yourself, just call this method:

https://docs.djangoproject.com/en/4.2/ref/validators/#validate-email

@@ -290,6 +292,19 @@ def get_context_data(self, **kwargs):
)
return ret

def get_initial(self):
intial = super(SignupView, self).get_initial()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use super() -- no need to be Python 2 compliant anymore.

@varunsaral
Copy link
Contributor Author

@pennersr Mistakenly rebased wrong and now whole branch got deleted 😭 , sorry about the trouble. I have redone all the changes and raised the pr #3447. I have checked it throughly and changed everything with your review suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Prefill option for E-Mail on signup form
4 participants