refactor email send class to account for item tasks - add cleanup of old items

pull/3/head
lance 5 years ago
parent d884436b42
commit 003da201d5

@ -2,23 +2,41 @@ from django.conf import settings
from django.core.mail import send_mail from django.core.mail import send_mail
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.urls import reverse from django.urls import reverse
from items.models import Item
from sales.models import ItemSale
class EmailTemplate: class EmailTemplate:
def __init__(self, item, scenario, role): def __init__(self, item, scenario, role):
context = { if isinstance(item, ItemSale):
'sale': item, context = {
'site_name': settings.SITE_NAME, 'sale': item,
'site_url': settings.SITE_URL, 'sale_path': reverse('get_sale', args=[item.id])
'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( 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, context=context,
request=None request=None
) )
body = render_to_string( 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, context=context,
request=None request=None
) )
@ -28,15 +46,10 @@ class EmailTemplate:
self.item = item self.item = item
def send(self): 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( res = send_mail(
self.subject, self.subject,
self.body, self.body,
settings.DEFAULT_FROM_EMAIL, settings.DEFAULT_FROM_EMAIL,
[to_address] [self.to_address]
) )
return res return res

@ -0,0 +1 @@
from items.tasks import cleanup

@ -0,0 +1,28 @@
from logging import getLogger
from datetime import timedelta
from django.utils import timezone as tz
from django.conf import settings
from huey import crontab
from huey.contrib.djhuey import periodic_task
from items.models import Item
from core.helpers.email_template import EmailTemplate
logger = getLogger('django.server')
@periodic_task(crontab(minute='0', hour='*/21'))
def close_stale_items():
time_delta = tz.now() - timedelta(days=settings.ESCROW_PERIOD_DAYS)
items = Item.objects.filter(list_date__lt=time_delta, available=True)
for item in items:
logger.info(f'[INFO] Found stale item #{item.id} (older than {settings.ESCROW_PERIOD_DAYS} days).')
if item.bids:
email_template = EmailTemplate(
item=item,
scenario='item_stale_with_bids',
role='seller'
)
email_template.send()

@ -11,8 +11,8 @@
<section> <section>
{% if not item.available %}<p>This item is currently pending sale. Bidding is temporarily closed.</p>{% endif %} {% if not item.available %}<p>This item is currently pending sale. Bidding is temporarily closed.</p>{% endif %}
<p class="sale-info"><strong>Whereabouts</strong>: <a href="https://duckduckgo.com/?q={{ item.whereabouts }}" target="_blank">{{ item.whereabouts }}</a></p> <p class="sale-info"><strong>Whereabouts</strong>: <a href="https://duckduckgo.com/?q={{ item.whereabouts }}" target="_blank">{{ item.whereabouts }}</a></p>
<p class="sale-info"><strong>Creation</strong>: {{ item.list_date | date:"d M Y H:i:s" }}</p> <p class="sale-info"><strong>Creation</strong>: {{ item.list_date | date:"d M Y H:i:s e" }}</p>
<p class="sale-info"><strong>Last Updated</strong>: {{ item.last_updated | date:"d M Y H:i:s" }}</p> <p class="sale-info"><strong>Last Updated</strong>: {{ item.last_updated | date:"d M Y H:i:s e" }}</p>
<p class="sale-info"><strong>Description</strong>: {{ item.description }}</p> <p class="sale-info"><strong>Description</strong>: {{ item.description }}</p>
<p class="sale-info"><strong>Asking Price (XMR)</strong>: {{ item.ask_price_xmr }}</p> <p class="sale-info"><strong>Asking Price (XMR)</strong>: {{ item.ask_price_xmr }}</p>
{% for img in item_images %} {% for img in item_images %}

@ -0,0 +1,9 @@
Bad News {{ item.owner }},
Your item, "{{ item.name }}" (#{{ item.id }}), has been listed on the auction house for over {{ escrow_period }} days. There are currently {{ item.bids.values | length }} bid(s) posted to it.
We'll provide you with a few more hours to either accept a bid or delete the item from the auction. Please do something soon.
https://{{ site_url }}{{ item_path }}
Thanks for using {{ site_name }}!

@ -0,0 +1 @@
[{{ site_name }}] Item Stale! (#{{ item.id }})

@ -6,6 +6,7 @@ def inject_site_meta(request):
'site_meta': { 'site_meta': {
'debug': settings.DEBUG, 'debug': settings.DEBUG,
'name': settings.SITE_NAME, 'name': settings.SITE_NAME,
'escrow_period': settings.ESCROW_PERIOD_DAYS,
'tip_address': settings.TIP_WALLET_ADDRESS, 'tip_address': settings.TIP_WALLET_ADDRESS,
'platform_address': settings.PLATFORM_WALLET_ADDRESS 'platform_address': settings.PLATFORM_WALLET_ADDRESS
} }