From ab5e3b88aea00957075d9f75ec8c266b74a6cdc5 Mon Sep 17 00:00:00 2001 From: "Martin J. Laubach" Date: Tue, 10 Sep 2024 15:12:10 +0200 Subject: [PATCH 1/3] Send email action: Offer dropdown of email fields present instead of free text input --- form_designer/models.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/form_designer/models.py b/form_designer/models.py index fe35e8d..42408b7 100644 --- a/form_designer/models.py +++ b/form_designer/models.py @@ -51,6 +51,13 @@ def validate_comma_separated_emails(value): for v in value.split(","): validate_email(v.strip()) +def email_field_choices(form: forms.ModelForm | None, required: bool=True) -> list[tuple[str, str]]: + if not form: + return [] + email_fields = form.instance.fields.filter(type='email') + choices = [] if required else [('', '--')] + choices.extend([(_.name, _.title) for _ in email_fields]) + return choices class Form(models.Model): CONFIG_OPTIONS = [ @@ -86,13 +93,13 @@ class Form(models.Model): ), ( "author_email_field", - forms.CharField( + forms.ChoiceField( label=capfirst(_("author's email field")), help_text=_( "The author of the submission will be added to the Cc: if this is set to an existing form field below." ), required=False, - widget=widgets.AdminTextInputWidget, + choices=email_field_choices(form, required=False), ), ), ], From 38b041bc0cf17acd46d2a821ee37af40b4e4d06f Mon Sep 17 00:00:00 2001 From: "Martin J. Laubach" Date: Tue, 10 Sep 2024 15:26:29 +0200 Subject: [PATCH 2/3] Fix tests failing --- form_designer/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/form_designer/models.py b/form_designer/models.py index 42408b7..79ace53 100644 --- a/form_designer/models.py +++ b/form_designer/models.py @@ -52,7 +52,7 @@ def validate_comma_separated_emails(value): validate_email(v.strip()) def email_field_choices(form: forms.ModelForm | None, required: bool=True) -> list[tuple[str, str]]: - if not form: + if not form or not form.instance or not form.instance.pk: return [] email_fields = form.instance.fields.filter(type='email') choices = [] if required else [('', '--')] From 92cabed4c02833f4cf25017ae5e6461169fc2ce8 Mon Sep 17 00:00:00 2001 From: "Martin J. Laubach" Date: Tue, 10 Sep 2024 15:31:09 +0200 Subject: [PATCH 3/3] py 3.9 needs Optional --- form_designer/models.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/form_designer/models.py b/form_designer/models.py index 79ace53..ae6e7a9 100644 --- a/form_designer/models.py +++ b/form_designer/models.py @@ -1,3 +1,5 @@ +from typing import Optional + import warnings from functools import partial @@ -51,7 +53,7 @@ def validate_comma_separated_emails(value): for v in value.split(","): validate_email(v.strip()) -def email_field_choices(form: forms.ModelForm | None, required: bool=True) -> list[tuple[str, str]]: +def email_field_choices(form: Optional[forms.ModelForm], required: bool=True) -> list[tuple[str, str]]: if not form or not form.instance or not form.instance.pk: return [] email_fields = form.instance.fields.filter(type='email')