cmsmanage/membershipworks/invoice_email.py

63 lines
2.2 KiB
Python
Raw Permalink Normal View History

from django.conf import settings
from django.core.mail import EmailMessage
from cmsmanage.email import TemplatedMultipartEmail
from membershipworks.models import EventInvoice
class InvoiceEmailBase(TemplatedMultipartEmail):
from_email = "CMS Invoices <invoices@claremontmakerspace.org>"
reply_to = ["Claremont MakerSpace <Info@ClaremontMakerSpace.org>"]
class InstructorInvoiceEmail(InvoiceEmailBase):
template = "membershipworks/email/event_invoice_instructor.dj.html"
@property
def subject(self) -> str:
event = self.context["invoice"].event
return f'Your CMS instructor invoice has been received for event "{event}" {event.start} - {event.end}'
@classmethod
def render_for_invoice(
cls, invoice: EventInvoice, pdf: bytes, event_url: str
) -> EmailMessage:
if invoice.event.instructor is None or invoice.event.instructor.member is None:
raise ValueError("Event Instructor not defined or is not member")
message = cls.render(
{"invoice": invoice, "event_url": event_url},
to=[invoice.event.instructor.member.sanitized_mailbox()],
)
message.attach(f"CMS_event_invoice_{invoice.uuid}.pdf", pdf, "application/pdf")
return message
class AdminInvoiceEmail(InvoiceEmailBase):
template = "membershipworks/email/event_invoice_admin.dj.html"
@property
def subject(self) -> str:
event = self.context["invoice"].event
return f'CMS instructor invoice created for event "{event}" {event.start} - {event.end}'
@classmethod
def render_for_invoice(
cls, invoice: EventInvoice, pdf: bytes, event_url: str
) -> EmailMessage:
message = cls.render(
{"invoice": invoice, "event_url": event_url},
# TODO: should this be in database instead?
to=settings.INVOICE_HANDLERS,
)
message.attach(f"CMS_event_invoice_{invoice.uuid}.pdf", pdf, "application/pdf")
return message
def make_invoice_emails(
invoice: EventInvoice, pdf: bytes, event_url: str
) -> list[EmailMessage]:
return [
InstructorInvoiceEmail.render_for_invoice(invoice, pdf, event_url),
AdminInvoiceEmail.render_for_invoice(invoice, pdf, event_url),
]