def db(app): def create_user(username, password, admin=False): return User(public_id=str(uuid.uuid4()), name=username, password_hash=generate_password_hash(password), admin=admin) def create_todo(text, user, complete=False): return Todo(text=text, complete=complete, user=user) if os.path.exists(app.config['DB_PATH']): os.unlink(app.config['DB_PATH']) with app.app_context(): _db.init_app(app) _db.create_all() admin_user = create_user("Admin", "password", admin=True) regular_user = create_user("User", "snowman") promotable_user = create_user("Promotable User", "greenman") _db.session.add(admin_user) _db.session.add(regular_user) _db.session.add(promotable_user) incomplete_todo = create_todo("Incomplete", regular_user) complete_todo = create_todo("Complete", regular_user, complete=True) _db.session.add(incomplete_todo) _db.session.add(complete_todo) _db.session.commit() yield _db _db.drop_all()
def main(): # process the command line first, then run the setup for the application args = parse_args() config_logging(args) if not os.path.isfile(args.config): logger.error( "The configuration file: {} does not exist. Unable to run.".format( args.config)) # config file validation request, so just run that and exit if args.validate: validate_config(load_config(args.config)) return True else: # run the startup routines, such as config loading, this should # return a configuration if the given arguments are valid configuration = load_config(args.config) # we have a config file, validate the config if not validate_config(configuration): return False # set defaults that are missing set_config_defaults(configuration) mode = "prod" if args.devel: mode = "dev" print(mode) # now create the flask app app = create_app(mode, configuration) from server.models import Servers from server.models import Datatable from server.models import Database from server.models import Aggregate from server.models import Attribute db.app = app db.init_app(app) db.create_all() db.session.commit() # setup services services.init_services(configuration) # finally run the app app.run(host="0.0.0.0", port=int(configuration["general"]["listen_on"]), debug=True, use_reloader=False)
def setUp(self): self.db_fd, self.db_filename = tempfile.mkstemp() app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////' + self.db_filename app.config['TESTING'] = True app.testing = True app.config['WTF_CSRF_ENABLED'] = False self.test_client = app.test_client() db.app = app db.init_app(app) with app.app_context(): db.create_all() seed.load_users() seed.load_houses()
def create_app(config_filename): """ Create an app given the filename of the configuration file """ app = Flask(__name__, static_folder="build", static_url_path="") app.config.from_object(config_filename) app.register_blueprint(api_bp, url_prefix='/api') db.init_app(app) app.app_context().push() db.create_all() CORS(app, resources={r"/api/*": {"origins": "*"}}) # this allows the api to be accessed by other users! return app
def create_app(test_config=None): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_relative_config=True) if test_config is None: app.config.from_mapping( SECRET_KEY="production", SQLALCHEMY_DATABASE_URI=config.MYSQL_CONFIG_PRODUCTION['DB_CONNECT_STRING'] ) else: app.config.from_mapping( SECRET_KEY="testing", SQLALCHEMY_DATABASE_URI=config.MYSQL_CONFIG_TESTING['DB_CONNECT_STRING'] ) app.config.update(test_config) @app.route("/hello") def hello(): return "Hello, World!" # register the database commands from server import db db.init_app(app) # apply the blueprints to the app # from server import auth, blog # app.register_blueprint(auth.bp) # app.register_blueprint(blog.bp) from server import v1 app.register_blueprint(v1.bp) # make url_for('index') == url_for('blog.index') # in another app, you might define a separate main index here with # app.route, while giving the blog blueprint a url_prefix, but for # the tutorial the blog will be the main index # app.add_url_rule("/", endpoint="index") return app
from server.models import Actor, Videos from server.models import Relationship_Actor_Videos, Relationship_Videos from server.queries import DBYouTube from flask import Flask import unittest from datetime import datetime from server import app, db app = Flask(__name__) app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" db.init_app(app) class TestFlask(unittest.TestCase): def setUp(self): app.app_context().push() db.create_all() collected_date_value = datetime.strptime('2018-06-14', '%Y-%m-%d').date() actor_db = { 'actor_name': 'Marina Silva', 'actor_username': '******', 'channel_id': 'channel_id_value', 'title': 'Marina Silva', 'subscribers': 13515, 'video_count': 876, 'view_count': 4307555, 'created_date': '2010-01-26', 'keywords': 'keywords_value', 'collected_date': collected_date_value,
from flask.ext.script import Manager from server import app, db import os manager = Manager(app) db.init_app(app) @manager.command def create_tables(): "Create the tables inside the database" db.create_all() @manager.command def drop_all(): "Drops all data (USE WITH CAUTION)" db.drop_all() @manager.command def populate(): "Populate the tables 'cities' and 'country' with standard data" os.system("sudo -u postgres psql segue < ./docs/country_city_data.sql") if __name__ == '__main__': manager.run()
import argparse from cryptography.fernet import Fernet from server import create_app, db from server.model import User, UserAddi app = create_app() app.app_context().push() db.init_app(app=create_app()) parser = argparse.ArgumentParser() parser.add_argument("--makedb", action="store_true") parser.add_argument("--makekey", action="store_true") args = parser.parse_args() if args.makedb: db.create_all() elif args.makekey: print("key: %s" % Fernet.generate_key()) else: print("Nothing Happened.")