Skip to content

Commit

Permalink
add default config and include user in cc
Browse files Browse the repository at this point in the history
  • Loading branch information
devketanpro committed Nov 6, 2024
1 parent 3bb7dae commit bfcf3a2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
4 changes: 3 additions & 1 deletion newsroom/agenda/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from newsroom.template_filters import is_admin_or_internal
from newsroom.settings import get_settings_collection, GENERAL_SETTINGS_LOOKUP
from newsroom.auth import get_company
from flask import current_app as app


def send_coverage_notification_email(user, agenda, wire_item):
Expand Down Expand Up @@ -84,7 +85,7 @@ def send_coverage_request_email(user, message, item):
email = user.get("email")

# send coverage request email copy to current User.
recipients.append(email)
cc = [email] if app.config.get("INCLUDE_CURRENT_USER_IN_CC") else []

item_name = item.get("name") or item.get("slugline")
user_company = get_company(user)
Expand All @@ -104,6 +105,7 @@ def send_coverage_request_email(user, message, item):

send_template_email(
to=recipients,
cc=cc,
template="coverage_request_email",
template_kwargs=template_kwargs,
)
12 changes: 8 additions & 4 deletions newsroom/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def handle_long_lines_html(html):


@celery.task(soft_time_limit=120)
def _send_email(to, subject, text_body, html_body=None, sender=None, sender_name=None, attachments_info=None):
def _send_email(to, subject, text_body, cc=None, html_body=None, sender=None, sender_name=None, attachments_info=None):
if attachments_info is None:
attachments_info = []

Expand All @@ -99,14 +99,14 @@ def _send_email(to, subject, text_body, html_body=None, sender=None, sender_name
except Exception as e:
logger.error("Error attaching {} file to mail. Receipient(s): {}. Error: {}".format(a["file_desc"], to, e))

msg = NewsroomMessage(subject=subject, sender=sender, recipients=to, attachments=decoded_attachments)
msg = NewsroomMessage(subject=subject, sender=sender, recipients=to, cc=cc, attachments=decoded_attachments)
msg.body = text_body
msg.html = html_body
app = current_app._get_current_object()
return app.mail.send(msg)


def send_email(to, subject, text_body, html_body=None, sender=None, sender_name=None, attachments_info=None):
def send_email(to, subject, text_body, cc=None, html_body=None, sender=None, sender_name=None, attachments_info=None):
"""
Sends the email
:param to: List of recipients
Expand All @@ -119,6 +119,7 @@ def send_email(to, subject, text_body, html_body=None, sender=None, sender_name=

kwargs = {
"to": to,
"cc": cc,
"subject": subject,
"text_body": handle_long_lines_text(text_body) if text_body else None,
"html_body": handle_long_lines_html(html_body) if html_body else None,
Expand Down Expand Up @@ -246,12 +247,13 @@ def send_template_email(
to: List[str],
template: str,
template_kwargs: Optional[TemplateKwargs] = None,
cc: Optional[List[str]] = None,
**kwargs: EmailKwargs,
) -> None:
"""Send email to list of recipients using default locale."""
language = current_app.config["DEFAULT_LANGUAGE"]
timezone = current_app.config["DEFAULT_TIMEZONE"]
_send_localized_email(to, template, language, timezone, template_kwargs or {}, kwargs)
_send_localized_email(to, template, language, timezone, template_kwargs or {}, kwargs, cc)


def _send_localized_email(
Expand All @@ -261,6 +263,7 @@ def _send_localized_email(
timezone: str,
template_kwargs: TemplateKwargs,
email_kwargs: EmailKwargs,
cc: Optional[List[str]] = None,
) -> None:
language = to_email_language(language)
email_templates = get_resource_service("email_templates")
Expand All @@ -272,6 +275,7 @@ def _send_localized_email(
template_kwargs.setdefault("recipient_language", language)
send_email(
to=to,
cc=cc,
subject=subject,
text_body=render_template(text_template, **template_kwargs),
html_body=render_template(html_template, **template_kwargs),
Expand Down
6 changes: 6 additions & 0 deletions newsroom/web/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,3 +851,9 @@
"query": "now/M",
},
]

#: Include Current User In Mail CC for Coverage Inquiry
#:
#: .. versionadded:: 2.8
#:
INCLUDE_CURRENT_USER_IN_CC = False
6 changes: 5 additions & 1 deletion tests/core/test_agenda.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ def test_agenda_search_filtered_by_query_product(client, app, public_company):

@mock.patch("newsroom.email.send_email", mock_send_email)
def test_coverage_request(client, app):
# enable config to include current user in CC
app.config["INCLUDE_CURRENT_USER_IN_CC"] = True

post_json(
client,
"/settings/general_settings",
Expand All @@ -332,7 +335,8 @@ def test_coverage_request(client, app):

assert resp.status_code == 201, resp.get_data().decode("utf-8")
assert len(outbox) == 1
assert outbox[0].recipients == ["[email protected]", "[email protected]"]
assert outbox[0].recipients == ["[email protected]"]
assert outbox[0].cc == ["[email protected]"]
assert outbox[0].subject == "Coverage inquiry: Conference Planning"
assert "admin admin" in outbox[0].body
assert "[email protected]" in outbox[0].body
Expand Down
6 changes: 4 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ def get_admin_user_id(app):
return (get_resource_service("users").find_one(req=None, _id=ADMIN_USER_ID) or {}).get("_id")


def mock_send_email(to, subject, text_body, html_body=None, sender=None, sender_name=None, attachments_info=[]):
def mock_send_email(
to, subject, text_body, cc=None, html_body=None, sender=None, sender_name=None, attachments_info=[]
):
if sender is None:
sender = app.config["MAIL_DEFAULT_SENDER"]

msg = SuperdeskMessage(subject=subject, sender=sender, recipients=to)
msg = SuperdeskMessage(subject=subject, sender=sender, recipients=to, cc=cc)
msg.body = text_body
msg.html = html_body
msg.attachments = [a["file_name"] for a in attachments_info]
Expand Down

0 comments on commit bfcf3a2

Please sign in to comment.