Add a worker to cancel old orders.

This commit is contained in:
Víðir Valberg Guðmundsson 2019-03-27 13:49:46 +01:00
parent 39383e0acb
commit 8a5b2e5ed1
6 changed files with 79 additions and 1 deletions

View file

@ -94,3 +94,5 @@ ACCOUNTINGSYSTEM_EMAIL = "{{ django_accountingsystem_email }}"
ECONOMYTEAM_EMAIL = "{{ django_economyteam_email }}" ECONOMYTEAM_EMAIL = "{{ django_economyteam_email }}"
ECONOMYTEAM_NAME = "Economy" ECONOMYTEAM_NAME = "Economy"
ORDER_TTL = 24
ORDER_TTL_UNIT = 'hours'

View file

@ -78,3 +78,6 @@ CHANNEL_LAYERS = {
} }
REIMBURSEMENT_MAIL = "reimbursement@example.com" REIMBURSEMENT_MAIL = "reimbursement@example.com"
ORDER_TTL = 30
ORDER_TTL_UNIT = 'minutes'

View file

@ -1,5 +1,9 @@
from utils.email import add_outgoing_email
import logging import logging
from django.conf import settings
from utils.email import add_outgoing_email
logger = logging.getLogger("bornhack.%s" % __name__) logger = logging.getLogger("bornhack.%s" % __name__)
@ -45,6 +49,25 @@ def add_invoice_email(invoice):
) )
def add_order_cancelled_email(order):
formatdict = {
'ordernumber': order.pk,
'order_ttl': settings.ORDER_TTL,
'order_ttl_unit': settings.ORDER_TTL_UNIT
}
subject = 'Your non-paid BornHack order has been cancelled.'
# add email to outgoing email queue
return add_outgoing_email(
text_template='emails/order_cancelled_email.txt',
html_template='emails/order_cancelled_email.html',
to_recipients=order.user.email,
formatdict=formatdict,
subject=subject,
)
def add_test_email(recipient): def add_test_email(recipient):
return add_outgoing_email( return add_outgoing_email(
text_template='emails/testmail.txt', text_template='emails/testmail.txt',

View file

@ -0,0 +1,30 @@
from dateutil import relativedelta
from django.conf import settings
from django.utils import timezone
from shop.models import Order
from shop.email import add_order_cancelled_email
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('bornhack.%s' % __name__)
def do_work():
"""
The order cleanup worker scans for orders that are still open
but are older than ORDER_TTL, and marks those as closed.
"""
time_threshold = timezone.now() - relativedelta.relativedelta(**{settings.ORDER_TTL_UNIT: settings.ORDER_TTL})
orders_to_delete = Order.objects.filter(open=True, cancelled=False, created__lt=time_threshold)
for order in orders_to_delete:
logger.info(
"Cancelling order %s since it has been open for more than %s %s" %
(order.pk, settings.ORDER_TTL, settings.ORDER_TTL_UNIT)
)
order.mark_as_cancelled()
add_order_cancelled_email(order)

View file

@ -0,0 +1,11 @@
Hello!<br>
<br>
Your order number {{ ordernumber }} has been open for more than {{ order_ttl }} {{ order_ttl_unit }} and has been cancelled.<br>
<br>
This means you will have to open a new order.<br>
<br>
<br>
Best regards,<br>
<br>
The BornHack Team<br>
<br>

View file

@ -0,0 +1,9 @@
Hello!
Your order number {{ ordernumber }} has been open for more than {{ order_ttl }} {{ order_ttl_unit }} and has been cancelled.
This means you will have to open a new order.
Best regards,
The BornHack Team