#170 initialize FAS only when needed
Merged 3 years ago by kparal. Opened 3 years ago by kparal.

file modified
-6
@@ -3,18 +3,15 @@ 

  import os

  

  from flask import Flask, render_template

- from flask_fas_openid import FAS

  from flask_sqlalchemy import SQLAlchemy

  

  from . import config

- from .util.login import FakeFas

  

  # the version as used in setup.py and docs

  __version__ = "1.1.0"

  

  # Flask App

  app = Flask(__name__)

- fas = None

  # Is this an OpenShift deployment?

  openshift = os.getenv('OPENSHIFT_PROD')

  
@@ -30,15 +27,12 @@ 

  if os.getenv('DEV') == 'true':

      app.logger.debug('Using development config')

      app.config.from_object('blockerbugs.config.DevelopmentConfig')

-     fas = FakeFas(app)

  elif os.getenv('TEST') == 'true' or openshift == "0":

      app.logger.debug('Using testing config')

      app.config.from_object('blockerbugs.config.TestingConfig')

-     fas = FAS(app)

  else:

      app.logger.debug('Using production config')

      app.config.from_object('blockerbugs.config.ProductionConfig')

-     fas = FAS(app)

  if openshift:

      app.logger.debug('Using openshift config')

      config.openshift_config(app.config, openshift)

@@ -23,10 +23,13 @@ 

  # Liberally borrows from the sample app in Flask-FAS maintained by Ian Weller

  

  from flask import redirect, url_for, request, g, abort

+ from flask_fas_openid import fas_login_required

  

- from blockerbugs import app, fas

+ from blockerbugs import app

+ import blockerbugs.util.login

+ 

+ fas = blockerbugs.util.login.getFAS()

  

- from flask_fas_openid import fas_login_required

  

  @app.route('/auth_login')

  def auth_login():
@@ -49,6 +52,7 @@ 

          app.logger.debug('No FAS user to logout')

      return redirect(url_for('main.index'))

  

+ 

  @fas_login_required

  def check_admin_rights():

      if app.config['FAS_ADMIN_GROUP'] in g.fas_user.groups:

file modified
+22 -1
@@ -22,9 +22,30 @@ 

  

  import munch

  from flask import g, request, redirect

+ from flask_fas_openid import FAS

  

+ from blockerbugs import app

  

- class FakeFas(object):

+ 

+ #: a singleton FAS instance. Use getFAS() to access it.

+ _fas = None

+ 

+ 

+ def getFAS():

+     '''Return either the real or a fake FAS object depending on the app config'''

+     global _fas

+     if _fas is not None:

+         return _fas

+ 

+     if (app.config['PRODUCTION'] or app.config['TESTING']):

+         _fas = FAS(app)

+     else:  # development

+         _fas = FakeFAS(app)

+ 

+     return _fas

+ 

+ 

+ class FakeFAS(object):

      fake_user_groups = ['qa-admin']

      fake_user = munch.Munch(username='developer',

                              groups=fake_user_groups)

It's good to avoid unnecessary code in __init__.py. FAS can be loaded only
when it's needed. Thanks to this, developers can finally avoid specifying
DEV=true before every command (if they modify their settings.py accordingly),
because FAS instance is now loaded based on config values and not envvars.


This depends on #169.

Build succeeded.

Commit a388752 fixes this pull-request

Pull-Request has been merged by kparal

3 years ago