@ -2,23 +2,41 @@ from django.conf import settings
from django . core . mail import send_mail
from django . template . loader import render_to_string
from django . urls import reverse
from items . models import Item
from sales . models import ItemSale
class EmailTemplate :
def __init__ ( self , item , scenario , role ) :
context = {
' sale ' : item ,
' site_name ' : settings . SITE_NAME ,
' site_url ' : settings . SITE_URL ,
' sale_path ' : reverse ( ' get_sale ' , args = [ item . id ] )
}
if isinstance ( item , ItemSale ) :
context = {
' sale ' : item ,
' sale_path ' : reverse ( ' get_sale ' , args = [ item . id ] )
}
tpl_path = ' sales '
if role == ' buyer ' :
self . to_address = item . bid . bidder . email
else :
self . to_address = item . item . owner . email
elif isinstance ( item , Item ) :
context = {
' item ' : item ,
' item_path ' : reverse ( ' get_item ' , args = [ item . id ] )
}
tpl_path = ' items '
self . to_address = item . owner . email
context [ ' site_name ' ] = settings . SITE_NAME
context [ ' site_url ' ] = settings . SITE_URL
context [ ' escrow_period ' ] = settings . ESCROW_PERIOD_DAYS
subject = render_to_string (
template_name = f ' sales/notify/ { scenario } / { role } /subject.txt ' ,
template_name = f ' { tpl_path } /notify/{ scenario } / { role } /subject.txt ' ,
context = context ,
request = None
)
body = render_to_string (
template_name = f ' sales/notify/ { scenario } / { role } /body.txt ' ,
template_name = f ' { tpl_path } /notify/{ scenario } / { role } /body.txt ' ,
context = context ,
request = None
)
@ -28,15 +46,10 @@ class EmailTemplate:
self . item = item
def send ( self ) :
if self . role == ' buyer ' :
to_address = self . item . bid . bidder . email
else :
to_address = self . item . item . owner . email
res = send_mail (
self . subject ,
self . body ,
settings . DEFAULT_FROM_EMAIL ,
[ to_address ]
[ self . to_address ]
)
return res