Esempio n. 1
0
def engine():
    sampledb_app = sampledb.create_app()
    db_url = sampledb_app.config['SQLALCHEMY_DATABASE_URI']
    engine = db.create_engine(
        db_url,
        echo=False,
        json_serializer=lambda obj: json.dumps(obj, cls=datatypes.JSONEncoder),
        json_deserializer=lambda obj: json.loads(obj, object_hook=datatypes.JSONEncoder.object_hook)
    )

    sampledb.utils.empty_database(engine, only_delete=False)
    return engine
Esempio n. 2
0
def create_app():
    logging.getLogger('flask.app').setLevel(logging.WARNING)
    os.environ['FLASK_ENV'] = 'development'
    os.environ['FLASK_TESTING'] = 'True'
    sampledb.utils.empty_database(
        sqlalchemy.create_engine(sampledb.config.SQLALCHEMY_DATABASE_URI))
    sampledb_app = sampledb.create_app()

    @sampledb_app.route('/users/me/loginstatus')
    def check_login():
        return flask.jsonify(flask_login.current_user.is_authenticated)

    @sampledb_app.route('/users/<int:user_id>/autologin')
    def autologin(user_id):
        user = sampledb.models.User.query.get(user_id)
        assert user is not None
        flask_login.login_user(user)
        return ''

    return sampledb_app
Esempio n. 3
0
def app_context():
    sampledb.utils.empty_database(
        sqlalchemy.create_engine(sampledb.config.SQLALCHEMY_DATABASE_URI))
    app = sampledb.create_app()
    with app.app_context():
        yield app
Esempio n. 4
0
import shutil
import tempfile
import os
import sqlalchemy
from sampledb import create_app
import sampledb.config
import sampledb.utils
from example_data import setup_data

sampledb.config.TEMPLATES_AUTO_RELOAD = True
sampledb.config.SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://{0}:@localhost:5432/{0}'.format(
    getpass.getuser())

temp_dir = tempfile.mkdtemp()
try:
    os.mkdir(os.path.join(temp_dir, 'uploaded_files'))
    sampledb.config.FILE_STORAGE_PATH = os.path.join(temp_dir,
                                                     'uploaded_files')

    # fully empty the database first
    sampledb.utils.empty_database(
        sqlalchemy.create_engine(sampledb.config.SQLALCHEMY_DATABASE_URI))
    app = create_app()
    app.config['SERVER_NAME'] = 'localhost:5000'
    with app.app_context():
        setup_data(app=app)

    app.run(debug=True)
finally:
    shutil.rmtree(temp_dir)