Beispiel #1
0
def create_wallet_transaction():
    data = request.get_json()
    wallet_transaction = WalletTransactionSchema().load(data)
    payer = User.query.get(wallet_transaction.payer_id)
    if payer is None:
        return json_response(ResponseCode.NOT_FOUND)
    payee = User.query.get(wallet_transaction.payee_id)
    if payee is None:
        return json_response(ResponseCode.NOT_FOUND)
    count1 = User.query.filter(
        and_(User.id == payer.id,
             User.wallet_money >= wallet_transaction.amount,
             User.wallet_money == payer.wallet_money)).update({
                 User.wallet_money:
                 payer.wallet_money - wallet_transaction.amount
             })
    count2 = User.query.filter(
        and_(User.id == payee.id,
             User.wallet_money == payee.wallet_money)).update({
                 User.wallet_money:
                 payee.wallet_money + wallet_transaction.amount
             })
    if count1 == 0 or count2 == 0:
        session.rollback()
        return json_response(ResponseCode.TRANSACTION_FAILURE)
    session.add(wallet_transaction)
    session.commit()
    return json_response(
        wallet_transaction=WalletTransactionSchema().dump(wallet_transaction))
Beispiel #2
0
def create_cart_product():
    """添加购物车商品
    """

    data = request.get_json()

    cart_product = CartProductSchema().load(data)

    cart_products = CartProduct.query.filter(
        CartProduct.user_id == cart_product.user_id).all()

    # 商品是否已在购物车
    existed = None
    for v in cart_products:
        if v.product_id == cart_product.product_id:
            existed = v
            break

    # 购物车商品数量不能超过限制
    if len(cart_products) >= current_app.config['CART_PRODUCT_LIMIT'] and existed is None:
        return json_response(ResponseCode.QUANTITY_EXCEEDS_LIMIT)

    # 商品已在购物车则更新数量,否则添加一条新纪录
    if existed is None:
        session.add(cart_product)
    else:
        existed.amount += cart_product.amount
    session.commit()

    return json_response(cart_product=CartProductSchema().dump(cart_product if existed is None else existed))
def wallet_transaction_info(wallet_transaction_id):
    wallet_transaction = WalletTransaction.query.get(wallet_transaction_id)
    if wallet_transaction is None:
        return json_response(ResponseCode.NOT_FOUND)

    return json_response(
        wallet_transaction=WalletTransactionSchema().dump(wallet_transaction))
Beispiel #4
0
def update_order(id):
    """更新订单,支持部分更新,但只能更新地址、备注、状态都信息
    注意订单商品要么不更新,要么整体一起更新
    """

    data = request.get_json()

    order = Order.query.get(id)
    if order is None:
        return json_response(ResponseCode.NOT_FOUND)
    if data.get('address_id') is not None:
        order.address_id = data.get('address_id')
    if data.get('note') is not None:
        order.note = data.get('note')
    if data.get('status') is not None:
        order.status = data.get('status')

    if data.get('order_products') is not None:
        order_products = []
        for op in data.get('order_products'):
            order_product = OrderProduct.query.get(op.get('id'))
            if order_product is None:
                return json_response(ResponseCode.NOT_FOUND)
            if op.get('amount') is not None:
                order_product.amount = op.get('amount')
            if op.get('price') is not None:
                order_product.price = op.get('price')
            order_products.append(order_product)
        order.order_products = order_products

    session.commit()

    return json_response(order=OrderSchema().dump(order))
Beispiel #5
0
def user_info(id):

    user = User.query.get(id)

    if user is None:
        return json_response(ResponseCode.NOT_FOUND)

    return json_response(user=UserSchema().dump(user))
def cart_product_info(user_id, product_id):
    cart_product = CartProduct.query.filter(
        and_(CartProduct.user_id == user_id,
             CartProduct.product_id == product_id)).first()
    if cart_product is None:
        return json_response(ResponseCode.NOT_FOUND)

    return json_response(cart_product=CartProductSchema().dump(cart_product))
Beispiel #7
0
def cart_product_info(id):
    """查询购物车商品
    """

    cart_product = CartProduct.query.filter(CartProduct.id == id).first()
    if cart_product is None:
        return json_response(ResponseCode.NOT_FOUND)

    return json_response(cart_product=CartProductSchema().dump(cart_product))
Beispiel #8
0
def product_info(id):
    """查询商品
    """

    product = Product.query.get(id)
    if product is None:
        return json_response(ResponseCode.NOT_FOUND)

    return json_response(product=ProductSchema().dump(product))
Beispiel #9
0
def address_info(id):
    """查询地址
    """

    address = Address.query.get(id)
    if address is None:
        return json_response(ResponseCode.NOT_FOUND)

    return json_response(address=AddressSchema().dump(address))
Beispiel #10
0
def order_info(id):
    """查询订单
    """

    order = Order.query.get(id)
    if order is None:
        return json_response(ResponseCode.NOT_FOUND)

    return json_response(order=OrderSchema().dump(order))
Beispiel #11
0
def update_user(id):
	data = request.get_json()
	user = User.query.get(id)
	if user is None:
		return json_response(ResponseCode.NOT_FOUND)
	for key, value in data.items():
		setattr(user, key, value)
	session.commit()
	return json_response(user=UserSchema().dump(user))
Beispiel #12
0
def shop_info(id):
    """查询店铺
    """

    shop = Shop.query.get(id)
    if shop is None:
        return json_response(ResponseCode.NOT_FOUND)

    return json_response(shop=ShopSchema().dump(shop))
Beispiel #13
0
def favorite_product_info(user_id, product_id):
    favorite_product = FavoriteProduct.query.filter(
        and_(FavoriteProduct.user_id == user_id,
             FavoriteProduct.product_id == product_id)).first()
    if favorite_product is None:
        return json_response(ResponseCode.NOT_FOUND)

    return json_response(
        favorite_product=FavoriteProductSchema().dump(favorite_product))
Beispiel #14
0
def update_shop(shop_id):
    data = request.get_json()

    count = Shop.query.filter(Shop.id == shop_id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    shop = Shop.query.get(shop_id)
    session.commit()

    return json_response(shop=ShopSchema().dump(shop))
Beispiel #15
0
def update_wallet_transaction(id):
    data = request.get_json()
    count = WalletTransaction.query.filter(
        WalletTransaction.id == id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    wallet_transaction = WalletTransaction.query.get(id)
    session.commit()
    return json_response(
        wallet_transaction=WalletTransactionSchema().dump(wallet_transaction))
Beispiel #16
0
def favorite_product_info(id):
    """查询收藏商品
    """

    favorite_product = FavoriteProduct.query.get(id)
    if favorite_product is None:
        return json_response(ResponseCode.NOT_FOUND)

    return json_response(
        favorite_product=FavoriteProductSchema().dump(favorite_product))
Beispiel #17
0
def update_product(product_id):
    data = request.get_json()

    count = Product.query.filter(Product.id == product_id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    product = Product.query.get(product_id)
    session.commit()

    return json_response(product=ProductSchema().dump(product))
Beispiel #18
0
def update_user(user_id):
    data = request.get_json()

    count = User.query.filter(User.id == user_id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    user = User.query.get(user_id)
    session.commit()

    return json_response(user=UserSchema().dump(user))
def delete_cart_product(user_id, product_id):
    cart_product = CartProduct.query.filter(
        and_(CartProduct.user_id == user_id,
             CartProduct.product_id == product_id)).first()
    if cart_product is None:
        return json_response(ResponseCode.NOT_FOUND)
    session.delete(cart_product)
    session.commit()

    return json_response(cart_product=CartProductSchema().dump(cart_product))
Beispiel #20
0
def update_order(order_id):
    data = request.get_json()

    count = Order.query.filter(Order.id == order_id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    order = Order.query.get(order_id)
    session.commit()

    return json_response(order=OrderSchema().dump(order))
Beispiel #21
0
def update_address(address_id):
    data = request.get_json()

    count = Address.query.filter(
        Address.id == address_id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    address = Address.query.get(address_id)
    session.commit()

    return json_response(address=AddressSchema().dump(address))
Beispiel #22
0
def check_password():
	username = request.args.get('username')
	password = request.args.get('password')
	if username is None or password is None:
		return json_response(isCorrect=False)

	user = User.query.filter(User.username == username).first()
	if user is None:
		return json_response(isCorrect=False)
	isCorrect = user.check_password(password)
	return json_response(isCorrect=isCorrect, user=UserSchema().dump(user) if isCorrect else None) 
Beispiel #23
0
def update_address(id):
    data = request.get_json()
    if data.get('is_default'):
        Address.query.filter(Address.is_default == True).update(
            {'is_default': False})
    count = Address.query.filter(Address.id == id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    address = Address.query.get(id)
    session.commit()
    return json_response(address=AddressSchema().dump(adress))
Beispiel #24
0
def delete_favorite_product(user_id, product_id):
    favorite_product = FavoriteProduct.query.filter(
        and_(FavoriteProduct.user_id == user_id,
             FavoriteProduct.product_id == product_id)).first()
    if favorite_product is None:
        return json_response(ResponseCode.NOT_FOUND)
    session.delete(favorite_product)
    session.commit()

    return json_response(
        favorite_product=FavoriteProductSchema().dump(favorite_product))
Beispiel #25
0
def delete_cart_product(id):
    """删除购物车商品
    """

    cart_product = CartProduct.query.filter(CartProduct.id == id).first()
    if cart_product is None:
        return json_response(ResponseCode.NOT_FOUND)

    session.delete(cart_product)
    session.commit()

    return json_response(cart_product=CartProductSchema().dump(cart_product))
Beispiel #26
0
def delete_favorite_product(id):
    """取消收藏商品
    """

    favorite_product = FavoriteProduct.query.get(id)
    if favorite_product is None:
        return json_response(ResponseCode.NOT_FOUND)
    session.delete(favorite_product)
    session.commit()

    return json_response(
        favorite_product=FavoriteProductSchema().dump(favorite_product))
Beispiel #27
0
def update_cart_product(id):
    """更新购物车商品,比如数量
    """

    data = request.get_json()

    count = CartProduct.query.filter(CartProduct.id == id).update(data)
    if count == 0:
        return json_response(ResponseCode.NOT_FOUND)
    cart_product = CartProduct.query.get(id)
    session.commit()

    return json_response(cart_product=CartProductSchema().dump(cart_product))
Beispiel #28
0
def create_file():
    if 'file' not in request.files or request.files['file'].filename == '':
        raise NotFound
    id = mongo.save_file(request.files['file'].filename, request.files['file'])
    _, ext = path.splitext(request.files['file'].filename)
    executor.submit(lambda: make_thumbnails(id))
    return json_response(id='{}{}'.format(id, ext))
Beispiel #29
0
def delete(id):
    """删除购物车商品
    """

    resp = TbBuy(current_app).delete_json('/cart_products/{}'.format(id))

    return json_response(resp['code'], resp['message'], **resp['data'])
Beispiel #30
0
def create_address():
    data = request.get_json()
    address = AddressSchema().load(data)
    session.add(address)
    session.commit()

    return json_response(address=AddressSchema().dump(address))