def app(request): """Yield the application instance.""" database = getattr(request.module, 'database', 'db.sqlite3') exclude_tables = getattr(request.module, 'exclude_tables', None) test_database_path = os.path.join('tests', 'data', 'test_db.sqlite3') pristine_database_path = os.path.join('tests', 'data', database) shutil.copy(pristine_database_path, test_database_path) model_module = getattr(request.module, 'model_module', None) user_models = [] if model_module: module = importlib.import_module(model_module) for name, obj in inspect.getmembers(module): if inspect.isclass(obj): if name != 'AutomapModel': user_models.append(obj) application = get_app('sqlite+pysqlite:///{}'.format(test_database_path), user_models=user_models, exclude_tables=exclude_tables) application.testing = True yield application with application.app_context(): db.session.remove() db.drop_all() os.unlink(test_database_path)
def app(request): """Yield the application instance.""" database = getattr(request.module, 'database', 'db.sqlite3') exclude_tables = getattr(request.module, 'exclude_tables', None) test_database_path = os.path.join('tests', 'data', 'test_db.sqlite3') pristine_database_path = os.path.join('tests', 'data', database) shutil.copy(pristine_database_path, test_database_path) model_module = getattr(request.module, 'model_module', None) user_models = [] if model_module: module = importlib.import_module(model_module) for name, obj in inspect.getmembers(module): if inspect.isclass(obj): if name != 'AutomapModel': user_models.append(obj) application = get_app( 'sqlite+pysqlite:///{}'.format( test_database_path), user_models=user_models, exclude_tables=exclude_tables) application.testing = True yield application with application.app_context(): db.session.remove() db.drop_all() os.unlink(test_database_path)
def main(): """Main entry point for script.""" parser = argparse.ArgumentParser( description='Auto-generate a RESTful API service ' 'from an existing database.' ) parser.add_argument( 'URI', help='Database URI in the format ' 'postgresql+psycopg2://user:password@host/database') parser.add_argument( '-d', '--debug', help='Turn on debug logging', action='store_true', default=False) parser.add_argument( '-p', '--port', help='Port for service to listen on', default=5000) parser.add_argument( '-l', '--local-only', help='Only provide service on localhost (will not be accessible' ' from other machines)', action='store_true', default=False) parser.add_argument( '-r', '--read-only', help='Make all database resources read-only (i.e. only the HTTP GET method is supported)', action='store_true', default=False) parser.add_argument( '-s', '--schema', help='Use this named schema instead of default', default=None) parser.add_argument( '-e', '--enable-cors', help='Enable Cross Origin Resource Sharing (CORS)', default=False) args = parser.parse_args() app = get_app(args.URI, read_only=args.read_only, schema=args.schema) if args.enable_cors: from flask_cors import CORS CORS(app) if args.debug: app.config['DEBUG'] = True if args.local_only: host = '127.0.0.1' else: host = '0.0.0.0' app.config['SECRET_KEY'] = '42' app.run(host=host, port=int(args.port))
def main(): """Main entry point for script.""" parser = argparse.ArgumentParser( description='Auto-generate a RESTful API service ' 'from an existing database.' ) parser.add_argument( '--URI', help='Database URI in the format ' 'postgresql+psycopg2://user:password@host/database', default='sqlite+pysqlite:///db.sqlite') parser.add_argument( '-d', '--debug', help='Turn on debug logging', action='store_true', default=True) parser.add_argument( '-p', '--port', help='Port for service to listen on', default=5000) parser.add_argument( '-l', '--local-only', help='Only provide service on localhost (will not be accessible' ' from other machines)', action='store_true', default=False) parser.add_argument( '-r', '--read-only', help='Make all database resources read-only (i.e. only the HTTP GET method is supported)', action='store_true', default=False) args = parser.parse_args() app = get_app(args.URI, read_only=args.read_only) app.secret_key = '42' if args.debug: app.config['DEBUG'] = True if args.local_only: host = '127.0.0.1' else: host = '0.0.0.0' CORS(app) from flask import request @app.before_request def log_request(): print(request.data) app.run(host=host, port=int(args.port))
def main(): """Main entry point for script.""" parser = argparse.ArgumentParser( description='Auto-generate a RESTful API service ' 'from an existing database.' ) parser.add_argument( 'URI', help='Database URI in the format ' 'postgresql+psycopg2://user:password@host/database') parser.add_argument( '-d', '--debug', help='Turn on debug logging', action='store_true', default=False) parser.add_argument( '-p', '--port', help='Port for service to listen on', default=5000) parser.add_argument( '-l', '--local-only', help='Only provide service on localhost (will not be accessible' ' from other machines)', action='store_true', default=False) parser.add_argument( '-r', '--read-only', help='Make all database resources read-only (i.e. only the HTTP GET method is supported)', action='store_true', default=False) parser.add_argument( '-s', '--schema', help='Use this named schema instead of default', default=None) args = parser.parse_args() app = get_app(args.URI, read_only=args.read_only, schema=args.schema) if args.debug: app.config['DEBUG'] = True if args.local_only: host = '127.0.0.1' else: host = '0.0.0.0' app.config['SECRET_KEY'] = '42' app.run(host=host, port=int(args.port))
def main(): """Main entry point for script.""" parser = argparse.ArgumentParser( description='Auto-generate a RESTful API service ' 'from an existing database.' ) parser.add_argument( 'URI', help='Database URI in the format ' 'postgresql+psycopg2://user:password@host/database') parser.add_argument( '-d', '--debug', help='Turn on debug logging', action='store_true', default=False) parser.add_argument( '-p', '--port', help='Port for service to listen on', default=5000) parser.add_argument( '-l', '--local-only', help='Only provide service on localhost (will not be accessible' ' from other machines)', action='store_true', default=False) args = parser.parse_args() app = get_app(args.URI) if args.debug: app.config['DEBUG'] = True if args.local_only: host = '127.0.0.1' else: host = '0.0.0.0' app.run(host=host, port=int(args.port))
def make_app(): app = sandman2.get_app(config.SQLA_URI, Base=utils.AutomapModel) app.json_encoder = utils.APIJSONEncoder app.config['CASE_INSENSITIVE'] = config.CASE_INSENSITIVE app.config['BASIC_AUTH_USERNAME'] = os.environ.get('AUTOAPI_ADMIN_USERNAME', '') app.config['BASIC_AUTH_PASSWORD'] = os.environ.get('AUTOAPI_ADMIN_PASSWORD', '') CORS(app) basic_auth = BasicAuth(app) @app.before_request def refresh(): tables = utils.get_tables() if tables != app.config['SQLALCHEMY_TABLES']: utils.refresh_tables() app.config['SQLALCHEMY_TABLES'] = tables @app.before_request def protect_admin(): if request.path.startswith('/admin/'): if not basic_auth.authenticate(): return basic_auth.challenge() aws_blueprint = aws.make_blueprint() app.register_blueprint(aws_blueprint) docs_blueprint = swagger.make_blueprint(app) app.register_blueprint(docs_blueprint) route = os.path.join('/api-program', config.API_NAME) container = DispatcherMiddleware(app.wsgi_app, {route: app}) with app.app_context(): app.config['SQLALCHEMY_TABLES'] = utils.get_tables() utils.activate() return app, container
from sandman2 import get_app app = get_app('sqlite+pysqlite:///db.sqlite3') if __name__ == '__main__': app.run(debug=True)
def make_app(): app = sandman2.get_app(config.SQLA_URI, Base=utils.AutomapModel) app.json_encoder = utils.APIJSONEncoder app.config['CASE_INSENSITIVE'] = config.CASE_INSENSITIVE app.config['BASIC_AUTH_USERNAME'] = os.environ.get( 'AUTOAPI_ADMIN_USERNAME', '') app.config['BASIC_AUTH_PASSWORD'] = os.environ.get( 'AUTOAPI_ADMIN_PASSWORD', '') CORS(app) basic_auth = BasicAuth(app) @app.before_request def refresh(): tables = utils.get_tables() if tables != app.config['SQLALCHEMY_TABLES']: utils.refresh_tables() app.config['SQLALCHEMY_TABLES'] = tables @app.before_request def protect_admin(): if request.path.startswith('/admin/'): if not basic_auth.authenticate(): return basic_auth.challenge() class RefreshTables(View): task_name = 'refresh' def dispatch_request(self): underway = RefreshLog.refresh_underway() if underway: return '''Refresh begun at {} still underway. Now: {}; timeout set for {} seconds'''.format( underway.begun_at, datetime.now(), config.REFRESH_TIMEOUT_SECONDS) try: subprocess.Popen(['invoke', self.task_name, ]) except Exception as e: print('Problem with table refresh:') print(e) return 'Table refresh requested.' class QuickRefreshTables(RefreshTables): task_name = 'quick_refresh' app.add_url_rule('/refresh/', view_func=RefreshTables.as_view('refresh')) app.add_url_rule('/quick_refresh/', view_func=QuickRefreshTables.as_view('quick_refresh')) aws_blueprint = aws.make_blueprint() app.register_blueprint(aws_blueprint) docs_blueprint = swagger.make_blueprint(app) app.register_blueprint(docs_blueprint) with app.app_context(): app.config['SQLALCHEMY_TABLES'] = utils.get_tables() utils.activate() refresh_log_db.init_app(app) refresh_log_db.create_all(app=app) return app
def make_app(): app = sandman2.get_app(config.SQLA_URI, Base=utils.AutomapModel) app.json_encoder = utils.APIJSONEncoder app.config['CASE_INSENSITIVE'] = config.CASE_INSENSITIVE app.config['BASIC_AUTH_USERNAME'] = os.environ.get( 'AUTOAPI_ADMIN_USERNAME', '') app.config['BASIC_AUTH_PASSWORD'] = os.environ.get( 'AUTOAPI_ADMIN_PASSWORD', '') CORS(app) basic_auth = BasicAuth(app) @app.before_request def refresh(): tables = utils.get_tables() if tables != app.config['SQLALCHEMY_TABLES']: utils.refresh_tables() app.config['SQLALCHEMY_TABLES'] = tables @app.before_request def protect_admin(): if request.path.startswith('/admin/'): if not basic_auth.authenticate(): return basic_auth.challenge() class RefreshTables(View): task_name = 'refresh' def dispatch_request(self): underway = RefreshLog.refresh_underway() if underway: return '''Refresh begun at {} still underway. Now: {}; timeout set for {} seconds'''.format( underway.begun_at, datetime.now(), config.REFRESH_TIMEOUT_SECONDS) try: subprocess.Popen([ 'invoke', self.task_name, ]) except Exception as e: print('Problem with table refresh:') print(e) return 'Table refresh requested.' class QuickRefreshTables(RefreshTables): task_name = 'quick_refresh' app.add_url_rule('/refresh/', view_func=RefreshTables.as_view('refresh')) app.add_url_rule('/quick_refresh/', view_func=QuickRefreshTables.as_view('quick_refresh')) aws_blueprint = aws.make_blueprint() app.register_blueprint(aws_blueprint) docs_blueprint = swagger.make_blueprint(app) app.register_blueprint(docs_blueprint) with app.app_context(): app.config['SQLALCHEMY_TABLES'] = utils.get_tables() utils.activate() refresh_log_db.init_app(app) refresh_log_db.create_all(app=app) return app
from sandman2 import get_app app = get_app('sqlite+pysqlite:///tests/data/db.sqlite3') def main(): app.run(debug=True) if __name__ == '__main__': main()
from user_models import User, Blog, Post from sandman2 import get_app app = get_app('sqlite+pysqlite:///blog.sqlite3', user_models=[User, Blog, Post]) if __name__ == '__main__': app.run(debug=True)
from models import t2es from sandman2 import get_app app = get_app("mssql+pyodbc://sa:[email protected]/astage?driver=SQL Server", user_models=[t2es]) if __name__ == '__main__': app.run(debug=True)
import sandman2 from sqlalchemy.engine.url import URL from utvsapi import models url = URL('mysql', query={'read_default_file': './mysql.cnf'}) app = sandman2.get_app(url, user_models=models.all(), read_only=True) if __name__ == '__main__': app.run(debug=True)
from sandman2 import get_app app = get_app('mysql://*****:*****@localhost/classicmodels') if __name__ == '__main__': app.run(debug=True)