import os
from flask import Flask, jsonify, g
from flask.ext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()


def create_app(config_name):
    """Create an application instance.

    This is called by a runner script, such as /run.py.
    """
    from .auth import password_auth

    app = Flask(__name__)

    # apply configuration
    cfg = os.path.join(os.getcwd(), 'config', config_name + '.py')
    app.config.from_pyfile(cfg)

    # initialize extensions
    db.init_app(app)

    # register blueprints
    from .api_v1 import api as api_blueprint
    app.register_blueprint(api_blueprint, url_prefix='/v1')

    # authentication token route
    @app.route('/token')
    @password_auth.login_required
    def get_auth_token():
        return jsonify({'token': g.user.generate_auth_token()})

    return app

import os
from flask import Flask, jsonify, g
from flask.ext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()


def create_app(config_name):
    """Create an application instance.

    This is called by a runner script, such as /run.py.
    """
    from .auth import password_auth

    app = Flask(__name__)

    # apply configuration
    _this_dir = os.path.dirname(os.path.abspath(__file__))
    cfg = os.path.join(_this_dir, '../config', config_name + '.py')
    app.config.from_pyfile(cfg)

    # initialize extensions
    db.init_app(app)

    # register blueprints
    from .api_v1 import api as api_blueprint
    app.register_blueprint(api_blueprint, url_prefix='/v1')

    # authentication token route
    @app.route('/token')
    @password_auth.login_required
    def get_auth_token():
        return jsonify({'token': g.user.generate_auth_token()})

    return app

Allow app to be imported
Allow app to be imported

Flask httpdomain needs to import the Flask app. It turns out the the
way to get the path to the configuration file assumed the app was always
being run/imported from the base of the ltd-keeper repo, which isn't
always true, especially for Sphinx doc builds.

This correctly derives a path from the absolute path of the Python
module itself.

For DM-5100.

  import os
  from flask import Flask, jsonify, g
  from flask.ext.sqlalchemy import SQLAlchemy
  
  db = SQLAlchemy()
  
  
  def create_app(config_name):
      """Create an application instance.
  
      This is called by a runner script, such as /run.py.
      """
      from .auth import password_auth
  
      app = Flask(__name__)
  
      # apply configuration
+     _this_dir = os.path.dirname(os.path.abspath(__file__))
-     cfg = os.path.join(os.getcwd(), 'config', config_name + '.py')
+     cfg = os.path.join(_this_dir, '../config', config_name + '.py')
      app.config.from_pyfile(cfg)
  
      # initialize extensions
      db.init_app(app)
  
      # register blueprints
      from .api_v1 import api as api_blueprint
      app.register_blueprint(api_blueprint, url_prefix='/v1')
  
      # authentication token route
      @app.route('/token')
      @password_auth.login_required
      def get_auth_token():
          return jsonify({'token': g.user.generate_auth_token()})
  
      return app
  
Allow app to be imported
## Code Before:
import os
from flask import Flask, jsonify, g
from flask.ext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()


def create_app(config_name):
    """Create an application instance.

    This is called by a runner script, such as /run.py.
    """
    from .auth import password_auth

    app = Flask(__name__)

    # apply configuration
    cfg = os.path.join(os.getcwd(), 'config', config_name + '.py')
    app.config.from_pyfile(cfg)

    # initialize extensions
    db.init_app(app)

    # register blueprints
    from .api_v1 import api as api_blueprint
    app.register_blueprint(api_blueprint, url_prefix='/v1')

    # authentication token route
    @app.route('/token')
    @password_auth.login_required
    def get_auth_token():
        return jsonify({'token': g.user.generate_auth_token()})

    return app

## Instruction:
Allow app to be imported
## Code After:
import os
from flask import Flask, jsonify, g
from flask.ext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()


def create_app(config_name):
    """Create an application instance.

    This is called by a runner script, such as /run.py.
    """
    from .auth import password_auth

    app = Flask(__name__)

    # apply configuration
    _this_dir = os.path.dirname(os.path.abspath(__file__))
    cfg = os.path.join(_this_dir, '../config', config_name + '.py')
    app.config.from_pyfile(cfg)

    # initialize extensions
    db.init_app(app)

    # register blueprints
    from .api_v1 import api as api_blueprint
    app.register_blueprint(api_blueprint, url_prefix='/v1')

    # authentication token route
    @app.route('/token')
    @password_auth.login_required
    def get_auth_token():
        return jsonify({'token': g.user.generate_auth_token()})

    return app
