def add_item_to_cart(user_id): data = request.json cart = Cart.query.filter_by(user_id=user_id).first() if not cart: cart = Cart(user_id=user_id) cart.save() item = Item(cart_id=cart.id, **data) item.save() Product.add_amount(**data) return jsonify(cart_schema.dump(cart)), HTTPStatus.OK
def get_cart(user_id): """ List all items currently not proceed in cart. :param user_id: :return: """ cart = Cart.query.filter_by(user_id=user_id).first() if not cart: cart = Cart(user_id=user_id) cart.save() return cart_schema.dump(cart), HTTPStatus.OK
def clear_cart(user_id): """ Physically delete item from a cart, bulk delete supported. :param user_id: :return: """ cart = Cart.query.filter_by(user_id=user_id).first() if not cart: cart = Cart(user_id=user_id) cart.save() item = Item.delete_many(Item.cart_id == cart.id) return jsonify({"num": item}), HTTPStatus.NO_CONTENT
def seed(): print("Seeding...") User.create(commit=True, name='user1', email='*****@*****.**', password='******', avatar='http://dummyimage.com/250x250.jpg/dddddd/000000', last_login=datetime.utcnow(), status=AccountStatus.active) User.create(commit=True, name='user2', email='*****@*****.**', password='******', avatar='http://dummyimage.com/250x250.jpg/dddddd/000000', last_login=datetime.utcnow(), status=AccountStatus.active) Address.create(street='stree1', city='city1', state='state1', zip_code='0000000', user_id=1) Address.create(street='stree2', city='city2', state='state2', zip_code='0000002', user_id=2) Category.create(category_name='cat1') Category.create(category_name='cat2') Category.create(category_name='cat3') Product.create(product_name='product1', description='product_des1', price='11', produced_at=datetime.utcnow(), capacity=11, category_id=1) Product.create(product_name='product2', description='product_des2', price='22', produced_at=datetime.utcnow(), capacity=22, category_id=2) Product.create(product_name='product3', description='product_des3', price='22', produced_at=datetime.utcnow(), capacity=22, category_id=3) ProductReview.create(rating=3, review="Good product1!", product_id=1) ProductReview.create(rating=3, review="Good product2!", product_id=2) Cart.create(user_id=1) Cart.create(user_id=2) Item.create(product_id=1, cart_id=1, amount=3) Item.create(product_id=2, cart_id=1, amount=3)
def checkout(user_id): user = User.query.filter_by(id=user_id).first() if not user: return UserExceptions.user_not_exist, HTTPStatus.BAD_REQUEST cart = Cart.query.filter_by(user_id=user_id).first() if not cart: cart = Cart(user_id=user_id) cart.save() items = Item.query.filter_by(cart_id=cart.id, order_id=None).all() if not items: return jsonify({}), HTTPStatus.NOT_FOUND order = Order(status=OrderStatus.pending) order.save() for item in items: item.update(order_id=order.id) item.save() return jsonify({}), HTTPStatus.OK