我有许多对象,User
,Role
,Post
,Category
可能还有更多,并且有许多管理视图,基本上只显示和处理与这些对象关联的表单。我当前的代码如下所示:
admin = Blueprint('admin', __name__)
@login_required
@admin.route('/users')
def users():
return list_object(User)
@roles_required('admin')
@admin.route('/users/new', methods = ['GET', 'POST'])
def create_user():
return create_object(User, UserForm)
@roles_required('admin')
@admin.route('/users/delete/<int:user_id>', methods = ['GET', 'POST'])
def delete_user(user_id):
return delete_object(User, user_id)
@roles_required('admin')
@admin.route('/users/<int:user_id>', methods = ['GET', 'POST'])
def edit_user(user_id):
return edit_object(User, user_id, UserForm)
@login_required
@admin.route('/categories')
def categories():
return list_object(Category)
@roles_accepted('admin', 'editor')
@admin.route('/categories/new', methods = ['GET', 'POST'])
def create_cat():
return create_object(Category, CategoryForm)
@roles_accepted('admin', 'editor')
@admin.route('/categories/delete/<int:cat_id>', methods = ['GET', 'POST'])
def delete_cat(cat_id):
return delete_object(Category, cat_id)
@roles_accepted('admin', 'editor')
@admin.route('/categories/<int:cat_id>', methods = ['GET', 'POST'])
def edit_cat(cat_id):
return edit_object(Category, cat_id, CategoryForm)
等等。edit_object
, list_object
&c 也被定义。我的问题是:我怎样才能减少这里的冗余?@login_required
并由@roles_required
提供flask-security
。如何优化此代码?