11.7 其他操作

1.更好的使用枚举

我们的数据库中pending存储的是数字类型,但是我们在代码中使用的是枚举类型。这肯定是匹配不上的,一种最优雅的解决方式就是为我们的Drift模型的pending属性编写getter/setter方法

    # 状态
    _pending = Column('pending', SmallInteger, default=1)

    @property
    def pending(self):
        return PendingStatus(self._pending)

    @pending.setter
    def pending(self, status):
        self._pending = status.value

这样就能在外部使用枚举类型操作我们的属性了

2.撤销操作业务逻辑

@web.route('/drift/<int:did>/redraw')
@login_required
def redraw_drift(did):
    with db.auto_commit():
        # 横向越权:一个用户访问另一个用户的数据并进行修改
        # requester_id=current_user.id 防止横向越权
        drift = Drift.query.filter_by(
            id=did, requester_id=current_user.id).first_or_404()
        drift.pending = PendingStatus.Redraw
        current_user.beans += 1

    return redirect(url_for('web.pending'))

3.拒绝操作业务逻辑

4.完成邮寄

5.撤销赠送

6.撤销心愿

7.赠送书籍

email/satisify_wish.html的内容

Last updated

Was this helpful?