I'm currently working on a web app where I'm using Flask and the Flask-User extension to handle authorization. At the moment I have something like this:
@application.route('/dummy')
@roles_required('Admin')
@login_required
def dummy():
in my views.py
file and something like this:
{% if current_user.is_authenticated and current_user.has_roles('Admin') %}
in my templates.
Now, there's been a few times where I've changed my mind in terms of role names and then had to search through the whole project to find and replace those usages. I wanted to define something like a ADMIN_ROLE
global that can be accessed inside my views.py
or any templates. I'd then just change a config file whenever I wanted to change a role's name.
I tried using my config.py
setting ADMIN_ROLE = "Admin"
and writing @roles_required(current_app.config['ADMIN_ROLE'])
but that gives me the following error:
RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed to interface with the current application object in a way. To solve this set up an application context with app.app_context(). See the documentation for more information.
so that should be wrong. What would be the most correct way to accomplish what I'm trying?