Skip to content

Commit

Permalink
uses new entry to retrieve data
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Feb 7, 2024
1 parent 9012d21 commit d0cde75
Showing 1 changed file with 44 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
</html>
"""


_NOTIFY_PAYMENTS_HTML = """
{% extends 'base.html' %}
Expand Down Expand Up @@ -91,10 +92,14 @@
"""


_NOTIFY_PAYMENTS_SUBJECT = "Your Payment {{ payment.price_dollars }} USD for {{ payment.osparc_credits }} Credits Was Successful"


_PRODUCT_NOTIFICATIONS_TEMPLATES = {
"base.html": _BASE_HTML,
"notify_payments.html": _NOTIFY_PAYMENTS_HTML,
"notify_payments.txt": _NOTIFY_PAYMENTS_TXT,
"notify_payments_subject.txt": _NOTIFY_PAYMENTS_SUBJECT,
}


Expand All @@ -120,36 +125,14 @@ class _PaymentData:
invoice_url: str


def _guess_file_type(file_path: Path) -> tuple[str, str]:
assert file_path.is_file()
mimetype, _encoding = mimetypes.guess_type(file_path)
if mimetype:
maintype, subtype = mimetype.split("/", maxsplit=1)
else:
maintype, subtype = "application", "octet-stream"
return maintype, subtype


def _add_attachments(msg: EmailMessage, file_paths: list[Path]):
for attachment_path in file_paths:
maintype, subtype = _guess_file_type(attachment_path)
msg.add_attachment(
attachment_path.read_bytes(),
filename=attachment_path.name,
maintype=maintype,
subtype=subtype,
)


async def _create_user_email(
env: Environment,
user: _UserData,
payment: PaymentTransaction,
payment: _PaymentData,
product: _ProductData,
) -> EmailMessage:
msg = EmailMessage()

# from/to
msg["From"] = Address(
display_name=f"{product.display_name} support",
addr_spec=product.support_email,
Expand All @@ -159,20 +142,13 @@ async def _create_user_email(
addr_spec=user.email,
)

# subject
msg[
"Subject"
] = f"Your Payment {payment.price_dollars:.2f} USD for {payment.osparc_credits:.2f} Credits Was Successful"
msg["Subject"] = env.get_template("notify_payments_subject.txt").render(data)

# body
data = {
"user": user,
"product": product,
"payment": _PaymentData(
price_dollars=f"{payment.price_dollars:.2f}",
osparc_credits=f"{payment.osparc_credits:.2f}",
invoice_url=payment.invoice_url,
),
"payment": payment,
}

text_template = env.get_template("notify_payments.txt")
Expand All @@ -183,6 +159,27 @@ async def _create_user_email(
return msg


def _guess_file_type(file_path: Path) -> tuple[str, str]:
assert file_path.is_file()
mimetype, _encoding = mimetypes.guess_type(file_path)
if mimetype:
maintype, subtype = mimetype.split("/", maxsplit=1)
else:
maintype, subtype = "application", "octet-stream"
return maintype, subtype


def _add_attachments(msg: EmailMessage, file_paths: list[Path]):
for attachment_path in file_paths:
maintype, subtype = _guess_file_type(attachment_path)
msg.add_attachment(
attachment_path.read_bytes(),
filename=attachment_path.name,
maintype=maintype,
subtype=subtype,
)


@asynccontextmanager
async def _create_email_session(
settings: SMTPSettings,
Expand Down Expand Up @@ -210,7 +207,6 @@ async def _create_email_session(


class EmailProvider(NotificationProvider):
# interfaces with the notification system
def __init__(self, settings: SMTPSettings, users_repo: PaymentsUsersRepo):
self._users_repo = users_repo
self._settings = settings
Expand All @@ -228,24 +224,24 @@ async def _create_message(
self, user_id: UserID, payment: PaymentTransaction
) -> EmailMessage:

# retrieve info
user = await self._users_repo.get_email_info(user_id)
product = _ProductData(
product_name="osparc",
display_name="o²S²PARC",
vendor_display_inline="IT'IS Foundation. Zeughausstrasse 43, 8004 Zurich, Switzerland ",
support_email="[email protected]",
)

# TODO: product? via wallet_id?
# TODO: ger support email and display name
data = await self._users_repo.get_notification_data(user_id, payment.payment_id)

# compose email
msg: EmailMessage = await _create_user_email(
self._jinja_env,
user=user,
payment=payment,
product=product,
user=_UserData(
first_name=data.first_name, last_name=data.last_name, email=data.email
),
payment=_PaymentData(
price_dollars=f"{payment.price_dollars:.2f}",
osparc_credits=f"{payment.osparc_credits:.2f}",
invoice_url=payment.invoice_url,
),
product=_ProductData(
product_name=data.product_name,
display_name=data.display_name,
vendor_display_inline=f"{data.vendor.get('name', '')}. Zeughausstrasse 43, 8004 Zurich, Switzerland ",
support_email=data.support_email,
),
)

return msg
Expand Down

0 comments on commit d0cde75

Please sign in to comment.