-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Conversation
allauth/account/views.py
Outdated
@@ -290,6 +290,13 @@ def get_context_data(self, **kwargs): | |||
) | |||
return ret | |||
|
|||
def get_initial(self): | |||
if self.request.GET.get("email", None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if self.request.GET.get("email", None): | |
if "email" in self.request.GET: |
allauth/account/views.py
Outdated
initial = super().get_initial() | ||
initial["email"] = self.request.GET.get("email") | ||
return initial | ||
return super().get_initial() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noted, will do
allauth/account/views.py
Outdated
twice_email = app_settings.SIGNUP_EMAIL_ENTER_TWICE | ||
request_get = self.request.GET | ||
|
||
def validate_and_set_email(email_key): |
There was a problem hiding this comment.
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
I think this should be fine @pennersr, don't know why black,isort are failing i applied them before pushing |
There was a problem hiding this 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.
allauth/account/views.py
Outdated
from django.core.validators import EmailValidator | ||
from django.forms import ValidationError |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
allauth/account/views.py
Outdated
@@ -290,6 +291,19 @@ def get_context_data(self, **kwargs): | |||
) | |||
return ret | |||
|
|||
def get_initial(self): | |||
intial = super(SignupView,self).get_initial() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
intial = super(SignupView,self).get_initial() | |
intial = super(SignupView, self).get_initial() |
To make black happy you need a space here.
8efe858
to
cfcf9f1
Compare
@pennersr this can be closed. Everything changed according to the suggestions |
Along with #3003 |
allauth/account/tests/test_signup.py
Outdated
@@ -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): |
There was a problem hiding this comment.
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...
allauth/account/views.py
Outdated
@@ -290,6 +292,19 @@ def get_context_data(self, **kwargs): | |||
) | |||
return ret | |||
|
|||
def get_initial(self): | |||
intial = super(SignupView, self).get_initial() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it says intial = ...
allauth/account/views.py
Outdated
email = self.request.GET.get("email") | ||
if email: | ||
try: | ||
EmailValidator()(email) |
There was a problem hiding this comment.
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
allauth/account/views.py
Outdated
@@ -290,6 +292,19 @@ def get_context_data(self, **kwargs): | |||
) | |||
return ret | |||
|
|||
def get_initial(self): | |||
intial = super(SignupView, self).get_initial() |
There was a problem hiding this comment.
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.
6d6f773
to
aaa0b67
Compare
This pr adds email prefilling suport in signup form, closes #2861