diff --git a/core/helpers/email_template.py b/core/helpers/email_template.py index 2adf5ab..05b7325 100644 --- a/core/helpers/email_template.py +++ b/core/helpers/email_template.py @@ -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 \ No newline at end of file diff --git a/items/tasks/__init__.py b/items/tasks/__init__.py new file mode 100644 index 0000000..cb71453 --- /dev/null +++ b/items/tasks/__init__.py @@ -0,0 +1 @@ +from items.tasks import cleanup \ No newline at end of file diff --git a/items/tasks/cleanup.py b/items/tasks/cleanup.py new file mode 100644 index 0000000..3f89b58 --- /dev/null +++ b/items/tasks/cleanup.py @@ -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() + + diff --git a/web/templates/items/get_item.html b/web/templates/items/get_item.html index 7cdaaf0..0589daa 100644 --- a/web/templates/items/get_item.html +++ b/web/templates/items/get_item.html @@ -11,8 +11,8 @@
{% if not item.available %}

This item is currently pending sale. Bidding is temporarily closed.

{% endif %}

Whereabouts: {{ item.whereabouts }}

-

Creation: {{ item.list_date | date:"d M Y H:i:s" }}

-

Last Updated: {{ item.last_updated | date:"d M Y H:i:s" }}

+

Creation: {{ item.list_date | date:"d M Y H:i:s e" }}

+

Last Updated: {{ item.last_updated | date:"d M Y H:i:s e" }}

Description: {{ item.description }}

Asking Price (XMR): {{ item.ask_price_xmr }}

{% for img in item_images %} diff --git a/web/templates/items/notify/item_stale_with_bids/seller/body.txt b/web/templates/items/notify/item_stale_with_bids/seller/body.txt new file mode 100644 index 0000000..0acd236 --- /dev/null +++ b/web/templates/items/notify/item_stale_with_bids/seller/body.txt @@ -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 }}! diff --git a/web/templates/items/notify/item_stale_with_bids/seller/subject.txt b/web/templates/items/notify/item_stale_with_bids/seller/subject.txt new file mode 100644 index 0000000..029879c --- /dev/null +++ b/web/templates/items/notify/item_stale_with_bids/seller/subject.txt @@ -0,0 +1 @@ +[{{ site_name }}] Item Stale! (#{{ item.id }}) diff --git a/xmrauctions/context_processors.py b/xmrauctions/context_processors.py index dc9c287..2a627f0 100644 --- a/xmrauctions/context_processors.py +++ b/xmrauctions/context_processors.py @@ -6,6 +6,7 @@ def inject_site_meta(request): 'site_meta': { 'debug': settings.DEBUG, 'name': settings.SITE_NAME, + 'escrow_period': settings.ESCROW_PERIOD_DAYS, 'tip_address': settings.TIP_WALLET_ADDRESS, 'platform_address': settings.PLATFORM_WALLET_ADDRESS }