def send_drift(gid): # filter_by(id=gid, launched=True)没有下面这条语句好。下面的语句可以多判断一种状态 current_gift = Gift.query.get_or_404(gid) # if current_gift.launched: # flash('这本书正处于交易状态,暂时不可以索要') # return redirect(url_for('web.book_detail', isbn=current_gift.isbn)) 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_satisfied_wish() if not can: return render_template('not_enough_beans.html', beans=current_user.beans) drift_form = DriftForm(request.form) if request.method == 'POST': if drift_form.validate(): save_drift(drift_form, current_gift) return redirect(url_for('web.pending')) # gift = Gift.query.filter(Gift.id == gid).first_or_404() gifter = current_gift.user.summary return render_template('drift.html', gifter=gifter, user_beans=current_user.beans, form=drift_form)
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_satisfied_wish() if not can: return render_template('not_enough_beans.html', beans=current_user.beans) # gifter = current_gift.user.summary drift_form = DriftForm(request.form) if request.method == 'POST': if drift_form.validate(): save_a_drift(drift_form, current_gift) # 异步发送,优化速度 send_email(current_gift.user.email, '有人想要一本书', 'email/get_gift', wisher=current_user, gift=current_gift) pass # return redirect(url_for('web.pending')) gifter = current_gift.user.summary return render_template('drift.html', gifter=gifter, user_beans=current_user.beans, form={'recipient_name':None, 'mobile':None, 'message':None, 'address':None}) pass
def send_drift(gid): current_gift = Gift.query.filter_by(id=gid).first_or_404() 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_satisfied_wish() if not can: return render_template('not_enough_beans.html', beans=current_user.beans) drift_form = DriftForm(request.form) if request.method == 'POST': if drift_form.validate(): save_a_drift(drift_form, current_gift) return redirect(url_for('web.drift:pending')) gifter = current_gift.user.summary return render_template('drift.html', gifter=gifter, user_beans=current_user.beans, form=drift_form)
def send_drift(gid): # filter_by(id=gid, launched=True)没有下面这条语句好。下面的语句可以多判断一种状态 current_gift = Gift.query.get_or_404(gid) # if current_gift.launched: # flash('这本书正处于交易状态,暂时不可以索要') # return redirect(url_for('web.book_detail', isbn=current_gift.isbn)) 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_satisfied_wish() if not can: return render_template('not_enough_beans.html', beans=current_user.beans) drift_form = DriftForm(request.form) if request.method == 'POST': if drift_form.validate(): DriftService.save_a_drift(drift_form, current_gift) # flash(drift_form.errors, category='drift_form_error') # else: # 反范式设计,会存储一部分冗余信息。 # 1. 减少查询次数 # 2. 交易记录本身就应该是历史记录,不应该动态改变 # with db.auto_commit(): # drift = Drift() # drift_form.populate_obj(drift) # drift.gift_id = gid # 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 = current_gift.book.title # drift.book_author = current_gift.book.author_str # drift.book_img = current_gift.book.image_large # db.session.add(drift) # send_email(current_gift.user.email, '有人想要一本书', 'email/get_gift', # wisher=current_user, # gift=current_gift) return redirect(url_for('web.pending')) # gift = Gift.query.filter(Gift.id == gid).first_or_404() gifter = current_gift.user.summary return render_template('drift.html', gifter=gifter, user_beans=current_user.beans, form=drift_form)