2013-03-24 151 views
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_objectlist_object & c也被定義。我的問題是:如何減少冗餘? @login_required@roles_requiredflask-security提供。我如何優化這段代碼?

回答

1

當你有這些相同的設置,你可能想看看Flask-Restless Extension。如果這並不完全適合您的需求,您可以使用瓶的pluggable (class-based) views

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 

或者,您可以avoid writing stupid classes,只是用一個函數:

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