Skip to content

Commit

Permalink
Allow To field in core EmailForm #4089
Browse files Browse the repository at this point in the history
  • Loading branch information
joemull authored and ajrbyers committed Jun 4, 2024
1 parent 9013a4e commit db1c014
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
31 changes: 27 additions & 4 deletions src/core/email.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass, field
from typing import Tuple

from django.conf import ImproperlyConfigured
from django.core.files.uploadedfile import InMemoryUploadedFile

from utils import notify_helpers
Expand All @@ -16,9 +17,31 @@ class EmailData:


def send_email(
user, email_data, request, article=None, preprint=None,
log_dict=None,
):
user,
email_data,
request,
article=None,
preprint=None,
log_dict=None,
):
""" A standard way to send email using data from core.forms.EmailForm.
:param user: The main recipient of the email. Can be None if email_data has recipient
:type user: Account or NoneType
:param email_data: The email data, typically generated from EmailForm
:type email_data: EmailData
:param request: The request object
:param article: an article to be used as the log target
:param preprint: a preprint to be used as the log target
:param log_dict: log details to be used instead of a generic email log
"""

if user:
to = user.email
elif email_data.to:
to = email_data.to
else:
raise ImproperlyConfigured('Pass a user or email_data with a to field')
subject = email_data.subject
message = email_data.body

Expand All @@ -34,7 +57,7 @@ def send_email(
notify_helpers.send_email_with_body_from_user(
request,
subject,
user.email,
to,
message,
log_dict=log_dict,
cc=email_data.cc,
Expand Down
2 changes: 2 additions & 0 deletions src/core/forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
EditorialGroupForm,
EmailForm,
FileUploadForm,
FullEmailForm,
FullSettingEmailForm,
GeneratedPluginSettingForm,
GeneratedSettingForm,
JournalArticleForm,
Expand Down
24 changes: 23 additions & 1 deletion src/core/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,6 @@ def is_confirmed(self):


class EmailForm(forms.Form):
subject = forms.CharField(max_length=1000)
cc = TagitField(
required=False,
max_length=10000,
Expand All @@ -776,6 +775,7 @@ class EmailForm(forms.Form):
required=False,
max_length=10000,
)
subject = forms.CharField(max_length=1000)
body = forms.CharField(widget=TinyMCE)
attachments = MultipleFileField(required=False)

Expand Down Expand Up @@ -803,6 +803,21 @@ def as_dataclass(self):
return email.EmailData(**self.cleaned_data)


class FullEmailForm(EmailForm):
""" An email form that includes the To field
"""
to = TagitField(
required=True,
max_length=10000,
)

field_order = ['to', 'cc', 'bcc', 'subject', 'body', 'attachments']

def clean_to(self):
to = self.cleaned_data['to']
return self.email_sequence_cleaner("to", to)


class SettingEmailForm(EmailForm):
""" An Email form that populates initial data using Janeway email settings
Expand Down Expand Up @@ -832,6 +847,13 @@ def __init__(self, *args, **kwargs):
setting_name,
)


class FullSettingEmailForm(SettingEmailForm, FullEmailForm):
""" A setting-based email form that includes the To field
"""
pass


class SimpleTinyMCEForm(forms.Form):
""" A one-field form for populating a TinyMCE textarea
"""
Expand Down

0 comments on commit db1c014

Please sign in to comment.