How to make the Flask-OpenID object global to the app? How to make the Flask-OpenID object global to the app? flask flask

How to make the Flask-OpenID object global to the app?


The three options have various trade offs (as you have discovered):

  1. Create auth in __init__.py - this results in circular references between your views and your base app - it can work, but it makes it more difficult to factor (as moving two otherwise unrelated imports can result in an error).
  2. Create a separate module to handle the separate concern - this avoids the circular reference problem and adds clarity to larger projects ("ah, here is where the code dealing with authentication belongs"). On the other hands, small projects with several small extensions can wind up with a Java-like explosion of modules.
  3. Create a separate module to initialize all of the Flask extensions that are used in the application. This avoids the circular reference problem too and keeps small projects from multiplying modules unnecessarily. However, on larger projects, such a module becomes a big-ball-of-mud. ("This is where the authentication, flat-file processing, and error handling are configured").

There is also a 4th option - add auth to your view layer, as how you are authenticated is specific to your application logic (rather than your domain model, for example).

I would recommend avoiding #1 entirely (the extra cost of skipping the "two modules" phase to go straight to the "three or more modules phase" is negligible). Which of the remaining three options is right for your project is really specific to the project and the developers working on it.