Search code examples
refactoringflaskflask-security

Optimise and reduce redundancy in Flask routing


I have a number of objects, User, Role, Post, Category and possibly more, and have a number of admin views that basically just display and handle forms associated with those objects. My current code looks like this:

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)

And so on. edit_object, list_object &c are defined as well. My question is: how can I reduce the redundancy here? @login_required and @roles_required are provided by flask-security. How can I optimise this code?


Solution

  • When you have these sorts of identical setups you may want to look into the Flask-Restless Extension. If that doesn't quite suite your needs you can use Flask's 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
    

    Alternately, you can avoid writing stupid classes and just use a function:

    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