1
我有許多對象,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
提供。我如何優化這段代碼?