1

I built a function wrapper using this method:

def parametrized(dec):

    def layer(*args, **kwargs):
        def repl(f):
            return dec(f, *args, **kwargs)

        return repl

    return layer


@parametrized
def role_required(f, roles):
    print(roles)

    def decorated_function(*args, **kwargs):
        print('in dec func') # never called
        auth_mod_used = 'auth' in app.blueprints.keys()
        if auth_mod_used:
            print(g.user.role.lower())
            print(g.user.role.lower() in (role.lower for role in roles))
            if g.user is None or g.user.role.lower() not in (role.lower for role in roles):
                flash('You are not authorized to preform this action.', 'danger')
                # TODO: log to logging service
                return redirect(url_for('home.index'))
            return f(*args, **kwargs)
        return f(*args, **kwargs)

    return decorated_function

It's intended purpose is to protect a route by only allowing specific roles to get through. I attempt to use it thusly:

@mod_lp.route('/add', methods=['POST'])
@role_required(['admin', 'principal'])
def add():
    form = LessonPlanForm()
    if form.validate_on_submit():
        lp = LessonPlan(form.name.data, form.class_day.data)
        db.session.add(lp)
        db.session.commit()
    return redirect(url_for('lesson_plan.index'))

error:

Could not build url for endpoint 'lesson_plan.add'

Jeff
  • 4,285
  • 15
  • 63
  • 115

1 Answers1

1

Found this issue. The parameterized function never called the function that was passed into it. I needed this line:

@wraps(f)

in the function, like this:

@parametrized
def role_required(f, roles):
    @wraps(f)
    def decorated_function(*args, **kwargs):
    # and so on
Jeff
  • 4,285
  • 15
  • 63
  • 115