Skip to content

Commit

Permalink
Send email action: Offer dropdown of email fields present instead of …
Browse files Browse the repository at this point in the history
…free text input
  • Loading branch information
Martin J. Laubach committed Sep 10, 2024
1 parent d00ec8b commit ab5e3b8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions form_designer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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),
),
),
],
Expand Down

0 comments on commit ab5e3b8

Please sign in to comment.