def save_to_wish(isbn): if current_user.can_save_to_list(isbn): with db.auto_commit(): wish = Wish() wish.isbn = isbn wish.uid = current_user.id wish.create_time = datetime.now() db.session.add(wish) else: flash('您已经有书籍在赠送或者心愿清单中,请勿重复添加!') return redirect(url_for('web.book_detail', isbn=isbn))
def redraw_from_wish(isbn): """ 撤销心愿 :param isbn:当前的书籍编号 :return: 跳转到当前礼物页面 """ wish = Wish.query.filter( Wish.isbn == isbn, Wish.launched == False, User.id == current_user.id).first_or_404() with db.auto_commit(): wish.delete() return redirect(url_for('web.my_wish'))
def save_to_gifts(isbn): if current_user.can_save_to_list(isbn): with db.auto_commit(): gift = Gift() gift.isbn = isbn gift.uid = current_user.id # current_user 实际上就是通过模型返回的USER类,里面有一个id gift.create_time = datetime.now() current_user.beans += current_app.config['BEANS_UPLOAD_ONE_BOOK'] db.session.add(gift) # 使用db.session来进行数据库的提交操作 else: flash('您已经有书籍在赠送或者心愿清单中,请勿重复添加!') # 此处注意url_for的写法 return redirect(url_for('web.book_detail', isbn=isbn))
def reject_drift(did): """ (赠送者)拒绝当前的交易 :param did: 礼物id号 :return:返回拒绝信息到当前的页面 """ with db.auto_commit(): drift = Drift.query.filter(Drift.id == did, Gift.uid == current_user.id).first_or_404() drift.pending = PendingStatus.Reject.value # 如果拒绝了这个交易,那么请求者的鱼豆需要+1(将鱼豆还给他) requester = User.query.get_or_404(drift.requester_id) requester.beans += 1 return redirect(url_for('web.pending'))
def redraw_drift(did): """ 撤销当前的交易 :param did: :return: 为了防止超权现象(通过自定义的URL来操作别人的鱼漂),还必须加一个requester.id =current_user.id """ with db.auto_commit(): drift = Drift.query.filter_by( id=did, requester_id=current_user.id).first_or_404() # 需要特别注意的是,直接读取PendingStatus.Redraw读出来的是枚举类型,需要增加.value才是真实的值 drift.pending = PendingStatus.Redraw.value current_user.beans += 1 return redirect(url_for('web.pending'))
def redraw_from_gifts(gid): """ 撤销礼物-前置条件,先判断鱼漂的操作是否是等待操作 :param gid: :return: """ gift = Gift.query.filter_by(id=gid).first_or_404() drift = Drift.query.filter_by(gift_id=gift.id).first() if drift and drift.pending == PendingStatus.Waiting.value: flash('您还有交易没有完成,请先完成交易再撤销礼物') else: with db.auto_commit(): # 返回鱼豆 current_user.beans -= current_app.config['BEANS_UPLOAD_ONE_BOOK'] # 删除礼物 gift.delete() return redirect(url_for('web.my_gifts'))
def mailed_drift(did): """ 点击已邮寄,完成此次交易,注意,要讲赠送清单与心愿清单同样撤销掉 :param did: :return: """ with db.auto_commit(): drift = Drift.query.filter_by(id=did).first_or_404() drift.pending = PendingStatus.Success.value # 撤销赠送者的礼物(Gift) gift = Gift.query.filter_by(id=drift.gift_id).first() if gift: gift.launched = True # 撤销请求者的心愿(Wish) wish = Wish.query.filter_by(isbn=drift.isbn, uid=drift.requester_id, launched=False).first() if wish: wish.launched = True return redirect(url_for('web.pending'))
def save_drift(drift_form, mygift): """保存交易信息方法""" with db.auto_commit(): drift = Drift() result = Gift.book(mygift.isbn) # 在数据一致的前提下,将drift_form中的字段保存到drift模型中 drift_form.populate_obj(drift) # 书籍信息 drift.isbn = result['isbn'] drift.book_title = result['title'] drift.book_author = result['author'] drift.book_img = result['image'] # 赠送人信息 drift.gifter_id = mygift.user.id drift.gifter_nickname = mygift.user.nickname drift.gift_id = mygift.id # 索要人信息 drift.requester_id = current_user.id drift.requester_nickname = current_user.nickname # 状态值 pending = PendingStatus.Waiting db.session.add(drift) # 用户的鱼豆-1 current_user.beans -= 1