In [2]:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'shopping_cart_project.settings'
from shopping_cart.models import *
from django.shortcuts import *

Home Page:


In [2]:
avg_transaction = 0 
orders = Order.objects.filter(buyer=User.objects.get(pk=1))
for o in orders:
    avg_purchase = 0
    sum_purchase = 0
    for t in o.transaction_set.all():
        sum_purchase += t.item.price
    avg_purchase = (sum_purchase / len(o.transaction_set.all()))
    avg_transaction = ((avg_transaction + avg_purchase) / (2))

In [3]:
avg_transaction


Out[3]:
Decimal('679.246640625')

In [27]:
orders[0].transaction_set.all()


Out[27]:
[<Transaction: Order Order by agconti: on Mon Sep  2 15:42:58 2013: on Mon Sep  2 15:42:58 2013>, <Transaction: Order Order by agconti: on Mon Sep  2 15:42:58 2013: on Mon Sep  2 15:42:58 2013>, <Transaction: Order Order by agconti: on Mon Sep  2 15:42:58 2013: on Mon Sep  2 15:42:58 2013>, <Transaction: Order Order by agconti: on Mon Sep  2 15:42:58 2013: on Mon Sep  2 15:42:58 2013>]

In [3]:
t = Transaction.objects.get(pk=1)

In [5]:
i = Item.objects.get(pk=1)

In [9]:
i.recommend(t)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-2d8d26766216> in <module>()
----> 1 i.recommend(t)

TypeError: recommend() takes exactly 2 arguments (1 given)

In [1]:
s = Store.objects.all()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-04bc060e53e7> in <module>()
----> 1 s = Store.objects.all()

NameError: name 'Store' is not defined

In [4]:
s


Out[4]:
[<Store: Store Life Store: by Merchant agconti>, <Store: Store Pirate Gear: by Merchant captian_jack>, <Store: Store Guitar Store: by Merchant non_super>]

In [9]:
s[0].name.replace(" ", "_")


Out[9]:
u'Life_Store'

In [4]:
Item.objects.filter(store__owner=s[0].owner)


Out[4]:
[<Item: Item: Time: Sold by Life Store>, <Item: Item: Sleep: Sold by Life Store>, <Item: Item: Do Overs: Sold by Life Store>]

In [5]:
u = s[0].owner

In [6]:
Store.objects.all().filter(owner=s[0].owner)


Out[6]:
[<Store: Store Life Store: by Merchant agconti>]

Store Home page:


In [7]:
store_id = Store.objects.all().filter(owner=i[0].store.owner)[0].id


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-4275825e08b4> in <module>()
----> 1 store_id = Store.objects.all().filter(owner=i[0].store.owner)[0].id

NameError: name 'i' is not defined

In [8]:
s = get_object_or_404(Store, pk=store_id) 
Item.objects.all().filter(store=s)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-b720a1ae02cf> in <module>()
----> 1 s = get_object_or_404(Store, pk=store_id)
      2 Item.objects.all().filter(store=s)

NameError: name 'store_id' is not defined

In [9]:
Items = _

In [10]:
Items[0].store.name


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-d8e0071e6861> in <module>()
----> 1 Items[0].store.name

AttributeError: 'Store' object has no attribute 'store'

Check Out:


In [2]:
Order.objects.all()


Out[2]:
[<Order: Order by agconti: on Mon Sep  2 00:14:28 2013>]

In [4]:
o = _

In [5]:
o[0].shipping


Out[5]:
u'teleporter'

In [17]:
sess = [{'item_id': u'5', 'price': u'50', 'name': u'Hook Hand', 'quantity': u'1'}, {'item_id': u'1', 'price': u'450', 'name': u'fender start', 'quantity': u'1'}, {'item_id': u'3', 'price': u'500', 'name': u'les paul', 'quantity': u'1'}, {'item_id': u'1', 'price': u'450', 'name': u'fender start', 'quantity': u'1'}]

In [18]:
sess


Out[18]:
[{'item_id': u'5', 'name': u'Hook Hand', 'price': u'50', 'quantity': u'1'},
 {'item_id': u'1', 'name': u'fender start', 'price': u'450', 'quantity': u'1'},
 {'item_id': u'3', 'name': u'les paul', 'price': u'500', 'quantity': u'1'},
 {'item_id': u'1', 'name': u'fender start', 'price': u'450', 'quantity': u'1'}]

In [6]:
i = Item.objects.get(pk=5)

In [7]:
u = i.store.owner

In [8]:
#Create order first
Order.objects.create(buyer=u)


Out[8]:
<Order: Order by captian_jack: on Mon Sep  2 14:33:21 2013>

In [9]:
o = _

In [14]:
# fill out transaction for each item
order=Transaction(o, item=i, quantity=3)


Out[14]:
<Transaction: Order Order by captian_jack: on Mon Sep  2 14:33:21 2013: on Mon Sep  2 14:33:21 2013>

In [25]:
#Create order first
o = Order.objects.create(buyer=u)
for i in sess:
    Transaction(order = o , item=i['item_id'], quantity=i['quantity'])


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-f55172becfad> in <module>()
      2 o = Order.objects.get_or_create(buyer=u)
      3 for i in sess:
----> 4     Transaction(order = o , item=i['item_id'], quantity=i['quantity'])
      5 

/home/agconti/my_dev/github/shopping_cart/venv/lib/python2.7/site-packages/django/db/models/base.pyc in __init__(self, *args, **kwargs)
    401                 # "user_id") so that the object gets properly cached (and type
    402                 # checked) by the RelatedObjectDescriptor.
--> 403                 setattr(self, field.name, rel_obj)
    404             else:
    405                 setattr(self, field.attname, val)

/home/agconti/my_dev/github/shopping_cart/venv/lib/python2.7/site-packages/django/db/models/fields/related.pyc in __set__(self, instance, value)
    403             raise ValueError('Cannot assign "%r": "%s.%s" must be a "%s" instance.' %
    404                                 (value, instance._meta.object_name,
--> 405                                  self.field.name, self.field.rel.to._meta.object_name))
    406         elif value is not None:
    407             if instance._state.db is None:

ValueError: Cannot assign "(<Order: Order by captian_jack: on Mon Sep  2 14:33:21 2013>, False)": "Transaction.order" must be a "Order" instance.

In [3]:
o = Order.objects.get(pk=1)

In [4]:
for i in o.transaction_set.all():
    print i


Order Order by agconti: on Mon Sep  2 15:42:58 2013: on Mon Sep  2 15:42:58 2013
Order Order by agconti: on Mon Sep  2 15:42:58 2013: on Mon Sep  2 15:42:58 2013
Order Order by agconti: on Mon Sep  2 15:42:58 2013: on Mon Sep  2 15:42:58 2013
Order Order by agconti: on Mon Sep  2 15:42:58 2013: on Mon Sep  2 15:42:58 2013

In [8]:
for i in o.transaction_set.all():
    print i.item.name


Time
Eye Patch
Chest
cannon ball 

In [10]:
for i in range(1,4):
    print i


1
2
3

Testing:


In [1]:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'shopping_cart_project.settings'
from shopping_cart.models import *
from django.shortcuts import *

In [2]:
import unittest
from django.test.client import Client
from django.test.utils import setup_test_environment
setup_test_environment()

In [3]:
client = Client()

In [4]:
login = client.login(username='agconti', password='password')
login


Out[4]:
True

In [10]:
Store.objects.get(name="Life Store")


Out[10]:
<Store: Store Life Store: by Merchant agconti>

In [5]:
r = client.get('/')

In [8]:
r.get_full_path()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-a6fe38a6a62a> in <module>()
----> 1 r.get_full_path()

AttributeError: 'HttpResponse' object has no attribute 'get_full_path'

In [91]:
r.status_code


Out[91]:
200

In [92]:
r.context['stores'] != None


Out[92]:
True

In [ ]:
ss add_to_cart_ViewTest(unittest.TestCase):
    def setUp(self):
        self.client = client

    def test_details(self):
    	# get the home view
        response = self.client.get('/add_to_cart/')

        # Check that the response is 302 Found.
        self.assertEqual(response.status_code, 302)

        # Check that the rendered context is not none 
        #self.assertTrue(request.session['cart'] != None)
        self.assertTrue(i.store.id != None)
        self.assertEqual(request.session.modified, True)
        self.assertEqual(ordered, True)

In [7]:
response = client.get('add_to_cart/')

In [10]:
response.context['i']


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-10-966cf4d1cb55> in <module>()
----> 1 response.context['i']

/home/agconti/my_dev/github/shopping_cart/venv/lib/python2.7/site-packages/django/template/context.pyc in __getitem__(self, key)
     55             if key in d:
     56                 return d[key]
---> 57         raise KeyError(key)
     58 
     59     def __delitem__(self, key):

KeyError: 'i'

In [ ]: