1

我有许多对象,UserRolePostCategory可能还有更多,并且有许多管理视图,基本上只显示和处理与这些对象关联的表单。我当前的代码如下所示:

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。如何优化此代码?

4

1 回答 1

1

当您拥有这些相同的设置时,您可能需要查看Flask-Restless Extension。如果这不能满足您的需求,您可以使用 Flask 的可插入(基于类)视图

from flask.views import View

LIST, NEW, EDIT, DELETE = "list", "new", "edit", "delete"
METHODS = (LIST, NEW, EDIT, DELETE)

class AbstractManager(View):
    DataClass = None
    Form = None

    methods = ["GET", "POST"]
    decorators = [login_required, create_roles_decorator_for("admin", "editor")]

    def dispatch_request(self, method=LIST, id=None):
        if not method in METHODS:
            abort(404)

        if method == LIST and id is not None:
            method = EDIT

        return getattr(self, method)(id)

    def list(self, id):
        if request.method != "GET":
            abort(405)
        return list_object(self.DataClass)

    def new(self, id):
        return create_object(self.DataClass, self.Form)

    def edit(self, id):
        return edit_object(self.DataClass, id, self.Form)

    def delete(self, id):
        return delete_object(self.DataClass, id)


class UserManager(AbstractManager):
    DataClass = User
    Form = UserForm


class CategoryManager(AbstractManager):
    DataClass = Category
    Form = CategoryForm

或者,您可以避免编写愚蠢的类而只使用一个函数:

def register_api_for(DataClass, ClassForm, name=None, app=None):
    name = name if name is not None else DataClass.__name__.rsplit(".", 1)[1]
    base_route = "/" + name

    @login_required
    @app.route(base_route, endpoint="list_" + name)
    def list():
        return list_object(DataClass)

    # remaining implementation left as an exercise for the reader
于 2013-03-24T02:47:01.943 回答