Skip to content

Commit

Permalink
Merge pull request #823 from lamontfr/fix822
Browse files Browse the repository at this point in the history
Command to set orders status to Delivered
  • Loading branch information
lamontfr authored Dec 12, 2017
2 parents e2f6b28 + 0c54266 commit 844ca09
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/order/management/commands/setordersdelivered.py
Original file line number Diff line number Diff line change
@@ -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()

numorders = Order.objects.filter(
status=ORDER_STATUS_ORDERED,
delivery_date=delivery_date
).update(
status=ORDER_STATUS_DELIVERED
)

# 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(numorders, delivery_date))
1 change: 1 addition & 0 deletions src/order/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
43 changes: 42 additions & 1 deletion src/order/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 844ca09

Please sign in to comment.