Skip to main content

Shared enums and optional PostgreSQL helpers for email workflows

Project description

wr-common-lib

Shared enums and optional PostgreSQL write helpers for the email table.

PyPI wr-common-lib
import wr_common_lib

Requires Python 3.12+.

Install

pip install wr-common-lib
pip install "wr-common-lib[db]"   # adds async-db-tools

Package layout

src/wr_common_lib/
├── __init__.py          # __version__
└── email/
    ├── __init__.py      # MailFlow, MailStatus, get_email_content_hash
    ├── constants.py     # MailFlow, MailStatus
    └── db_oper.py       # EmailDbOper, get_email_content_hash

Enums

MailFlow and MailStatus match PostgreSQL mail_flow / mail_status.

from wr_common_lib.email import MailFlow, MailStatus

MailFlow.INBOUND
MailStatus.PENDING.value

Status flows

Outbound:  PENDING → SENT | FAILED;webhook → DELIVERED | BOUNCED
Inbound:   RECEIVED → PARSED | PARSE_FAILED

Helpers

from wr_common_lib.email import get_email_content_hash

content_hash = get_email_content_hash(
    to="a@example.com,b@example.com",
    subject="...",
    content="...",
    cc="",
    attachments=[{"filename": "report.pdf"}],
)

get_email_content_hash normalizes to / cc (comma-separated), strips subject/content, and hashes attachment filenames only.

EmailDbOper

Requires the [db] extra. Pass an async-db-tools PostgresPool (or compatible pool with fetchval / execute).

from wr_common_lib.email import MailFlow, MailStatus, get_email_content_hash
from wr_common_lib.email.db_oper import EmailDbOper

db_oper = EmailDbOper(pool)

Write operations only; reads should use db_oper._db directly (e.g. fetchrow, fetchval).

insert_email

email_id, created = await db_oper.insert_email(
    task,
    MailFlow.OUTBOUND,
    MailStatus.PENDING,
)
# created is True when a new row was inserted, False when content_hash already existed

Task fields (inbound and outbound use the same shape):

Field Required Notes
imo yes
voyage_id yes UUID string
mail_from yes Sender
to yes Recipients (comma-separated allowed)
cc no Default ""
subject no Default ""
content no Default ""
attachments no List of dicts with filename
content_hash no Computed via get_email_content_hash when omitted
created_user_id no Outbound only

Stored as DB columns mail_to, mail_cc, attachment (jsonb).

On duplicate content_hash, the insert is skipped (ON CONFLICT DO NOTHING); the existing row id is returned and created is False.

Example:

task = {
    "imo": 1234567,
    "voyage_id": "550e8400-e29b-41d4-a716-446655440000",
    "mail_from": "ship@example.com",
    "to": "inbox@example.com",
    "cc": "",
    "subject": "Noon report",
    "content": "...",
    "attachments": [],
}

email_id, created = await db_oper.insert_email(
    task, MailFlow.INBOUND, MailStatus.RECEIVED
)

update_status

await db_oper.update_status(email_id, MailStatus.SENT)
await db_oper.update_status(email_id, MailStatus.DELIVERED)
await db_oper.update_status(email_id, MailStatus.FAILED)

update_parsed_data

Sets mail_status to PARSED and writes parsed_data (jsonb).

await db_oper.update_parsed_data(email_id, {"parsed": "..."})

Dependencies

install brings in
wr-common-lib
wr-common-lib[db] async-db-tools

Your application owns the database URL and pool lifecycle.

Publish

pip install -e ".[db]"

uv version 0.1.5          # bump before each PyPI release
export UV_PUBLISH_TOKEN=pypi-...
uv build && uv publish

PyPI does not allow re-uploading the same version.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wr_common_lib-0.1.5.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wr_common_lib-0.1.5-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file wr_common_lib-0.1.5.tar.gz.

File metadata

  • Download URL: wr_common_lib-0.1.5.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for wr_common_lib-0.1.5.tar.gz
Algorithm Hash digest
SHA256 ea031a173f66f472bcc86be7638a66ca31e3622a02bc2bf949f512684e2c8c7d
MD5 d4739377eacaccddf4926ee1eaf06947
BLAKE2b-256 d725bba910d0ba203be884ea616c24127474337d65c7fd4219f68b2631b064cb

See more details on using hashes here.

File details

Details for the file wr_common_lib-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: wr_common_lib-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for wr_common_lib-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9e966d52895b752add1c9d909c0bc9a187fd04b94296ab064c0b7fc1dc86d88c
MD5 d5386f7a2e48729a5013b8017ebd602b
BLAKE2b-256 f5c35c3d731c8e1ba048879c20a57cd8e5d5047b63eef4b3eb20e02c5a58467d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page