From 8a5b2e5ed1f7d7152d8f174e94bbc8796e5c2574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Wed, 27 Mar 2019 13:49:46 +0100 Subject: [PATCH] Add a worker to cancel old orders. --- src/bornhack/environment_settings.py.dist | 2 ++ src/bornhack/environment_settings.py.dist.dev | 3 ++ src/shop/email.py | 25 +++++++++++++++- src/shop/order_cleanup_worker.py | 30 +++++++++++++++++++ .../emails/order_cancelled_email.html | 11 +++++++ .../emails/order_cancelled_email.txt | 9 ++++++ 6 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/shop/order_cleanup_worker.py create mode 100644 src/shop/templates/emails/order_cancelled_email.html create mode 100644 src/shop/templates/emails/order_cancelled_email.txt diff --git a/src/bornhack/environment_settings.py.dist b/src/bornhack/environment_settings.py.dist index c62434b5..cc0389a7 100644 --- a/src/bornhack/environment_settings.py.dist +++ b/src/bornhack/environment_settings.py.dist @@ -94,3 +94,5 @@ ACCOUNTINGSYSTEM_EMAIL = "{{ django_accountingsystem_email }}" ECONOMYTEAM_EMAIL = "{{ django_economyteam_email }}" ECONOMYTEAM_NAME = "Economy" +ORDER_TTL = 24 +ORDER_TTL_UNIT = 'hours' diff --git a/src/bornhack/environment_settings.py.dist.dev b/src/bornhack/environment_settings.py.dist.dev index 3009d2c9..488767e4 100644 --- a/src/bornhack/environment_settings.py.dist.dev +++ b/src/bornhack/environment_settings.py.dist.dev @@ -78,3 +78,6 @@ CHANNEL_LAYERS = { } REIMBURSEMENT_MAIL = "reimbursement@example.com" + +ORDER_TTL = 30 +ORDER_TTL_UNIT = 'minutes' diff --git a/src/shop/email.py b/src/shop/email.py index ba1edef5..14042b9d 100644 --- a/src/shop/email.py +++ b/src/shop/email.py @@ -1,5 +1,9 @@ -from utils.email import add_outgoing_email import logging + +from django.conf import settings + +from utils.email import add_outgoing_email + 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): return add_outgoing_email( text_template='emails/testmail.txt', diff --git a/src/shop/order_cleanup_worker.py b/src/shop/order_cleanup_worker.py new file mode 100644 index 00000000..ff4e94c5 --- /dev/null +++ b/src/shop/order_cleanup_worker.py @@ -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) diff --git a/src/shop/templates/emails/order_cancelled_email.html b/src/shop/templates/emails/order_cancelled_email.html new file mode 100644 index 00000000..44650fe1 --- /dev/null +++ b/src/shop/templates/emails/order_cancelled_email.html @@ -0,0 +1,11 @@ +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
+
diff --git a/src/shop/templates/emails/order_cancelled_email.txt b/src/shop/templates/emails/order_cancelled_email.txt new file mode 100644 index 00000000..c530ee12 --- /dev/null +++ b/src/shop/templates/emails/order_cancelled_email.txt @@ -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