From bb5c3e2fecf3fdf16dee3991dc96ccb4518601ad Mon Sep 17 00:00:00 2001 From: Francois Lamontagne Date: Mon, 11 Dec 2017 13:39:59 +0700 Subject: [PATCH 1/2] command to set orders status to Delivered --- .../management/commands/setordersdelivered.py | 38 ++++++++++++++++ src/order/models.py | 1 + src/order/tests.py | 43 ++++++++++++++++++- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/order/management/commands/setordersdelivered.py diff --git a/src/order/management/commands/setordersdelivered.py b/src/order/management/commands/setordersdelivered.py new file mode 100644 index 00000000..0cd67573 --- /dev/null +++ b/src/order/management/commands/setordersdelivered.py @@ -0,0 +1,38 @@ +from django.core.management.base import BaseCommand +from order.models import Order, ORDER_STATUS_ORDERED, ORDER_STATUS_DELIVERED +from datetime import datetime +from django.contrib.admin.models import LogEntry, ADDITION + + +class Command(BaseCommand): + help = 'Set status to Delivered for all orders that have the status\ + Ordered for the specified delivery date.' + + def add_arguments(self, parser): + parser.add_argument( + 'delivery_date', + help='The date must be in the format YYYY-MM-DD', + ) + + def handle(self, *args, **options): + delivery_date = datetime.strptime( + options['delivery_date'], '%Y-%m-%d' + ).date() + + orders = Order.objects.filter( + status=ORDER_STATUS_ORDERED, + delivery_date=delivery_date) + for order in orders: + order.status = ORDER_STATUS_DELIVERED + order.save() + + # Log the execution + LogEntry.objects.log_action( + user_id=1, content_type_id=1, + object_id="", + object_repr="Status set to delivered for orders on" + str( + delivery_date.strftime('%Y-%m-%d %H:%M')), + action_flag=ADDITION, + ) + print("Status set to Delivered for {0} orders whose " + "delivery date is {1}.".format(len(orders), delivery_date)) diff --git a/src/order/models.py b/src/order/models.py index c2501417..ddf93f40 100644 --- a/src/order/models.py +++ b/src/order/models.py @@ -32,6 +32,7 @@ ) ORDER_STATUS_ORDERED = ORDER_STATUS[0][0] +ORDER_STATUS_DELIVERED = ORDER_STATUS[1][0] ORDER_STATUS_CANCELLED = ORDER_STATUS[3][0] SIZE_CHOICES = ( diff --git a/src/order/tests.py b/src/order/tests.py index d405967d..880da6e7 100644 --- a/src/order/tests.py +++ b/src/order/tests.py @@ -21,7 +21,9 @@ from meal.factories import ComponentFactory from order.models import Order, Order_item, MAIN_PRICE_DEFAULT, \ OrderStatusChange, COMPONENT_GROUP_CHOICES_MAIN_DISH, \ - ORDER_ITEM_TYPE_CHOICES_COMPONENT + ORDER_ITEM_TYPE_CHOICES_COMPONENT, \ + ORDER_STATUS_ORDERED, ORDER_STATUS_DELIVERED, ORDER_STATUS_CANCELLED + from order.factories import OrderFactory from sous_chef.tests import TestMixin as SousChefTestMixin @@ -1723,6 +1725,45 @@ def test_generateorders_create_only_if_scheduled_today(self): len(self.ongoing_clients) - 4 ) + def test_setordersdelivered(self): + """Set status to delivered for orders on a given day""" + delivery_date_str = "2017-12-09" + delivery_date = datetime.datetime.strptime( + delivery_date_str, '%Y-%m-%d').date() + + # status should be set to Delivered + OrderFactory( + delivery_date=delivery_date, + status=ORDER_STATUS_ORDERED + ) + # status should be set to Delivered + OrderFactory( + delivery_date=delivery_date, + status=ORDER_STATUS_ORDERED + ) + orders_to_be_set = 2 + + # status should NOT be set to Delivered + OrderFactory( + delivery_date=delivery_date, + status=ORDER_STATUS_CANCELLED + ) + # status should NOT be set to Delivered + OrderFactory( + delivery_date=datetime.datetime.strptime( + "2017-12-31", '%Y-%m-%d').date(), + status=ORDER_STATUS_ORDERED + ) + args = [delivery_date_str] + opts = {} + call_command('setordersdelivered', *args, **opts) + self.assertEqual( + Order.objects.filter( + delivery_date=delivery_date, + status=ORDER_STATUS_DELIVERED).count(), + orders_to_be_set + ) + class OrderListViewTestCase(SousChefTestMixin, TestCase): def test_redirects_users_who_do_not_have_read_permission(self): From 0c54266d0d1a4d4558baf1354f30007928739ff9 Mon Sep 17 00:00:00 2001 From: Francois Lamontagne Date: Tue, 12 Dec 2017 10:39:07 +0700 Subject: [PATCH 2/2] replaced loop by direct update --- src/order/management/commands/setordersdelivered.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/order/management/commands/setordersdelivered.py b/src/order/management/commands/setordersdelivered.py index 0cd67573..5829dd54 100644 --- a/src/order/management/commands/setordersdelivered.py +++ b/src/order/management/commands/setordersdelivered.py @@ -19,12 +19,12 @@ def handle(self, *args, **options): options['delivery_date'], '%Y-%m-%d' ).date() - orders = Order.objects.filter( + numorders = Order.objects.filter( status=ORDER_STATUS_ORDERED, - delivery_date=delivery_date) - for order in orders: - order.status = ORDER_STATUS_DELIVERED - order.save() + delivery_date=delivery_date + ).update( + status=ORDER_STATUS_DELIVERED + ) # Log the execution LogEntry.objects.log_action( @@ -35,4 +35,4 @@ def handle(self, *args, **options): action_flag=ADDITION, ) print("Status set to Delivered for {0} orders whose " - "delivery date is {1}.".format(len(orders), delivery_date)) + "delivery date is {1}.".format(numorders, delivery_date))