def test_reserved(self): qty = random.randint(1,10) print ' To reserve: ',qty, if Product.reserve(self.name,qty): p = Product.get_by_key_name(self.name) print ' AVAILABLE: ',p.available self.assertEqual(p.available, p.stock - qty, 'Not reserving correctly') else: p = Product.get_by_key_name(self.name) print ' Not enough stock', p.available self.assertGreater(qty, p.available, 'Not reserving correctly')
def test_stocked(self): qty = random.randint(1,10) print ' To buy: ',qty, if Product.reserve(self.name,qty): p = Product.get_by_key_name(self.name) before = p.reserved Product.remove_stock(self.name,qty) p = Product.get_by_key_name(self.name) print ' AVAILABLE: ',p.available self.assertEqual(p.available, p.stock, 'Not lowering stock correctly ') self.assertEqual(before - qty,p.reserved, 'Not lowering reserves correctly') else: p = Product.get_by_key_name(self.name) print ' Not enough stock', p.available self.assertGreater(qty, p.available, 'Not reserving correctly')
def get(self, subdomain=None): #args item = self.request.get('item','-') qty = int(self.request.get('qty',1)) '''NAMESPACE CHANGE (memcache and datastore)''' namespace = namespace_manager.get_namespace() namespace_manager.set_namespace(subdomain) #15 mins session (900 secs), if a previous session does not exist it creates a new one self.session = self.session_store.get_session(name='mc_session',max_age=900,backend='memcache') try: cart = self.session['cart'] logging.info('cart found in memcache :)') except: return self.response.write('Not cart in session or the session has expired') product = Product.get_by_key_name(item) if not product: return self.response.write('The product does not exist') elif product.unreserve(qty): '''It was not removed from stock''' cart.remove_item(CartItem(product),qty) else: self.response.write('Some products were not reserved. Probably the session expired') self.session['cart'] = cart self.session_store.save_sessions(self.response) '''NAMESPACE CHANGE''' namespace_manager.set_namespace(namespace) return self.response.write(str(cart))
def details_search(request): p = Product.get_by_key_name(request.GET["app"], None) if not p: raise Http404("Product cannot be found") return render_to_response('store/details.html', { 'p': p }, RequestContext(request))
def post_delete(self, obj_key): obj = Product.get_by_key_name(obj_key) # For some reason, obj.user.key.name doesn't work here, so we just figure out what the user key is using the email address. # This is a hack which WILL NOT work if we change how user keys work. Need to figure out the right way to do this. obj_user_key = obj.user.email.split('@')[0] if obj_user_key == self.session['user_key']: Crud.post_delete(self, obj_key) self.redirect('/user/r/'+self.session['user_key']) else: self.redirect('/error/not_owner')
def test_price_tax_cart(self): value_tax_included = 0.0 for i in range(3): p = Product.get_by_key_name(self.products[i]) cart_item = CartItem(p) qty = random.randint(1,10) #qty = 1 value_tax_included += cart_item.final_price*qty self.cart.add_item(cart_item, qty) #print value_tax_included, self.cart.total_without_tax, self.cart.sub_total_tax, self.cart.sub_total_price, self.cart.total self.assertEqual(round(value_tax_included,2), round(self.cart.sub_total_price,2), 'taxes not being calculated well')
def test_price_cart(self): #3 random products in cart value = 0.0 for i in range(3): p = Product.get_by_key_name(self.products[i]) #print p.price, value += p.price self.cart.add_item(CartItem(p),1) #print value, self.cart.total_without_tax, self.cart.sub_total_tax, self.cart.sub_total_price, self.cart.total self.assertEqual(round(value,2), round(self.cart.total_without_tax,2), 'price not matching')
def setUp(self): self.products = [] self.cart = Cart() for i in range(1,10): product = Product(product_id=i, key_name="test_product_"+str(i),price=round(random.uniform(1,10),2),tax_percent=18.00) product.put() product.add_stock("test_product_"+str(i),10) self.products.append("test_product_"+str(i)) p = Product.get_by_key_name("test_product_"+str(i)) self.cart.add_item(CartItem(p), random.randint(1,10)) random.shuffle(self.products)
def activate(request): """ Make sure the user has the product (identify using PIN), and return activation code. """ pin = request.GET['pin'] p = Product.get_by_key_name(request.GET["app"], None) if not p: raise Http404("Product not found") userproduct = UserProduct.gql("where pin = :1 and product = :2", pin, p).get() if not userproduct: raise Http404("Activation data not found") return HttpResponse(userproduct.get_activation_code(), mimetype='text/plain')
def test_reserved_stock(self): #3 random products in cart value = 0.0 result = 0 items_no = 0 for i in range(3): qty = random.randint(1,10) print 'product: ', self.products[i], ' to reserve: ',qty, if Product.reserve(self.products[i],qty): p = Product.get_by_key_name(self.products[i]) #print p.stock, p.reserved self.cart.add_item(CartItem(p),qty) result += qty items_no +=1 #p = Product.get_by_key_name(self.products[i]) print self.cart.total_different_items, items_no self.assertEqual(self.cart.total_different_items, items_no, 'Error in total different items') self.assertEqual(self.cart.total_items, result, 'Error in total_items')
def test_totals_with_delivery(self): print '----------------- TEST RfB TOTALS --------------------' self.cart.delivery = {'price':2.5,'tax':15.0,'name':'testDelivery'} self.cart.recalc() delivery_gross = self.cart.delivery['price'] + self.cart.delivery['price'] * self.cart.delivery['tax']/100 delivery_tax = self.cart.delivery['price'] * self.cart.delivery['tax'] / 100 delivery_net = self.cart.delivery['price'] print 'DELIVERY ','price:', self.cart.delivery['price'], 'plus tax:', delivery_gross, 'tax percent:', self.cart.delivery['tax'],'price tax:', delivery_tax sub_total_tax_items = self.cart.sub_total_tax_items #TAX Items sub_total_price = self.cart.sub_total_price #GROSS Items total = self.cart.total #GROSS Items + GROSS Delivery total_without_tax = self.cart.total_without_tax #NET Items + NET Delivery total_tax = self.cart.total_tax #TAX items + TAX Delivery for i in range(3): qty = random.randint(1,10) qty=1 p = Product.get_by_key_name(self.products[i]) print self.products[i],'price:', p.price, 'plus tax:', p.price + p.price * p.tax_percent / 100,'tax percent: ',p.tax_percent, 'price tax:',p.price * p.tax_percent / 100, 'qty:',qty sub_total_tax_items -= (p.price * p.tax_percent / 100)*qty sub_total_price -= (p.price + p.price * p.tax_percent / 100)*qty total -= (p.price + p.price * p.tax_percent / 100)*qty total_without_tax -= (p.price)*qty total_tax -= (p.price * p.tax_percent / 100)*qty self.cart.remove_item(CartItem(p),qty) print 'sub_total_tax_items: ', sub_total_tax_items, ' CART sub_total_tax_items: ',self.cart.sub_total_tax_items self.assertAlmostEqual(sub_total_tax_items, self.cart.sub_total_tax_items,7, 'Error calculating sub_total_tax_items') print 'sub_total_price: ', sub_total_price, ' CART sub_total_price: ',self.cart.sub_total_price self.assertEqual(round(sub_total_price,2), round(self.cart.sub_total_price,2), 'Error calculating sub_total_price') print 'total: ', total, ' CART total: ',self.cart.total self.assertEqual(round(total,2), round(self.cart.total,2), 'Error calculating total') print 'total_without_tax: ', total_without_tax, ' CART total_without_tax: ',self.cart.total_without_tax self.assertEqual(round(total_without_tax,2), round(self.cart.total_without_tax,2), 'Error calculating total_without_tax') print 'total_tax: ', total_tax, ' CART total_tax: ',self.cart.total_tax self.assertEqual(round(total_tax,2), round(self.cart.total_tax,2), 'Error calculating total_tax') print '----------------- END TEST -----------------------'
def get(self, subdomain=None): #args item = self.request.get('item','-') qty = int(self.request.get('qty',1)) '''NAMESPACE CHANGE (memcache and datastore)''' namespace = namespace_manager.get_namespace() namespace_manager.set_namespace(subdomain) '''Name of the memcache''' logging.info(str(self.session)) #cookie_value = self.request.cookies.get('shopapp-cart') #logging.info(str(cookie_value)) '''logging.info(cookie_value) if not cookie_value: #random number import random, string cookie_value = ''.join(random.choice(string.ascii_uppercase + '123456789') for i in xrange(10)) self.response.set_cookie(key='shopapp-cart', value=cookie_value,max_age=900) logging.info(str(self.request.cookies.get('shopapp-cart')))''' #self.response.set_cookie(key='guille', value='el mejor',max_age=900) self.session = self.session_store.get_session(name='shopapp',max_age=None,backend='memcache') logging.info(str(self.session)) #15 mins session (900 secs), if a previous session does not exist it creates a new one #self.session = self.session_store.get_session(name='mc_session',max_age=900,backend='memcache') #logging.info(cookie_value) try: cart = self.session['cart'] logging.info('cart found in memcache :)') except: cart = Cart() logging.info('cart not found in memcache :(') #return self.response.write(str(self.session['cart'].__dict__)) '''import random for i in range(1,10): product = Product(product_id=i, key_name="test_product_"+str(i),price=round(random.uniform(1,10),2),tax_percent=18.00) product.put() product.add_stock("test_product_"+str(i),10)''' product = Product.get_by_key_name(item) if not product: return self.response.write('The product does not exist') elif product.reserve(qty): '''Checked stock''' cart.add_item(CartItem(product),qty) else: self.response.write('Not enough products in the warehouse') self.session['cart'] = cart self.session_store.save_sessions(self.response) '''NAMESPACE CHANGE''' namespace_manager.set_namespace(namespace) return self.response.write(str(cart))
def purchase_search(request): return purchase_do(request, Product.get_by_key_name(request.GET["app"], None))