def reject_drift(did): # 拒绝请求 为书籍赠送者操作 with db.auto_db_commit(): drift = Drift.query.filter_by(gifter_id=current_user.id, id=did).first_or_404() drift.pending = PendingStatus.Reject requester = User.query.get_or_404(drift.requester_id) requester.beans += 1 return redirect(url_for('web.pending'))
def redraw_drift(did): #撤销请求 为书籍索要者操作 #注意超权现象 用户登陆后修改传过来的did 来操作别人的数据 #解决:判断当前用户身份和did归属性 with db.auto_db_commit(): drift = Drift.query.filter_by(requester_id=current_user.id, id=did).first_or_404() drift.pending = PendingStatus.Redraw current_user.beans += 1 return redirect(url_for('web.pending'))
def save_to_wish(isbn): if current_user.can_save_to_list(isbn): with db.auto_db_commit(): wish = Wish() wish.isbn = isbn wish.uid = current_user.id db.session.add(wish) else: flash('这本书已添加至你的赠送清单或已存在于你的心愿清单,请不要重复添加') return redirect(url_for('web.book_detail', isbn=isbn))
def reset_password(token, new_password): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token.encode('utf-8')) #判断data的token 可能token为伪造的 except: return False uid = data.get('id') with db.auto_db_commit(): user = User.query.get(uid) user.password = new_password return True
def redraw_from_gifts(gid): #撤销赠送清单 gift = Gift.query.filter_by(id=gid, launched=False).first_or_404() drift = Drift.query.filter_by(gift_id=gid, pending=PendingStatus.Waiting).first() if drift: flash('该书籍处于交易状态,请先前往鱼漂处理该交易') else: with db.auto_db_commit(): current_user.beans -= current_app.config['BEANS_UPLOAD_ONE_BOOK'] gift.delete() return redirect(url_for('web.my_gifts'))
def mailed_drift(did): with db.auto_db_commit(): drift = Drift.query.filter_by(gifter_id=current_user.id, id=did).first_or_404() drift.pending = PendingStatus.Success current_user.beans += 1 gift = Gift.query.filter_by(id=drift.gift_id).first_or_404() gift.launched = True wish = Wish.query.filter_by(isbn=drift.isbn, uid=drift.requester_id, launched=False).first_or_404() wish.launched = True return redirect(url_for('web.pending'))
def save_to_gifts(isbn): #假如事务提交失败了,需进行回滚 rollback if current_user.can_save_to_list(isbn): # try: # gift = Gift() # gift.uid = current_user.id #current_user指代当前登陆的用户 通过def get_user(uid):获取当前用户信息 # # current_user.beans += 0.5 # current_user.beans += current_app.config['BEANS_UPLOAD_ONE_BOOK'] # db.session.add(gift) # db.session.commit() # except Exception as e: # db.session.rollback() #假如commit或者前面流程报错,则进行事务回滚 # raise e #抛出异常 with db.auto_db_commit(): gift = Gift() gift.isbn = isbn gift.uid = current_user.id current_user.beans += current_app.config['BEANS_UPLOAD_ONE_BOOK'] db.session.add(gift) else: flash('这本书已添加至你的赠送清单或已存在于你的心愿清单,请不要重复添加') return redirect(url_for('web.book_detail', isbn=isbn))
def save_drift(drift_form, current_gift): with db.auto_db_commit(): drift = Drift() # drift.message = drift_form.message.data drift_form.populate_obj(drift) #保存请求者 赠送者信息到数据库 drift.gift_id = current_gift.id drift.requester_id = current_user.id drift.requester_nickname = current_user.nickname drift.gifter_nickname = current_gift.user.nickname drift.gifter_id = current_gift.user.id #保存书籍信息到数据库 book = current_gift.book drift.book_title = book['title'] drift.book_author = book['author'] drift.book_img = book['image'] drift.isbn = book['isbn'] current_user.beans -= 1 db.session.add(drift)
def redraw_from_wish(isbn): wish = Wish.query.filter_by(isbn=isbn, launched=False).first_or_404() with db.auto_db_commit(): wish.delete() return redirect(url_for('web.my_wish'))