Skip to content

Commit

Permalink
feature: create config page, add redirect option part2
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Jan 4, 2024
1 parent 413b68b commit 5cce7b0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 25 deletions.
6 changes: 2 additions & 4 deletions ckanext/mailcraft/config_declaration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ groups:
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
- key: ckanext.mailcraft.redirect_emails_to
description: Redirect outgoing emails to a specified email
9 changes: 9 additions & 0 deletions ckanext/mailcraft/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ def mc_mail_delete(context, data_dict):
context["session"].commit()

return True


def mc_mail_clear(context, data_dict):
"""Clear all stored mails"""
tk.check_access("mc_mail_delete", context, data_dict)

emails_deleted = mc_model.Email.clear_emails()

return {"deleted": emails_deleted}
30 changes: 17 additions & 13 deletions ckanext/mailcraft/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Attachment,
AttachmentWithoutType,
AttachmentWithType,
EmailData,
)

log = logging.getLogger(__name__)
Expand All @@ -38,6 +39,8 @@ def __init__(self):
self.site_url = tk.config["ckan.site_url"]

self.conn_timeout = mc_config.get_conn_timeout()
self.stop_outgoing = mc_config.stop_outgoing_emails()
self.redirect_to = mc_config.get_redirect_email()

@abstractmethod
def mail_recipients(
Expand Down Expand Up @@ -113,21 +116,23 @@ def mail_recipients(
if attachments:
self.add_attachments(msg, attachments)

email_data: EmailData = dict(msg.items()) # type: ignore

try:
# print(msg.get_body(("html",)).get_content()) # type: ignore
if mc_config.stop_outgoing_emails():
self._save_email(
msg, body_html, mc_model.Email.State.stopped, dict(msg.items())
)
if self.stop_outgoing:
self._save_email(email_data, body_html, mc_model.Email.State.stopped)
else:
if recipient := mc_config.get_redirect_email():
email_data["redirected_from"] = recipients
recipients = [recipient]
email_data["To"] = email_data["Bcc"] = ", ".join(recipients)

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

def add_attachments(self, msg: EmailMessage, attachments) -> None:
"""Add attachments on an email message
Expand Down Expand Up @@ -186,12 +191,11 @@ def get_connection(self) -> smtplib.SMTP:

def _save_email(
self,
msg: EmailMessage,
email_data: EmailData,
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 {})
mc_model.Email.save_mail(email_data, body_html, state)

def _send_email(self, recipients, msg: EmailMessage):
conn = self.get_connection()
Expand Down
18 changes: 11 additions & 7 deletions ckanext/mailcraft/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
from datetime import datetime
from typing import Any
from email.message import EmailMessage

from sqlalchemy import Column, DateTime, Integer, Text
from sqlalchemy.orm import Query
Expand All @@ -14,6 +13,8 @@
import ckan.model as model
from ckan.plugins import toolkit as tk

from ckanext.mailcraft.types import EmailData

log = logging.getLogger(__name__)


Expand Down Expand Up @@ -43,16 +44,19 @@ def all(cls) -> list[dict[str, Any]]:

@classmethod
def save_mail(
cls, msg: EmailMessage, body_html: str, state: str, extras: dict[str, Any]
cls,
email_data: EmailData,
body_html: str,
state: str,
) -> Email:
mail = cls(
subject=msg["Subject"],
timestamp=msg["Date"],
sender=msg["From"],
recipient=msg["To"],
subject=email_data["Subject"],
timestamp=email_data["Date"],
sender=email_data["From"],
recipient=email_data["To"],
message=body_html,
state=state,
extras=extras,
extras=email_data,
)

model.Session.add(mail)
Expand Down
2 changes: 1 addition & 1 deletion ckanext/mailcraft/templates/mailcraft/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h1>{{ _("Mailcraft configuration") }}</h1>
<form method="POST">
{% for _, config in configs.items() %}
{% if config.type == "select" %}
{{ form.select(config.key, label=config.label, options=config.options, selected=data[config.key] | o if data else config.value, error=errors[config.key]) }}
{{ form.select(config.key, label=config.label, options=config.options, selected=data[config.key] | int if data else config.value, error=errors[config.key]) }}
{% elif config.type in ("text", "number") %}
{{ form.input(config.key, label=config.label, value=data[config.key] if data else config.value, error=errors[config.key], type=config.type) }}
{% else %}
Expand Down
7 changes: 7 additions & 0 deletions ckanext/mailcraft/templates/mailcraft/emails/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "mailcraft/emails/base.html" %}

{% block main_text %}

Test

{% endblock %}
18 changes: 18 additions & 0 deletions ckanext/mailcraft/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
from __future__ import annotations

from typing import IO, Tuple, Union
from typing_extensions import NotRequired, TypedDict


AttachmentWithType = Union[Tuple[str, IO[str], str], Tuple[str, IO[bytes], str]]
AttachmentWithoutType = Union[Tuple[str, IO[str]], Tuple[str, IO[bytes]]]
Attachment = Union[AttachmentWithType, AttachmentWithoutType]


EmailData = TypedDict(
"EmailData",
{
"Bcc": str,
"Content-Type": NotRequired[str],
"Date": str,
"From": str,
"MIME-Version": NotRequired[str],
"Subject": str,
"To": str,
"X-Mailer": NotRequired[str],
"redirected_from": NotRequired["list[str]"]
},
)

0 comments on commit 5cce7b0

Please sign in to comment.