Skip to content

Commit

Permalink
feature: add extras field to store msg headers
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Sep 14, 2023
1 parent 8fc28cc commit 4859187
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
14 changes: 8 additions & 6 deletions ckanext/mailcraft/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ def mail_recipients(
self.add_attachments(msg, attachments)

try:
import ipdb

ipdb.set_trace()
# print(msg.get_body(("html",)).get_content()) # type: ignore
if mc_config.stop_outgoing_emails():
self._save_email(msg, body_html, state=mc_model.Email.State.stopped)
self._save_email(
msg, body_html, mc_model.Email.State.stopped, dict(msg.items())
)
else:
self._send_email(recipients, msg)
except MailerException:
self._save_email(msg, body_html, state=mc_model.Email.State.failed)
self._save_email(
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)
Expand Down Expand Up @@ -188,8 +189,9 @@ def _save_email(
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)
mc_model.Email.save_mail(msg, body_html, state, extras or {})

def _send_email(self, recipients, msg: EmailMessage):
conn = self.get_connection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
Create Date: 2023-09-14 11:53:58.371935
"""
from alembic import op
import sqlalchemy as sa

from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "9f83e5bfff72"
Expand All @@ -31,6 +32,7 @@ def upgrade():
sa.Column("recipient", sa.Text),
sa.Column("message", sa.Text),
sa.Column("state", sa.Text, server_default="success"),
sa.Column("extras", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
)


Expand Down
7 changes: 6 additions & 1 deletion ckanext/mailcraft/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from sqlalchemy import Column, DateTime, Integer, Text
from sqlalchemy.orm import Query
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.ext.mutable import MutableDict
from typing_extensions import Self

import ckan.model as model
Expand All @@ -31,6 +33,7 @@ class State:
recipient = Column(Text)
message = Column(Text)
state = Column(Text, nullable=False, default=State.success)
extras = Column("extras", MutableDict.as_mutable(JSONB))

@classmethod
def all(cls) -> list[dict[str, Any]]:
Expand All @@ -39,14 +42,15 @@ def all(cls) -> list[dict[str, Any]]:
return [mail.dictize({}) for mail in query.all()]

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

model.Session.add(mail)
Expand All @@ -63,6 +67,7 @@ def dictize(self, context):
"recipient": self.recipient,
"message": self.message,
"state": self.state,
"extras": self.extras or {}
}

@classmethod
Expand Down
13 changes: 13 additions & 0 deletions ckanext/mailcraft/templates/mailcraft/mail_read.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@
{% endblock breadcrumb_content %}

{% block ap_content %}
{% if mail.extras %}
<div class="mailcraft-mail-meta">
<h3>{{ _("Email headers") }}</h3>
<ul>
{% for key, value in mail.extras.items() %}
<li><b>{{ key }}</b>: {{ value }}</li>
{% endfor %}
</ul>
</div>
{% endif %}

<div class="mailcraft-mail-body">
<h3>{{ _("Email body") }}</h3>

{{ mail.message | safe}}
</div>
{% endblock ap_content %}

0 comments on commit 4859187

Please sign in to comment.