def test_production_config(): """Production config.""" app = create_app(ProdConfig) assert app.config['ENV'] == 'prod' assert app.config['DEBUG'] is False assert app.config['DEBUG_TB_ENABLED'] is False assert app.config['ASSETS_DEBUG'] is False
def create_app(self): # pass in test configurations config_name = 'testing' app = create_app(config_name) app.config.update( SQLALCHEMY_DATABASE_URI=get_connection_strs().test_str ) return app
def app(): """An application for the tests.""" _app = create_app(TestConfig) ctx = _app.test_request_context() ctx.push() yield _app ctx.pop()
def initialize_database(): try: flask_str = get_connection_strs().flask_str # CREATE CONFIG FILES logger.info("Creating configuration files") instance_folder = sys.prefix + "/var/webapp.app-instance" if not os.path.exists(instance_folder): os.makedirs(instance_folder) config_file = instance_folder + "/config.py" config_lines = [] track_mod_line = "SQLALCHEMY_TRACK_MODIFICATIONS=False\n" sql_config_line = "SQLALCHEMY_DATABASE_URI='{constr}'\n" sql_config_line = sql_config_line.format(constr=flask_str) secret_key_line = "SECRET_KEY='{secret}'\n" secret_key_line = secret_key_line.format(secret=rand_word(20)) if os.path.exists(config_file): with open(config_file, "r") as f: config_lines = f.readlines() has_uri_cfg = False has_secret = False has_track_mod = False for i in range(len(config_lines)): if re.search(".*SQLALCHEMY_DATABASE_URI.*", config_lines[i]): config_lines[i] = sql_config_line has_uri_cfg = True if re.search(".*SECRET_KEY.*", config_lines[i]): has_secret = True if re.search(".*SQLALCHEMY_TRACK_MODIFICATIONS.*", config_lines[i]): has_track_mod = True if not has_uri_cfg: config_lines.append(sql_config_line) if not has_secret: config_lines.append(secret_key_line) if not has_track_mod: config_lines.append(track_mod_line) with open(config_file, "w") as f: for l in config_lines: f.write(l) # CREATE CONFIG FILES logger.info("Configuring database") from webapp.app import db, create_app app = create_app('production') app.config.update(SQLALCHEMY_DATABASE_URI=flask_str) with app.app_context(): db.create_all() except Exception as err: logger.exception(err)
def get_context(): """ This context is necessary for using the flask models outside the app example: > with get_context(): > cls = Classifications.query.all() """ flask_str = get_connection_strs().flask_str logger.info("Creating app context") from webapp.app import create_app app = create_app('production') app.config.update(SQLALCHEMY_DATABASE_URI=flask_str) return app.app_context()
def create_app(self): app = create_app(testing=True) app.secret_key = "secret_key" app.config["WTF_CSRF_METHODS"] = [] return app
def create_app(self): app = create_app(testing=True) return app
import os from pathlib import Path HERE = Path(__file__).parent APP_TO_LOAD = os.environ.get('APP_TO_LOAD', 'webapp') # Comma-separated names that correspond to filenames in the config directory. CONFIGS_TO_LOAD = (os.environ.get('CONFIGS_TO_LOAD') or 'default').split(',') config_absolute_paths = [ Path(HERE, f'config/{config}.cfg') for config in CONFIGS_TO_LOAD ] if APP_TO_LOAD == 'webapp': from webapp.app import create_app app = create_app(config_absolute_paths) else: raise NotImplementedError(f'Invalid APP_TO_LOAD {APP_TO_LOAD}') if __name__ == '__main__': app.run()
def test_dev_config(): """Development config.""" app = create_app(DevConfig) assert app.config['ENV'] == 'dev' assert app.config['DEBUG'] is True
def create_app(self): app = create_app(testing=True) app.secret_key = "secret_key" app.config["WTF_CSRF_METHODS"] = [] return app
def create_app(self): app = create_app(testing=True) app.secret_key = "secret_key" return app
# python standard import os # local imports from webapp.app import create_app config_name = os.getenv('FLASK_CONFIG') or "production" app = create_app(config_name) if __name__ == '__main__': app.run()
def setUp(self): """ Set up Flask app for testing """ app = create_app(True) self.client = app.test_client()
def setUp(self): self.app = create_app({"SECRET_KEY": "pytest key"}) self.ctx = self.app.app_context() self.ctx.push() self.client = TestClient(self.app)
# ----------------------------------------------------------------------------- # Related Libraries Imports # ----------------------------------------------------------------------------- from flask_script import Manager, Server from flask_script.commands import ShowUrls, Clean # ----------------------------------------------------------------------------- # Local Imports # ----------------------------------------------------------------------------- from webapp.app import app, create_app from webapp.settings import DevConfig, ProdConfig HERE = os.path.abspath(os.path.dirname(__file__)) TEST_PATH = os.path.join(HERE, 'tests') app = create_app(app, DevConfig) manager = Manager(app) @manager.command def test(): """Run the tests.""" import pytest exit_code = pytest.main([TEST_PATH, '--verbose']) return exit_code # register commands with app manager # now to run app type: # $ python manage.py runserver
def app(): app = create_app(app, TestConfig) return app
def create_app(self): app = create_app(testing=True) app.config['WTF_CSRF_METHODS'] = [] return app
from webapp.app import create_app app = create_app() if __name__ == "__main__": app.run()
def create_app(self): app = create_app(testing=True) app.secret_key = "secret_key" return app
def create_app(self): app = create_app(testing=True) app.config["BLOG_CATEGORIES_ENABLED"] = "true" app.secret_key = "secret_key" return app
def create_app(self): app = create_app(testing=True) app.config["PRESERVE_CONTEXT_ON_EXCEPTION"] = False return app
import os import yaml from webapp.app import create_app secret_key = os.environ.get("SECRET_KEY", None) if not secret_key: # SECRET_KEY is set in github secrets with open("./secret.yaml") as f: secrets = yaml.load(f, Loader=yaml.SafeLoader)["env_variables"] secret_key = secrets["SECRET_KEY"] assert secret_key is not None, "Failed to get secret key from environ or secret.json" config = {"SECRET_KEY": secret_key, "PROFILE": True} if os.environ.get("DEV_OVERRIDE_USER"): config["DEV_OVERRIDE_USER"] = os.environ["DEV_OVERRIDE_USER"] app = create_app(config) if __name__ == "__main__": app.env = "development" app.run(host="127.0.0.1", port=8080, debug=True, ssl_context="adhoc")
def test_testing_config(): """Basic config""" app = create_app(TestConfig) assert app.config['ENV'] == 'test'
from subprocess import call from flask_migrate import Migrate, MigrateCommand from flask_script import Command, Manager, Option, Server, Shell from flask_script.commands import Clean, ShowUrls from webapp.app import create_app from webapp.database import db from webapp.settings import DevConfig, ProdConfig from webapp.user.models import User CONFIG = ProdConfig if os.environ.get('WEBAPP_ENV') == 'prod' else DevConfig HERE = os.path.abspath(os.path.dirname(__file__)) TEST_PATH = os.path.join(HERE, 'tests') app = create_app(CONFIG) manager = Manager(app) migrate = Migrate(app, db) def _make_context(): """Return context dict for a shell session so you can access app, db, and the User model by default.""" return {'app': app, 'db': db, 'User': User} @manager.command def test(): """Run the tests.""" import pytest exit_code = pytest.main([TEST_PATH, '--verbose']) return exit_code