Skip to content

Commit

Permalink
feature: add config to stop saving emails to dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Sep 15, 2023
1 parent 06c07f6 commit 98a152c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
8 changes: 8 additions & 0 deletions ckanext/mailcraft/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
CONF_MAIL_PER_PAGE = "ckanext.mailcraft.mail_per_page"
DEF_MAIL_PER_PAGE = 20

CONF_SAVE_TO_DASHBOARD = "ckanext.mailcraft.save_to_dashboard"
DEF_SAVE_TO_DASHBOARD = False


def get_conn_timeout() -> int:
"""Return a timeout for an SMTP connection"""
Expand All @@ -33,3 +36,8 @@ def stop_outgoing_emails() -> bool:
def get_mail_per_page() -> int:
"""Return a number of mails to show per page"""
return tk.asint(tk.config.get(CONF_MAIL_PER_PAGE, DEF_MAIL_PER_PAGE))


def is_save_to_dashboard_enabled() -> bool:
"""Check if we are saving outgoing emails to dashboard"""
return tk.asbool(tk.config.get(CONF_SAVE_TO_DASHBOARD, DEF_SAVE_TO_DASHBOARD))
5 changes: 5 additions & 0 deletions ckanext/mailcraft/config_declaration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ groups:
type: int
description: Specify a number of mails to show per page on a dashboard
default: 20

- key: ckanext.mailcraft.save_to_dashboard
type: bool
description: Specify do we need to save outgoing emails to dashboard
default: false
27 changes: 15 additions & 12 deletions ckanext/mailcraft/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def create_reset_key(self, user: model.User) -> None:
def verify_reset_link(self, user: model.User, key: Optional[str]) -> bool:
pass

def save_to_dashboard(
self,
msg: EmailMessage,
body_html: str,
state: str = mc_model.Email.State.success,
extras: Optional[dict[str, Any]] = None,
) -> None:
if not mc_config.is_save_to_dashboard_enabled():
return

mc_model.Email.save_mail(msg, body_html, state, extras or {})


class DefaultMailer(Mailer):
def mail_recipients(
Expand Down Expand Up @@ -130,18 +142,18 @@ def mail_recipients(

try:
if mc_config.stop_outgoing_emails():
self._save_email(
self.save_to_dashboard(
msg, body_html, mc_model.Email.State.stopped, dict(msg.items())
)
else:
self._send_email(recipients, msg)
except MailerException:
self._save_email(
self.save_to_dashboard(
msg, body_html, mc_model.Email.State.failed, dict(msg.items())
)
else:
if not mc_config.stop_outgoing_emails():
self._save_email(msg, body_html)
self.save_to_dashboard(msg, body_html)

def add_attachments(self, msg: EmailMessage, attachments) -> None:
"""Add attachments on an email message
Expand Down Expand Up @@ -198,15 +210,6 @@ def get_connection(self) -> smtplib.SMTP:

return conn

def _save_email(
self,
msg: EmailMessage,
body_html: str,
state: str = mc_model.Email.State.success,
extras: Optional[dict[str, Any]] = None,
) -> None:
mc_model.Email.save_mail(msg, body_html, state, extras or {})

def _send_email(self, recipients, msg: EmailMessage):
conn = self.get_connection()

Expand Down

0 comments on commit 98a152c

Please sign in to comment.