def send_drift(gid): current_gift = Gift.query.get_or_404(gid) if current_gift.is_yourself_gift(current_user.id): flash('这本书是你自己的^_^, 不能向自己索要书籍噢') return redirect(url_for('web.book_detail', isbn=current_gift.isbn)) can = current_user.can_send_drift() if not can: return render_template('not_enough_beans.html', beans=current_user.beans) form = DriftForm(request.form) if request.method == 'POST' and form.validate(): save_drift(form, current_gift) send_user = User.query.get(current_gift.uid) send_mail(send_user.email, '有人想要一本书', 'email/get_gift.html', wisher=current_user, gift=current_gift) return redirect(url_for('web.pending')) gifter = User.query.get(current_gift.uid).summary return render_template('drift.html', gifter=gifter, user_beans=current_user.beans, form=form)
def save_drift(drift_form, current_gift): with db.auto_commit(): book = BookViewModel(current_gift.book) drift = Drift() 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 drift.book_title = book.title drift.book_author = book.author drift.book_img = book.image drift.isbn = book.isbn # 当请求生成时,不需要让这个礼物处于锁定状态 # 这样赠送者是可以收到多个索取请求的,由赠送者选择送给谁 # current_gift.launched = True # 请求者鱼豆-1 current_user.beans -= 1 # 但是赠送者鱼豆不会立刻+1 # current_gift.user.beans += 1 db.session.add(drift) send_mail(current_gift.user.email, '有人想要一本书', 'email/get_gift', wisher=current_user, gift=current_gift)
def forget_password_request(): form = EmailForm(request.form) if request.method == "POST": if form.validate(): account_email = form.email.data user = User.query.filter_by(email=account_email).first_or_404() send_mail(account_email, "重置你的密码", "email/reset_password.html", user=user, token=user.generate_token()) flash("一封邮件已经发送到邮箱" + account_email + ",请及时查收") # return redirect(url_for("Web.login")) return render_template("auth/forget_password_request.html", form=form)
def satisfy_wish(wid): wish = Wish.query.get_or_404(wid) gift = Gift.query.filter_by(uid=current_user.id, isbn=wish.isbn).first() if not gift: flash('你还没有上传此书,' '请点击“加入到赠送清单”添加此书。添加前,请确保你可以赠送此书。') else: send_mail(wish.user.email, '有人想送你一本书', 'email/satisfy_wish.html', wish=wish, gift=gift) flash('已向他/她发送了一封邮件,如果他/她愿意接受你的赠送,你将接收到一个鱼漂') return redirect(url_for('web.book_detail', isbn=wish.isbn))
def forget_password_request(): """ 忘记密码申请视图函数 :return: """ form = EmailForm(request.form) if request.method == 'POST' and form.validate(): account_email = form.email.data user = User.query.filter_by(email=account_email).first_or_404() # 向用户发送电子邮件 send_mail(account_email, '重置你的密码', 'email/reset_password.html', user=user, token=user.generate_token()) flash('一封邮件已发送到邮箱' + account_email + ', 请及时查收') # return redirect(url_for('web.login')) return render_template('auth/forget_password_request.html', form=form)
def send_drift(gid): """ 发起鱼漂交易 :param gid: 礼物 id :return: """ # 查找礼物 current_gift = Gift.query.get_or_404(gid) # 自己不能向自己请求书籍 if current_gift.is_yourself_gift(current_user.id): flash('这本书是你自己的^_^,不能向自己所要书籍噢') return redirect(url_for('web.book_detail', isbn=current_gift.isbn)) # 能否发起鱼漂 can = current_user.can_send_drift() if not can: return render_template('not_enough_beans.html', beans=current_user.beans) form = DriftForm(request.form) if request.method == 'POST' and form.validate(): save_to_drift(form, current_gift) # 向赠书者发送一份邮件提醒有人向他索取 send_mail(current_gift.user.email, '有人想要一本书', 'email/get_gift.html', wisher=current_user, gift=current_gift) return redirect(url_for('web.pending')) # 赠送者简要信息 gifter = current_gift.user.summary return render_template('drift.html', gifter=gifter, user_beans=current_user.beans, form=form)