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]:
In [27]:
orders[0].transaction_set.all()
Out[27]:
In [3]:
t = Transaction.objects.get(pk=1)
In [5]:
i = Item.objects.get(pk=1)
In [9]:
i.recommend(t)
In [1]:
s = Store.objects.all()
In [4]:
s
Out[4]:
In [9]:
s[0].name.replace(" ", "_")
Out[9]:
In [4]:
Item.objects.filter(store__owner=s[0].owner)
Out[4]:
In [5]:
u = s[0].owner
In [6]:
Store.objects.all().filter(owner=s[0].owner)
Out[6]:
Store Home page:
In [7]:
store_id = Store.objects.all().filter(owner=i[0].store.owner)[0].id
In [8]:
s = get_object_or_404(Store, pk=store_id)
Item.objects.all().filter(store=s)
In [9]:
Items = _
In [10]:
Items[0].store.name
Check Out:
In [2]:
Order.objects.all()
Out[2]:
In [4]:
o = _
In [5]:
o[0].shipping
Out[5]:
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]:
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]:
In [9]:
o = _
In [14]:
# fill out transaction for each item
order=Transaction(o, item=i, quantity=3)
Out[14]:
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'])
In [3]:
o = Order.objects.get(pk=1)
In [4]:
for i in o.transaction_set.all():
print i
In [8]:
for i in o.transaction_set.all():
print i.item.name
In [10]:
for i in range(1,4):
print i
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]:
In [10]:
Store.objects.get(name="Life Store")
Out[10]:
In [5]:
r = client.get('/')
In [8]:
r.get_full_path()
In [91]:
r.status_code
Out[91]:
In [92]:
r.context['stores'] != None
Out[92]:
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']
In [ ]: