def test_find_by_state(self): """ Find Carts by State """ ShoppingCart(state="activated", userId=4).save() ShoppingCart(state="deactivated", userId=5).save() shopcarts = ShoppingCart.find_by_state("deactivated") self.assertEqual(shopcarts[0].state, "deactivated") self.assertEqual(shopcarts[0].userId, 5)
def test_find_by_user(self): """ Find a Cart by UserId """ ShoppingCart(state="activated", userId=4).save() ShoppingCart(state="deactivated", userId=5).save() shopcarts = ShoppingCart.find_by_user(5) self.assertEqual(shopcarts[0].state, "deactivated") self.assertEqual(shopcarts[0].userId, 5)
def list_carts(): """ Returns all of the Carts """ if request.args.get('userId'): user_id = request.args.get('userId') app.logger.info('Getting Cart for user with id: {}'.format(user_id)) results = ShoppingCart.find_by_user(user_id) if not results: raise NotFound( 'Cart with user id: {} was not found'.format(user_id)) return jsonify([cart.serialize() for cart in results]), status.HTTP_200_OK elif request.args.get('state'): state = request.args.get('state') app.logger.info('Getting Carts with state: {}'.format(state)) results = ShoppingCart.find_by_state(state) if not results: raise NotFound('Carts with state: {} was not found'.format(state)) return jsonify([cart.serialize() for cart in results]), status.HTTP_200_OK else: results = [] app.logger.info('Getting all Carts') results = ShoppingCart.all() return jsonify([cart.serialize() for cart in results]), status.HTTP_200_OK
def test_deserialize_a_cart(self): """ Test deserialization of a Cart """ data = {"id": 1, "state": "deactivated", "userId": 15} shopcart = ShoppingCart() shopcart.deserialize(data) self.assertNotEqual(shopcart, None) self.assertEqual(shopcart.id, None) self.assertEqual(shopcart.state, "deactivated") self.assertEqual(shopcart.userId, 15)
def test_serialize_a_cart(self): """ Test serialization of a Cart """ shopcart = ShoppingCart(state="deactivated", userId=10) data = shopcart.serialize() self.assertNotEqual(data, None) self.assertIn('id', data) self.assertEqual(data['id'], None) self.assertIn('state', data) self.assertEqual(data['state'], "deactivated") self.assertIn('userId', data) self.assertEqual(data['userId'], 10)
def test_delete_a_cart(self): """ Delete a Cart """ shopcart = ShoppingCart(state="activated", userId=5) shopcart.save() self.assertEqual(len(ShoppingCart.all()), 1) # delete the cart and make sure it isn't in the database shopcart.delete() self.assertEqual(len(ShoppingCart.all()), 0)
def test_create_a_shopcart(self): """ Create a cart and assert that it exists """ shopcart = ShoppingCart(state="activated", userId=5) self.assertTrue(shopcart != None) self.assertEqual(shopcart.id, None) self.assertEqual(shopcart.state, "activated") self.assertEqual(shopcart.userId, 5)
def delete_carts(cart_id): """ Deletes a Cart with given cart ID """ app.logger.info('Request to delete cart with id: {}'.format(cart_id)) cart = ShoppingCart.find(cart_id) if cart: cart.delete() return make_response('', status.HTTP_204_NO_CONTENT)
def purchased(): produc = ShoppingCart.query.all() total = ShoppingCart.gettotal() if total != 0: return render_template('purchased.html', product=produc, total=total) else: return redirect(url_for('cart'))
def get_carts(cart_id): """ Returns all of the Carts with given ID """ app.logger.info('Getting Cart with id: {}'.format(cart_id)) cart = ShoppingCart.find(cart_id) if not cart: raise NotFound('Cart with id: {} was not found'.format(cart_id)) return jsonify(cart.serialize()), status.HTTP_200_OK
def delete_from_cart(product_id): produc = ShoppingCart.query.all() total = ShoppingCart.gettotal() for prod in produc: if prod.id == product_id: db.session.delete(prod) db.session.commit() total -= prod.price return redirect(url_for('cart'))
def buy(request): userid = request.GET.get('userid') movieid = request.GET.get('movieid') price = request.GET.get('price') quantity = request.GET.get('quantity') seats = request.GET.get('seats') userfor = User.objects.filter(id=userid).first() moviesfor = Movies.objects.filter(id=movieid).first() shopping = ShoppingCart() shopping.userid = userfor shopping.movieid = moviesfor shopping.price = price shopping.quantity = quantity shopping.seats = seats shopping.save() return render(request, 'user/userInfo.html')
def add_to_cart(product_cat, product_id): produc = Products.query.filter_by(category=product_cat).all() for prod in produc: if prod.id == product_id: prod = ShoppingCart(prod.name, prod.description, prod.price, prod.category, prod.image) db.session.add(prod) db.session.commit() return render_template('products.html', product=produc)
def get_items(cart_id): """ Returns all the items in a cart """ app.logger.info('Getting items of Cart with id: {}'.format(cart_id)) cart = ShoppingCart.find(cart_id) if not cart: raise NotFound('Cart with id: {} was not found'.format(cart_id)) results = ShoppingCartItems.allItems(cart_id) if not results: raise NotFound( 'Cart with id: {} does not have any item'.format(cart_id)) return jsonify([item.serialize() for item in results]), status.HTTP_200_OK
def test_update_a_cart(self): """ Update a ShopCart """ shopcart = ShoppingCart(state="activated", userId=5) shopcart.save() self.assertEqual(shopcart.id, 1) # Change it an save it shopcart.state = "deactivated" shopcart.save() self.assertEqual(shopcart.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change shopcarts = ShoppingCart.all() self.assertEqual(len(shopcarts), 1) self.assertEqual(shopcarts[0].state, "deactivated")
def create_carts(): """ Create a new Cart """ app.logger.info('Create Cart requested') check_content_type('application/json') cart = ShoppingCart(None) cart.deserialize(request.get_json()) cart.save() app.logger.info('Created Cart with id: {}'.format(cart.id)) return make_response( jsonify(cart.serialize()), status.HTTP_201_CREATED, {'Location': url_for('get_carts', cart_id=cart.id, _external=True)})
def get_item(cart_id, item_id): app.logger.info( 'Getting particular item of id: {} in Cart with id: {}'.format( item_id, cart_id)) cart = ShoppingCart.find(cart_id) if not cart: raise NotFound('Cart with id: {} was not found'.format(cart_id)) results = ShoppingCartItems.find(cart_id, item_id) if not results: raise NotFound( 'Cart with id: {} does not have any item with id: {}'.format( cart_id, item_id)) return jsonify([item.serialize() for item in results]), status.HTTP_200_OK
def update_carts(cart_id): """ Update a cart with the given cart ID """ app.logger.info('Updating cart with id: {}'.format(cart_id)) check_content_type('application/json') cart = ShoppingCart.find(cart_id) if not cart: raise NotFound('Cart with id: {} was not found'.format(cart_id)) # process the update request cart.deserialize(request.get_json()) cart.id = cart_id # make id matches request cart.save() app.logger.info('Cart with id {} has been updated'.format(cart_id)) return jsonify(cart.serialize()), status.HTTP_200_OK
def test_find_cart(self): """ Find a Cart by ID """ ShoppingCart(state="activated", userId=7).save() deactivated = ShoppingCart(state="deactivated", userId=8) deactivated.save() shopcart = ShoppingCart.find(deactivated.id) self.assertIsNot(shopcart, None) self.assertEqual(shopcart.id, deactivated.id) self.assertEqual(shopcart.state, "deactivated") self.assertEqual(shopcart.userId, 8)
def create_items(cart_id): app.logger.info('Create Item requested') cart = ShoppingCart.find(cart_id) if not cart: raise NotFound('No Cart with id: {} exist'.format(cart_id)) item = ShoppingCartItems() item.deserialize(request.get_json()) item.cartId = cart_id item.add() app.logger.info('Created Item with id: {}'.format(item.id)) return make_response( jsonify(item.serialize()), status.HTTP_201_CREATED, { 'Location': url_for( 'get_item', cart_id=cart_id, item_id=item.id, _external=True) })
def test_add_a_cart(self): """ Create a cart and add it to the database """ shopcarts = ShoppingCart.all() self.assertEqual(shopcarts, []) shopcart = ShoppingCart(state="activated", userId=5) self.assertTrue(shopcart != None) self.assertEqual(shopcart.id, None) shopcart.save() # Asert that it was assigned an id and shows up in the database self.assertEqual(shopcart.id, 1) shopcarts = ShoppingCart.all() self.assertEqual(len(shopcarts), 1)
def update_cartitems(cart_id, item_id): """ Update a cart with the given cart ID and item ID """ app.logger.info('Updating cart with id: {} and item {}'.format( cart_id, item_id)) check_content_type('application/json') cart = ShoppingCart.find(cart_id) if not cart: raise NotFound('Cart with id: {} was not found'.format(cart_id)) results = ShoppingCartItems.find(cart_id, item_id) if not results: raise NotFound('CartID: {} does not have item ID {}'.format( cart_id, item_id)) # process the update request for item in results: item.deserialize(request.get_json()) item.id = item_id item.add() app.logger.info('CartID {} and item ID {} has been updated'.format( cart_id, item_id)) return jsonify([item.serialize() for item in results]), status.HTTP_200_OK
def quantity(): produc = ShoppingCart.query.all() total = ShoppingCart.gettotal() return render_template('cart.html', product=produc, total=total)
def setUp(self): ShoppingCart.init_db(app) db.drop_all() # clean up the last tests db.create_all() # make our sqlalchemy tables
def test_deserialize_bad_data(self): """ Test deserialization of bad data """ data = "this is not a dictionary" shopcart = ShoppingCart() self.assertRaises(DataValidationError, shopcart.deserialize, data)
def init_db(): """ Initialies the SQLAlchemy app """ global app ShoppingCart.init_db(app) ShoppingCartItems.init_db(app)