Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion orders/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.db.models import Count


class Order(models.Model):
Expand All @@ -11,6 +12,64 @@ class Order(models.Model):
shipping_method = models.CharField(max_length=100, choices=SHIPPING_CHOICES)
date_completed = models.DateTimeField()

@staticmethod
def split_by_shipping_method():
fcm_orders = list(Order.objects.values_list('id', flat=True).filter(shipping_method=Order.FCM))
pri_orders = list(Order.objects.values_list('id', flat=True).filter(shipping_method=Order.PRI))
orders = (fcm_orders, pri_orders)

return orders

@staticmethod
def split_by_single_and_multiple():
single_orders = list(Order.objects.annotate(total=Count('items')).values_list('id', flat=True).filter(total=1))
multiple_orders = list(Order.objects.annotate(total=Count('items')).values_list('id', flat=True).filter(total__gt=1))

orders = (single_orders, multiple_orders)

return orders

@staticmethod
def single_orders_are_sorted():
#Hmm... hate doing two queries here but it looks like the only way to get the product field
single_orders = list(Order.objects.annotate(total=Count('items')).values_list('id', flat=True).filter(total=1))
#Overwrite the value to include product ids
single_orders = list(OrderItem.objects.values_list('order_id', 'product').filter(order_id__in=single_orders))

sorted_single_orders = sorted(single_orders, key=lambda order: OrderItem.priority[order[1]])

orders = [sorted_single_order[0] for sorted_single_order in sorted_single_orders]

return orders

@staticmethod
def orders_split_by_xxl_and_not():
#Hmm... hate doing two queries here but it looks like the only way to get the product field
multiple_orders = list(Order.objects.annotate(total=Count('items')).values_list('id', flat=True).filter(total__gt=1))
#Overwrite the value to include product ids
multiple_orders = list(OrderItem.objects.values_list('order_id', 'product').filter(order_id__in=multiple_orders))

#Use hash to determine if order has XXL or not. True means yes, False means no
xxl_test = {}
for order in multiple_orders:
#Value previously unset, make False
if order[0] not in xxl_test:
xxl_test[order[0]] = False

#If we see an XXL item, set to True
if order[1] == OrderItem.XXL:
xxl_test[order[0]] = True

xxl = []
no_xxl = []
#Sort the keys first so the orders come out ascending
for order in sorted(xxl_test.keys()):
if xxl_test[order] == True:
xxl.append(order)
else:
no_xxl.append(order)

return (xxl, no_xxl)

class OrderItem(models.Model):
XS = 'XS'
Expand All @@ -30,4 +89,4 @@ class OrderItem(models.Model):
priority = {'XS': 0, 'S': 1, 'M': 2, 'L': 3, 'XL': 4, 'XXL': 5}
order = models.ForeignKey(Order, related_name='items')
product = models.CharField(max_length=100, choices=PRODUCT_CHOICES)
quantity = models.PositiveIntegerField(default=1)
quantity = models.PositiveIntegerField(default=1)
10 changes: 5 additions & 5 deletions orders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ class OrderOrderingTestCase(TestCase):
fixtures = ['test_orders.json']

def test_orders_are_split_by_shipping_method(self):
#fcm, pri = Order.split_by_shipping_method()
fcm, pri = Order.split_by_shipping_method()
self.assertEqual(results.fcm, fcm)
self.assertEqual(results.pri, pri)

def test_orders_are_split_by_single_and_multiple(self):
#singles, multiples = Order.split_by_single_and_multiple()
singles, multiples = Order.split_by_single_and_multiple()
self.assertEqual(results.singles, singles)
self.assertEqual(results.multiples, multiples)

def test_single_orders_are_sorted(self):
#single_sorted_orders = Order.single_orders_are_sorted()
single_sorted_orders = Order.single_orders_are_sorted()
self.assertEqual(results.single_sorted_orders, single_sorted_orders)

def test_multiple_orders_are_split_by_xxl_and_not(self):
#xxl, not_xxl = Order.orders_split_by_xxl_and_not()
xxl, not_xxl = Order.orders_split_by_xxl_and_not()
self.assertEqual(results.xxl, xxl)
self.assertEqual(results.not_xxl, not_xxl)
self.assertEqual(results.not_xxl, not_xxl)