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
42 changes: 41 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 import models, connection
from django.db.models import Count, Q


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

@classmethod
def split_by_shipping_method(cls):
""" Return two lists of primary keys: one for orders done by First Class Mail, and one for
orders done by Priority Mail."""
fcm = list(cls.objects.filter(shipping_method='FCM').values_list('pk', flat=True))
pri = list(cls.objects.filter(shipping_method='PRI').values_list('pk', flat=True))
return fcm, pri

@classmethod
def split_by_single_and_multiple(cls):
""" Return two lists of primary keys: one for orders that have only one item, and one for
orders that have multiple items."""
singles = list(cls.objects.annotate(Count('items')).filter(items__count=1).values_list('pk', flat=True))
multiples = list(cls.objects.annotate(Count('items')).filter(items__count__gte=2).values_list('pk', flat=True))
return singles, multiples

@classmethod
def single_orders_are_sorted(cls):
""" Return list of orders with only one item, sorted by the priority denoted in the priority attribute of the OrderItem
class (XS -> XXL)."""
cursor = connection.cursor()
cursor.execute("""SELECT innerQuery.id, orders_orderitem.product from (SELECT orders_order.id FROM orders_order LEFT OUTER JOIN orders_orderitem ON ( orders_order.id = orders_orderitem.order_id )
GROUP BY orders_order.id, orders_order.shipping_method, orders_order.date_completed
HAVING COUNT(orders_orderitem.id) = 1) innerQuery LEFT OUTER JOIN orders_orderitem ON (innerQuery.id = orders_orderitem.order_id)""")

single_orders = cursor.fetchall()
single_sorted_orders = [pk for (pk, product) in sorted(single_orders, key=lambda tup: OrderItem.priority[tup[1]])]
return single_sorted_orders

@classmethod
def orders_split_by_xxl_and_not(cls):
""" Return two lists of primary keys: one for multiple orders that include at least one
XXL shirt, and one for multiple orders that have no XXL shirts."""
base_query = cls.objects.annotate(Count('items')).filter(items__count__gte=2)
xxl = list(base_query.filter(items__product='XXL').values_list('pk', flat=True))
not_xxl = list(base_query.filter(~Q(items__product = 'XXL')).values_list('pk', flat=True))
return xxl, not_xxl



class OrderItem(models.Model):
Expand Down
8 changes: 4 additions & 4 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)