def edit(): if request.method == 'POST': form = EditForm().validate() with db.auto_commit(): current_user.nickname = form.nickname.data current_user.email = form.email.data db.session.add(current_user) return Success(msg='信息修改成功') return render_template('user/edit.html', current='edit')
def set(id): member = Member.query.get_or_404_deleted(id, msg='找不到指定会员') if request.method == 'POST': form = SetForm().validate() with db.auto_commit(): member.nickname = form.nickname.data db.session.add(member) return Success() return render_template('member/set.html', info=member, current='index')
def share(): form = UrlForm().validate() member = g.member if not member: return NotFound(msg='找不到指定会员') with db.auto_commit(): share = WxShareHistory() share.member_id = member.id db.session.add(share) return Success()
def ops(id): id, act = OpsForm().validate().data.values() member = Member.query.get_or_404(id, msg='找不到指定会员') with db.auto_commit(): if act == 'remove': member.remove() msg = '删除成功' elif act == 'recover': member.recover() msg = '恢复成功' db.session.add(member) return Success(msg=msg)
def category_set(id): food_cat = None if id: food_cat = FoodCat.query.get_or_404_deleted(id, msg='未找到指定分类') if request.method == 'POST': form = CategorySetForm().validate() with db.auto_commit(): if not food_cat: food_cat = FoodCat() food_cat.name = form.name.data food_cat.weight = form.weight.data db.session.add(food_cat) return Success() return render_template('food/cat_set.html', current='cat', info=food_cat)
def set(id): food = None if id: food = Food.query.get_or_404_deleted(id, msg='找不到指定商品') cat_list = FoodCat.query.all() if request.method == 'POST': form = SetForm().validate() with db.auto_commit(): if not food: food = Food() stock_ago = food.stock or 0 food.set_attrs(form.data) db.session.add(food) FoodStockChangeLog.set_stock_change_log(food.id, stock_ago, food.stock, '后台修改') return Success() return render_template('food/set.html', info=food, cat_list=cat_list, current='index')
def set(): form = SetCartForm().validate() id, number = form.id.data, form.number.data member = Member.query.get_or_404_deleted(g.member.id) food = Food.query.get_or_404_deleted(id, msg='指定商品不存在') if food.stock < number: raise UnderStock() cart = member.carts.filter_by(food_id=food.id).first() with db.auto_commit(): if not cart: cart = MemberCart() cart.food_id = food.id cart.member_id = member.id cart.quantity = number db.session.add(cart) return Success(msg='添加购物车成功')
def set(id): user = None if id: user = User.query.get_or_404_deleted(id, msg='对不起,找不到指定用户') if request.method == 'POST': form = SetForm().validate() with db.auto_commit(): if not user: user = User() user.nickname = form.nickname.data user.login_name = form.login_name.data user.password = form.login_pwd.data user.email = form.email.data user.mobile = form.mobile.data db.session.add(user) return Success() return render_template('account/set.html', user=user)
def add_comment(): form = AddCommentForm().validate() order_sn, score, content = form.order_sn.data, form.score.data, form.content.data member = Member.query.get_or_404_deleted(g.member.id, msg='找不到指定会员') order = member.orders.filter_by(order_sn=order_sn).first_or_404( msg='指定订单不存在') if order.order_status_enum == OrderStatus.DONE: raise OrderException(msg='订单已经评价过了, 不需要再评价了') food_id_list = [food.id for food in order.foods] food_ids_str = '_'.join(food_id_list) with db.auto_commit(): comment = MemberComment() comment.food_ids = food_ids_str comment.member_id = member.id comment.order_id = order.id comment.score = score comment.content = content db.session.add(comment) order.order_status_enum = OrderStatus.DONE db.session.add(order) return Success(msg='添加评论成功')
def cancel_order(order): """ 取消订单: 还原商品库存, 订单状态改为已关闭 :param order: :return: """ if order.order_status_enum != OrderStatus.UNPAID: raise OrderException(msg='要取消订单,订单必须是未支付状态') order_foods = order.order_foods with db.auto_commit(): for item in order_foods: food = item.food stock_ago = food.stock food.stock = food.stock + item.quantity item.remove() db.session.add(food) db.session.add(item) FoodStockChangeLog.set_stock_change_log( food.id, stock_ago, food.stock, '取消订单') order.order_status_enum = OrderStatus.CLOSE db.session.add(order) return Success(msg='取消订单成功')