Skip to content

Commit

Permalink
Added some type annotations, code tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart van der Schoor committed Feb 28, 2024
1 parent 95cca1d commit e409ac0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions mail_editor/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ def __init__(self, *args, **kwargs):
domain = current_site.domain
except Exception as e:
domain = ""

self.fields["body"].initial = template.render({"domain": domain}, None)
8 changes: 4 additions & 4 deletions mail_editor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ def build_message(
email_message.attach(*attachment)

if cid_attachments:
for cid, content, subtype in cid_attachments:
subtype = subtype.split("/", maxsplit=1)
for att in cid_attachments:
subtype = att.content_type.split("/", maxsplit=1)
assert subtype[0] == "image"
mime_image = MIMEImage(content, _subtype=subtype[1])
mime_image.add_header("Content-ID", f"<{cid}>")
mime_image = MIMEImage(att.content, _subtype=subtype[1])
mime_image.add_header("Content-ID", f"<{att.cid}>")
email_message.attach(mime_image)

return email_message
Expand Down
33 changes: 23 additions & 10 deletions mail_editor/process.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import hashlib
import os
from mimetypes import guess_type
from typing import NamedTuple, Optional
from urllib.parse import urlparse

from django.conf import settings
Expand All @@ -23,7 +24,7 @@
]


def _html_inline_css(html):
def _html_inline_css(html: str) -> str:
inliner = css_inline.CSSInliner(
inline_style_tags=True,
keep_style_tags=False,
Expand All @@ -45,7 +46,18 @@ def _html_inline_css(html):
return html


def process_html(html, base_url, extract_attachments=True, inline_css=True):
class CIDAttachment(NamedTuple):
cid: str
content: bytes
content_type: str


def process_html(
html: str,
base_url: str,
extract_attachments: bool = True,
inline_css: bool = True,
) -> tuple[str, list[CIDAttachment]]:
# TODO handle errors in cosmetics and make sure we always produce something
parser = etree.HTMLParser()
root = etree.fromstring(html, parser)
Expand Down Expand Up @@ -113,20 +125,21 @@ def process_html(html, base_url, extract_attachments=True, inline_css=True):
result = _html_inline_css(result)

attachments = [
(cid, content, ct) for cid, (content, ct) in image_attachments.items()
CIDAttachment(cid, content, ct)
for cid, (content, ct) in image_attachments.items()
]

return result, attachments


def cid_for_bytes(content):
def cid_for_bytes(content: bytes) -> str:
# hash content for de-duplication
h = hashlib.sha1(usedforsecurity=False)
h.update(content)
return h.hexdigest()


def read_image_file(path):
def read_image_file(path: str) -> tuple[Optional[bytes], str]:
try:
with open(path, "rb") as f:
content = f.read()
Expand All @@ -140,22 +153,22 @@ def read_image_file(path):
return None, ""


def find_static_path_for_inliner(url, static_url):
def find_static_path_for_inliner(url: str, static_url: str) -> Optional[str]:
if url.startswith(static_url):
file_name = url[len(static_url) :]
file_path = os.path.join(settings.STATIC_ROOT, file_name)
if os.path.exists(file_path):
return os.path.relpath(file_path, start=FILE_ROOT)
return str(os.path.relpath(file_path, start=FILE_ROOT))
else:
file_path = finders.find(file_name)
if file_path:
return os.path.relpath(file_path, start=FILE_ROOT)
return str(os.path.relpath(file_path, start=FILE_ROOT))

# we don't allow external links
return None


def load_image(url, base_url):
def load_image(url: str, base_url: str) -> tuple[Optional[bytes], str]:
# TODO support data urls? steal from mailcleaner
static_url = make_url_absolute(settings.STATIC_URL, base_url)
media_url = make_url_absolute(settings.MEDIA_URL, base_url)
Expand Down Expand Up @@ -195,7 +208,7 @@ def load_image(url, base_url):
return content, content_type


def make_url_absolute(url, base_url=""):
def make_url_absolute(url: str, base_url: str = "") -> str:
"""
base_url: https://domain
"""
Expand Down
2 changes: 1 addition & 1 deletion mail_editor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def UNIQUE_LANGUAGE_TEMPLATES(self):
settings = Settings()


def get_choices():
def get_choices() -> list[tuple[str, str]]:
choices = []
for key, values in settings.TEMPLATES.items():
choices += [(key, values.get("name", key.title()))]
Expand Down

0 comments on commit e409ac0

Please sign in to comment.