adding more tasks for cleaning up items and notifying
parent
5172433857
commit
aba60012d2
@ -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 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
|
||||
from items.tasks.common import get_items_past_days
|
||||
|
||||
|
||||
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)
|
||||
|
||||
@periodic_task(crontab(minute='0', hour='0', day='*'))
|
||||
def delete_expired_items():
|
||||
expired_days = settings.ESCROW_PERIOD_DAYS
|
||||
items = get_items_past_days(expired_days)
|
||||
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()
|
||||
|
||||
|
||||
logger.info(f'[INFO] Found expired item #{item.id} (older than {expired_days} days).')
|
||||
item.delete()
|
||||
|
@ -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
|
Reference in New Issue