-
Notifications
You must be signed in to change notification settings - Fork 314
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
get_email_options
not work when AllAuthPasswordResetForm
is used
#651
Comments
Based on dj_rest_auth/forms.py: class AllAuthPasswordResetForm(DefaultPasswordResetForm):
# ...
def save(self, request, **kwargs):
# ...
for user in self.users:
# ...
if (
allauth_account_settings.AUTHENTICATION_METHOD != allauth_account_settings.AuthenticationMethod.EMAIL
):
context['username'] = user_username(user)
get_adapter(request).send_mail(
'account/email/password_reset_key', email, context
) It expects the template to be in
solves the problem.
|
My solution is to override
from allauth.account import app_settings as allauth_account_settings
from allauth.account.adapter import get_adapter
from allauth.account.forms import ResetPasswordForm as DefaultPasswordResetForm
from allauth.account.forms import default_token_generator
from allauth.account.utils import (
filter_users_by_email,
user_pk_to_url_str,
user_username,
)
from dj_rest_auth.forms import default_url_generator
class CustomPasswordResetForm(DefaultPasswordResetForm):
def clean_email(self):
email = self.cleaned_data["email"]
email = get_adapter().clean_email(email)
self.users = filter_users_by_email(email, is_active=True)
return self.cleaned_data["email"]
def save(self, request, **kwargs):
email = self.cleaned_data["email"]
token_generator = kwargs.get("token_generator", default_token_generator)
for user in self.users:
temp_key = token_generator.make_token(user)
url_generator = kwargs.get("url_generator", default_url_generator)
url = url_generator(request, user, temp_key)
uid = user_pk_to_url_str(user)
context = {
"user": user,
"password_reset_url": url,
"request": request,
"token": temp_key,
"uid": uid,
}
extra_email_context = kwargs.get("extra_email_context", {})
context.update(extra_email_context)
if (
allauth_account_settings.AUTHENTICATION_METHOD !=
allauth_account_settings.AuthenticationMethod.EMAIL
):
context["username"] = user_username(user)
get_adapter(request).send_mail(
"account/email/password_reset_key", email, context
)
return self.cleaned_data["email"]
from dj_rest_auth.serializers import PasswordResetSerializer
from config import settings
from .forms import CustomPasswordResetForm
class CustomPasswordResetSerializer(PasswordResetSerializer):
password_reset_form_class = CustomPasswordResetForm
def get_email_options(self):
return {
"extra_email_context": {
"settings": settings,
}
} and have files
REST_AUTH = {
# ...
"PASSWORD_RESET_SERIALIZER":
"apps.authentication.serializers.CustomPasswordResetSerializer",
} |
I am trying to have a custom email template. When I use
PasswordResetForm
, everything seems to work fine:I am using
allauth
. In this case, in order to have the correctuid
andtoken
, I need to useAllAuthPasswordResetForm
.But then
get_email_options
not working. It keeps sending the default template.My settings look like this:
Dependencies:
How to use my custom email template?
The text was updated successfully, but these errors were encountered: