[Django] #36110: Unable to customize kwargs passed to PasswordResetForm EmailMultiAlternatives

11 views
Skip to first unread message

Django

unread,
Jan 18, 2025, 10:44:24 AMJan 18
to django-...@googlegroups.com
#36110: Unable to customize kwargs passed to PasswordResetForm
EmailMultiAlternatives
------------------------------+----------------------------------------
Reporter: Claude Paroz | Type: New feature
Status: new | Component: contrib.auth
Version: dev | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Use case: you'd like to add a Reply-To header to the password reset
message.

To do that currently, you have no other choice than to rewrite the whole
`PasswordResetForm.send_mail` method. I think a hook is needed to be able
to define any other EmailMultiAlternatives init arguments.
--
Ticket URL: <https://code.djangoproject.com/ticket/36110>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 18, 2025, 10:46:48 AMJan 18
to django-...@googlegroups.com
#36110: Unable to customize kwargs passed to PasswordResetForm
EmailMultiAlternatives
------------------------------+--------------------------------------
Reporter: Claude Paroz | Owner: (none)
Type: New feature | Status: new
Component: contrib.auth | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Comment (by Claude Paroz):

Note I didn't pass through a forum post as I think the need is rather
straightforward. You may disagree :-)

A possible implementation:
{{{
diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py
index cd177fa5b6..56fedc4ae7 100644
--- a/django/contrib/auth/forms.py
+++ b/django/contrib/auth/forms.py
@@ -402,6 +402,14 @@ class PasswordResetForm(forms.Form):
widget=forms.EmailInput(attrs={"autocomplete": "email"}),
)

+ def extra_email_kwargs(self):
+ """
+ Allow setting extra EmailMultiAlternatives init parameters, after
the
+ first four parameters (subject, body, from_email, to) which are
defined
+ inside the send_mail() method.
+ """
+ return {}
+
def send_mail(
self,
subject_template_name,
@@ -419,7 +427,7 @@ class PasswordResetForm(forms.Form):
subject = "".join(subject.splitlines())
body = loader.render_to_string(email_template_name, context)

- email_message = EmailMultiAlternatives(subject, body, from_email,
[to_email])
+ email_message = EmailMultiAlternatives(subject, body, from_email,
[to_email], **self.extra_email_kwargs())
if html_email_template_name is not None:
html_email =
loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, "text/html")
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36110#comment:1>

Django

unread,
Jan 18, 2025, 9:20:42 PMJan 18
to django-...@googlegroups.com
#36110: Unable to customize kwargs passed to PasswordResetForm
EmailMultiAlternatives
------------------------------+--------------------------------------
Reporter: Claude Paroz | Owner: Antoliny
Type: New feature | Status: assigned
Component: contrib.auth | Version: dev
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Changes (by Antoliny):

* keywords: => email
* owner: (none) => Antoliny
* status: new => assigned

Comment:

May I work on this issue? If you decide to take it on, feel free to
unassign me.
--
Ticket URL: <https://code.djangoproject.com/ticket/36110#comment:2>

Django

unread,
Jan 19, 2025, 2:52:31 PMJan 19
to django-...@googlegroups.com
#36110: Unable to customize kwargs passed to PasswordResetForm
EmailMultiAlternatives
------------------------------+--------------------------------------
Reporter: Claude Paroz | Owner: Antoliny
Type: New feature | Status: assigned
Component: contrib.auth | Version: dev
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Comment (by Claude Paroz):

Sure, but please wait for the ticket approval first.
--
Ticket URL: <https://code.djangoproject.com/ticket/36110#comment:3>

Django

unread,
Jan 19, 2025, 4:18:14 PMJan 19
to django-...@googlegroups.com
#36110: Unable to customize kwargs passed to PasswordResetForm
EmailMultiAlternatives
------------------------------+--------------------------------------
Reporter: Claude Paroz | Owner: Antoliny
Type: New feature | Status: assigned
Component: contrib.auth | Version: dev
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Comment (by Tim Graham):

The use case was mentioned in #17431 where `PasswordResetForm.send_mail()`
was added. I agree it's not so nice to have to override the entire method,
however, the test added in a00b78b1e2ac9bf271d55c1799138a27f5e0d03e adds
Reply-To and does not look so ugly.

Also note that much this form's API is controlled in the
[https://github.com/django/django/blob/1c29ab24df876c47317713f9dacd5bc51cf90857/django/contrib/auth/views.py#L215-L243
view via form.save() kwargs]. In light of that, it looks a bit out of
place to add a method hook for the proposed purpose.
--
Ticket URL: <https://code.djangoproject.com/ticket/36110#comment:4>

Django

unread,
Jan 19, 2025, 4:28:41 PMJan 19
to django-...@googlegroups.com
#36110: Unable to customize kwargs passed to PasswordResetForm
EmailMultiAlternatives
------------------------------+--------------------------------------
Reporter: Claude Paroz | Owner: Antoliny
Type: New feature | Status: assigned
Component: contrib.auth | Version: dev
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Comment (by Claude Paroz):

> and does not look so ugly

However it hardcodes things to not look ugly!

> it looks a bit out of place to add a method hook for the proposed
purpose

Sure, the implementation could be different than the one I proposed. I'm
not opposed to make the customization as a new `form.save()` kwarg.
--
Ticket URL: <https://code.djangoproject.com/ticket/36110#comment:5>

Django

unread,
Jan 20, 2025, 11:29:31 AMJan 20
to django-...@googlegroups.com
#36110: Unable to customize kwargs passed to PasswordResetForm
EmailMultiAlternatives
------------------------------+--------------------------------------
Reporter: Claude Paroz | Owner: Antoliny
Type: New feature | Status: closed
Component: contrib.auth | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: email | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* resolution: => wontfix
* status: assigned => closed

Comment:

> However it hardcodes things to not look ugly!

True, though I believe if you needed to overwrite `send_mail`, it's
probable that you can simplify and hard-code some logic (I might be wrong)

I am on the fence here, so would prefer for this to be discussed on
[https://forum.djangoproject.com/c/internals/5 the forum] and see if there
are other folks who are in favor of the change
--
Ticket URL: <https://code.djangoproject.com/ticket/36110#comment:6>
Reply all
Reply to author
Forward
0 new messages