def create_user(username, password, email, role='user', confirmed=False, test=False): """ Create a new user for OGRE test (bool) Only check if user has been created; don't actually do anything """ try: # load a user setup_db_session(app) from ogreserver.models.user import User user = User.query.filter_by(username=username).first() except ProgrammingError as e: if "doesn't exist" in str(e): print 'You must run init_ogre command first!' sys.exit(1) else: raise e if test is True: # only report state in test mode if user is None: print "User doesn't exist" sys.exit(1) else: print 'User {} exists'.format(username) sys.exit(0) else: if user is None: try: # celery is required for flask_security as it imports tasks.py app.celery = make_celery(app) register_tasks(app) from ogreserver.extensions.flask_security import init_security app.security = init_security(app) user = app.security.datastore.create_user( username=username, email=email, password=password ) if confirmed: from flask.ext.security.confirmable import confirm_user confirm_user(user) app.security.datastore.commit() print "Created user {} with role '{}'".format(username, role) except IntegrityError: print 'A user with this email address already exists' sys.exit(1) else: print 'User {} already exists'.format(username) sys.exit(1)
def _flask_app(request, app_config): if request.config.getoption("--only-client"): yield None return from ogreserver.factory import create_app, make_celery, configure_extensions, register_blueprints, register_signals from ogreserver.extensions.celery import register_tasks app = create_app(app_config) app.testing = True app.celery = make_celery(app) register_tasks(app) configure_extensions(app) register_blueprints(app) register_signals(app) yield app if os.path.exists(app_config["WHOOSH_BASE"]): shutil.rmtree(app_config["WHOOSH_BASE"])
def rebuild_index(foreground=False): """ Reindex the entire DB into Whoosh """ # setup celery for rebuilding meta in background app.celery = make_celery(app) register_tasks(app) register_signals(app) from ogreserver.tasks import index_for_search # connect to rethinkdb and run index task for all ebooks conn = r.connect('localhost', 28015, db='ogreserver') for ebook_data in r.table('ebooks').run(conn): if foreground: index_for_search(ebook_data=ebook_data) else: index_for_search.delay(ebook_data=ebook_data)
def rebuild_metadata(foreground=False): """ Reload all metadata from external APIs. Use with caution! """ # setup celery for rebuilding meta in background app.celery = make_celery(app) register_tasks(app) register_signals(app) from ogreserver.tasks import query_ebook_metadata # connect to rethinkdb and run metadata task for all ebooks conn = r.connect('localhost', 28015, db='ogreserver') for ebook_data in r.table('ebooks').run(conn): if foreground: query_ebook_metadata(ebook_data) else: query_ebook_metadata.delay(ebook_data)
def init_ogre(test=False): """ Initialize the AWS S3 bucket & RethinkDB storage for OGRE. test (bool) Only check if OGRE has been setup; don't actually do anything """ # init S3 s3 = connect_s3(app.config) # check bucket already exists aws_setup1 = aws_setup2 = False for b in s3.get_all_buckets(): if b.name == app.config['EBOOK_S3_BUCKET']: aws_setup1 = True elif b.name == app.config['STATIC_S3_BUCKET']: aws_setup2 = True aws_setup = aws_setup1 & aws_setup2 # check mysql DB created try: setup_db_session(app) from ogreserver.models.user import User User.query.first() db_setup = True except ProgrammingError: db_setup = False # check rethinkdb initialized conn = r.connect('localhost', 28015) try: r.db('ogreserver').table('ebooks').run(conn) rdb_setup = True except RqlRuntimeError: rdb_setup = False if test is True: # only report state in test mode if aws_setup is True and db_setup is True and rdb_setup is True: print 'Already setup' sys.exit(0) else: print 'Not setup' sys.exit(1) else: if aws_setup is True and db_setup is True and rdb_setup is True: print 'You have already initialized OGRE :D' sys.exit(1) # create the local mysql database from our models if db_setup is False: create_tables(app) # celery is required for setup_roles as it imports tasks.py via flask_security app.celery = make_celery(app) register_tasks(app) setup_roles(app) for bucket in (app.config['EBOOK_S3_BUCKET'], app.config['STATIC_S3_BUCKET']): try: if not app.config['DEBUG']: s3.create_bucket(bucket, location=app.config['AWS_REGION']) print 'Created S3 bucket in {}'.format(app.config['AWS_REGION']) else: s3.create_bucket(bucket) print 'Created S3 bucket' except boto.exception.S3ResponseError as e: sys.stderr.write('Failed verifying or creating S3 bucket.. ({})\n'.format(e.error_message)) sys.exit(1) except boto.exception.S3CreateError as e: if e.error_code == 'BucketAlreadyExists': sys.stderr.write('Bucket name already in use! ({})\n'.format(e.error_message)) sys.exit(1) elif e.error_code == 'BucketAlreadyOwnedByYou': pass else: raise e if rdb_setup is False: # create a database and a couple of tables r.db_create('ogreserver').run(conn) r.db('ogreserver').table_create('ebooks', primary_key='ebook_id').run(conn) r.db('ogreserver').table_create('versions', primary_key='version_id').run(conn) r.db('ogreserver').table_create('formats', primary_key='file_hash').run(conn) r.db('ogreserver').table_create('authors', primary_key='author_id').run(conn) r.db('ogreserver').table_create('sync_events').run(conn) set_indexes() print 'Succesfully initialized OGRE'