adding more tasks for cleaning up items and notifying

pull/3/head
lance 5 years ago
parent 5172433857
commit aba60012d2

@ -29,6 +29,7 @@ class EmailTemplate:
context['site_name'] = settings.SITE_NAME context['site_name'] = settings.SITE_NAME
context['site_url'] = settings.SITE_URL context['site_url'] = settings.SITE_URL
context['escrow_period'] = settings.ESCROW_PERIOD_DAYS context['escrow_period'] = settings.ESCROW_PERIOD_DAYS
context['stale_period'] = settings.STALE_PERIOD_DAYS
subject = render_to_string( subject = render_to_string(
template_name=f'{tpl_path}/notify/{scenario}/{role}/subject.txt', template_name=f'{tpl_path}/notify/{scenario}/{role}/subject.txt',

@ -0,0 +1,68 @@
from secrets import token_urlsafe
from monero.seed import Seed
from django.test import TestCase
from django.contrib.auth.models import User
from django.test.client import Client
from django.shortcuts import reverse
from bids.models import ItemBid
from items.models import Item
from core.helpers.email_template import EmailTemplate
from sales.models import ItemSale
class ItemBidModelsTestCase(TestCase):
def setUp(self):
self.client = Client()
self.seller_password = token_urlsafe(32)
self.buyer_password = token_urlsafe(32)
self.seller = User.objects.create_user(
'seller', self.seller_password
)
self.buyer = User.objects.create_user(
'buyer', self.buyer_password
)
self.payout_address = Seed().public_address(net='stagenet')
self.return_address = Seed().public_address(net='stagenet')
self.whereabouts = 'Los Angeles, CA'
self.test_item = Item.objects.create(
owner=self.seller,
name='Test Item',
description='Test item',
ask_price_xmr=0.3,
payout_address=self.payout_address,
whereabouts=self.whereabouts
)
def test_email_template_helper_sends_successfully_for_(self):
e = EmailTemplate(
item=self.test_item,
scenario='item_has_bids',
role='seller'
)
res = e.send()
self.assertEqual(res, 1)
def test_email_template_helper_sends_successfully_for_aaa(self):
new_bid = ItemBid.objects.create(
item=self.test_item,
bidder=self.buyer,
bid_price_xmr=0.1,
return_address=self.return_address
)
self.client.login(username=self.seller.username, password=self.seller_password)
response = self.client.get(reverse('accept_bid', args=[new_bid.id]))
item_sale = ItemSale.objects.filter(item=self.test_item, bid=new_bid).first()
updated_bid = ItemBid.objects.get(id=new_bid.id)
self.assertTrue(updated_bid.accepted)
self.assertFalse(updated_bid.item.available)
e = EmailTemplate(
item=item_sale,
scenario='item_shipped',
role='buyer'
)
res = e.send()
self.assertEqual(res, 1)

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

@ -0,0 +1,10 @@
from datetime import timedelta
from django.utils import timezone as tz
from django.conf import settings
from items.models import Item
def get_items_past_days(days=settings.ESCROW_PERIOD_DAYS) -> list:
time_delta = tz.now() - timedelta(days=days)
items = Item.objects.filter(list_date__lt=time_delta, available=True)
return items

@ -1,8 +1,10 @@
from logging import getLogger from logging import getLogger
from huey import crontab from huey import crontab
from huey.contrib.djhuey import periodic_task from huey.contrib.djhuey import periodic_task
from django.conf import settings
from core.helpers.email_template import EmailTemplate from core.helpers.email_template import EmailTemplate
from items.models import Item from items.models import Item
from items.tasks.common import get_items_past_days
logger = getLogger('django.server') logger = getLogger('django.server')
@ -19,3 +21,17 @@ def notify_seller_of_item_bids():
role='seller' role='seller'
) )
email_template.send() email_template.send()
@periodic_task(crontab(minute='0', hour='0', day='*/3'))
def notify_seller_of_stale_items():
stale_days = settings.ESCROW_PERIOD_DAYS - 7
items = get_items_past_days(stale_days)
for item in items:
logger.info(f'[INFO] Found stale item #{item.id} (older than {stale_days} days).')
if item.bids:
email_template = EmailTemplate(
item=item,
scenario='item_stale_with_bids',
role='seller'
)
email_template.send()

@ -1,6 +1,6 @@
Bad News {{ item.owner }}, 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. Your item, "{{ item.name }}" (#{{ item.id }}), has been listed on the auction house for over {{ stale_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. 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.

@ -27,6 +27,7 @@ DEBUG = os.environ.get('DEBUG', False)
ALLOWED_HOSTS = str(os.environ['ALLOWED_HOSTS']).split(',') ALLOWED_HOSTS = str(os.environ['ALLOWED_HOSTS']).split(',')
ALLOWED_HOSTS.append('gunicorn') ALLOWED_HOSTS.append('gunicorn')
ESCROW_PERIOD_DAYS = os.environ.get('ESCROW_PERIOD_DAYS', 30) ESCROW_PERIOD_DAYS = os.environ.get('ESCROW_PERIOD_DAYS', 30)
STALE_PERIOD_DAYS = os.environ.get('STALE_PERIOD_DAYS', 21)
PLATFORM_FEE_PERCENT = os.environ.get('PLATFORM_FEE_PERCENT', 0) PLATFORM_FEE_PERCENT = os.environ.get('PLATFORM_FEE_PERCENT', 0)
BLOCK_CONFIRMATIONS_RCV = os.environ.get('BLOCK_CONFIRMATIONS_RCV', 3) BLOCK_CONFIRMATIONS_RCV = os.environ.get('BLOCK_CONFIRMATIONS_RCV', 3)
BLOCK_EXPLORER = 'https://community.xmr.to/explorer/%s' BLOCK_EXPLORER = 'https://community.xmr.to/explorer/%s'