def create_order(product_ids, address_id, user_coupon_id, remark, uid): order_no = Order.__make_order_no() o_products = [] for ids in product_ids: o_products.append(Product.get_order_product(ids)) with db.auto_check_empty(UserException(error_code=6001, msg='地址不存在')): user_address = UserAddress.query.filter_by(id=address_id).first() snap_address = user_address.name + ' ' + user_address.mobile + ' ' + user_address.detail if user_coupon_id: with db.auto_check_empty( UserException(error_code=7001, msg='优惠卷不存在')): coupon_price = UserCoupon.query.filter( and_(UserCoupon.id == user_coupon_id, UserCoupon.status == 1)).first().coupon.price else: coupon_price = 0 total_price = Order.__product_to_calculate(o_products)[0] postage = Order.__product_to_calculate(o_products)[1] with db.auto_commit(): order = Order() order.order_no = order_no order.user_id = uid order.snap_address = snap_address order.coupon_price = coupon_price order.remark = remark order.total_price = total_price order.postage = postage order.pay_price = total_price + postage - coupon_price OrderSnap.add_order_snap(o_products, order_no) Order.__minus_product_stock(o_products) Order.__revise_user_coupon(user_coupon_id) db.session.add(order) return order_no
def renew_address(): address_info = AddressNew().validate_for_api().data uid = g.user.uid with db.auto_check_empty(UserException): user = User.query.filter_by(id=uid).first_or_404() user.save_address(address_info) return RenewSuccess()
def renew_address(): uid = g.user.uid address_info = AddressNew().validate_for_api().data with db.auto_check_empty(UserException): user = User.query.filter_by(id=uid).first_or_404() user.save_address(address_info, uid) user_address = UserAddress.query.filter_by(user_id=uid).first() return jsonify(user_address)
def get_product_by_category_id(id, count, page): with db.auto_check_empty(ProductException): products = Product.query.filter_by( category_id=id).limit(count).offset(page).all() if products: products = [product.hide('postage') for product in products] return products else: return None
def validate_cart_ids(self, value): cart_ids = value.data if not isinstance(cart_ids, list): raise ValidationError(message='购物车参数不正确') if len(cart_ids) == 0: raise ValidationError(message='购物车列表不能为空') for cart_id in cart_ids: if not self.isPositiveInteger(cart_id): raise ValidationError(message='购物车列表参数错误') else: with db.auto_check_empty(CartException()): Cart.query.filter_by(id=cart_id).first_or_404()
def get_order_product(ids): with db.auto_check_empty(ProductException): product = dict( Product.query.filter_by( id=ids['product_id']).first_or_404().hide( 'originalPrice', 'sale', 'summary')) product['qty'] = ids['qty'] if 'property_id' in ids.keys(): if ids['property_id']: with db.auto_check_empty(PropertyException): product['property'] = dict( Product2Property.query.filter_by( id=ids['property_id']).first_or_404()) if product['qty'] > product['property']['stock']: raise StockException(msg=str(product['id']) + '库存不足') else: product['property'] = None else: product['property'] = None if product['qty'] > product['stock']: raise StockException(str(msg=product['id']) + '库存不足') return product
def get_most_recent(count, page): with db.auto_check_empty(ProductException): products = Product.query.order_by(desc( Product.create_time)).limit(count).offset(page).all() products = [product.hide('postage') for product in products] return products
def get_product_by_category_id(id): with db.auto_check_empty(ProductException): return Product.query.filter_by(category_id=id).all()
def get_most_recent(count): with db.auto_check_empty(ProductException): return Product.query.filter_by(id=1).all()
def get_product_detail(id): with db.auto_check_empty(ProductException): return Product.query.filter_by( id=id).first_or_404().hide('category_id')
def get_most_recent(count): with db.auto_check_empty(ProductException): return Product.query.order_by(desc( Product.create_time)).limit(count).all()
def get_theme_detail(id): # get theme detail with product with db.auto_check_empty(ThemeException): theme_detail = db.session.query(Theme).filter( Theme.id == id).first_or_404() return theme_detail.append('products')
def product(self): with db.auto_check_empty(ProductException): return Product.query.filter_by( id=self.product_id).first_or_404().hide('summary', 'sale')
def __get_user_address(self): with db.auto_check_empty(UserException(error_code=6001, msg='用户地址不存在, 下单失败')): user_address = UserAddress.query.filter_by(user_id=self.uid).first_or_404() return jsonify(user_address)
def get_theme_detail(id): # get theme detail with product with db.auto_check_empty(ThemeException): theme_detail = db.session.query(Theme).filter(Theme.id == id).first_or_404() return theme_detail.append('products')
def get_comment_by_pid(pid, count, page): with db.auto_check_empty(NotFound): return Comment.query.filter_by( product_id=pid).limit(count).offset(page).all()
def userInfo(self): with db.auto_check_empty(UserException): return User.query.filter_by(id=self.uid).first_or_404().hide( 'email', 'auth')
def get_cart_by_uid(uid, count, page): with db.auto_check_empty(NotFound): return Cart.query.filter_by( uid=uid).limit(count).offset(page).all()
def property(self): if self.property_id: with db.auto_check_empty(PropertyException): return Product2Property.query.filter_by( id=self.property_id).first_or_404()
def get_banner_by_id(id): with db.auto_check_empty(BannerMissException): return Banner.query.filter_by(id=id).first_or_404()
def get_theme_by_id(id): with db.auto_check_empty(ThemeException): return Theme.query.filter_by(id=id).first_or_404()
def get_product_detail(id): with db.auto_check_empty(ProductException): return Product.query.filter_by(id=id).first_or_404() \ .append('detail_img', 'banner_img', 'property', 'content', 'main_img_id' )
def get_address(): '''获取「用户自身的地址」''' uid = g.user.uid with db.auto_check_empty(UserException(error_code=6001, msg='用户地址不存在')): user_address = UserAddress.query.filter_by(user_id=uid).first_or_404() return Success(user_address)
def get_all_categories(): with db.auto_check_empty(CategoryException): return Category.query.all()