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
Binary file added orders/.models.py.swp
Binary file not shown.
Binary file added orders/.tests.py.swp
Binary file not shown.
19 changes: 19 additions & 0 deletions orders/migrations/0002_auto_20141111_2233.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('orders', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='orderitem',
name='product',
field=models.CharField(max_length=100, choices=[(b'XS', b'Extra Small Tee'), (b'S', b'Small Tee'), (b'M', b'Medium Tee'), (b'L', b'Large Tee'), (b'XL', b'Extra Large Tee'), (b'XXL', b'Double Extra Large Tee')]),
),
]
72 changes: 70 additions & 2 deletions orders/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models

from operator import itemgetter
from django.db.models import Count

class Order(models.Model):
FCM = 'FCM'
Expand All @@ -10,8 +11,75 @@ 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 = list(Order.objects.filter(shipping_method='FCM').values_list('pk', flat=True))
pri = list(Order.objects.filter(shipping_method='PRI').values_list('pk', flat=True))
return [fcm, pri]


@staticmethod
def split_by_single_and_multiple():
singles = []
multiples = []
orders = Order.objects.all()

for order in orders:
if order.items.count() == 1:
singles.append(order.id)
else:
multiples.append(order.id)

return [singles, multiples]


@staticmethod
def single_orders_are_sorted():
unsorted_orders = []
orders = Order.objects.annotate(item_count=Count('items')).filter(item_count=1)

#Get priority (key, value) dictionary
priority = orders[0].items.first().priority

for order in orders:
#since these are single orders, they can be found at items.first()
only_order = order.items.first()

#store the priority at index 0, and the order_id at index 1
unsorted_orders.append([priority[only_order.product], only_order.order.id])

#sort by using the priority number (stored at index 0)
sorted_orders = sorted(unsorted_orders, key=itemgetter(0))


#create a new array with only the order_id
sorted_ids = []
for i in range (0, len(sorted_orders)):
sorted_ids.append(sorted_orders[i][1])

return sorted_ids


@staticmethod
def orders_split_by_xxl_and_not():
xxl = []
no_xxl = []
orders = Order.objects.annotate(item_count=Count('items')).filter(item_count__gt=1)

for order in orders:
has_xxl = False
for item in order.items.all():
if item.product == 'XXL':
xxl.append(order.id)
has_xxl = True
break
if has_xxl == False:
no_xxl.append(order.id)

return [xxl, no_xxl]


class OrderItem(models.Model):
XS = 'XS'
S = 'S'
Expand All @@ -30,4 +98,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)
14 changes: 7 additions & 7 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()
self.assertEqual(results.single_sorted_orders, single_sorted_orders)
# def test_single_orders_are_sorted(self):
# 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)
2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

ROOT_URLCONF = 'urls'

WSGI_APPLICATION = 'apps.wsgi.application'
WSGI_APPLICATION = 'wsgi.application'

DATABASES = {
'default': {
Expand Down