Пример #1
0
 def init_app(self, app, entry_point_group='invenio_celery.tasks',
              **kwargs):
     """Initialize application object."""
     self.init_config(app)
     self.celery = FlaskCeleryExt(app).celery
     self.entry_point_group = entry_point_group
     app.extensions['invenio-celery'] = self
Пример #2
0
 def init_app(self, app, assets=None,
              entry_point_group='invenio_celery.tasks', **kwargs):
     """Initialize application object."""
     self.init_config(app)
     self.celery = FlaskCeleryExt(app).celery
     self.entry_point_group = entry_point_group
     app.extensions['invenio-celery'] = self
Пример #3
0
def app(request):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app = Flask(__name__, instance_path=instance_path)
    app.config.update(
        CELERY_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND="memory",
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_RESULT_BACKEND="cache",
        SECRET_KEY="CHANGE_ME",
        SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO",
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        TESTING=True,
    )
    FlaskCeleryExt(app)
    InvenioDB(app)
    InvenioWorkflows(app)
    InvenioWorkflowsUI(app)
    InvenioOAIHarvester(app)
    INSPIRECrawler(app)

    with app.app_context():
        yield app

    shutil.rmtree(instance_path)
Пример #4
0
def app(request):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)
    app.config.update(
        BROKER_URL=os.environ.get('BROKER_URL', 'memory://'),
        CELERY_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND="memory",
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_RESULT_BACKEND="cache",
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        SQLALCHEMY_TRACK_MODIFICATIONS=False,
        TESTING=True,
    )
    FlaskCLI(app)
    FlaskCeleryExt(app)
    InvenioDB(app)
    InvenioRecords(app)
    InvenioSearch(app)
    InvenioIndexer(app)

    with app.app_context():
        if not database_exists(str(db.engine.url)):
            create_database(str(db.engine.url))
        db.create_all()

    def teardown():
        with app.app_context():
            drop_database(str(db.engine.url))
        shutil.rmtree(instance_path)

    request.addfinalizer(teardown)
    return app
Пример #5
0
def app(request):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app = Flask(__name__, instance_path=instance_path)
    app.config.update(
        CELERY_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND="memory",
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_RESULT_BACKEND="cache",
        SECRET_KEY="CHANGE_ME",
        SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO",
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        TESTING=True,
    )
    FlaskCLI(app)
    FlaskCeleryExt(app)
    InvenioDB(app)
    InvenioRecords(app)

    with app.app_context():
        db.create_all()

    def teardown():
        with app.app_context():
            db.drop_all()
        shutil.rmtree(instance_path)

    request.addfinalizer(teardown)

    return app
Пример #6
0
def app(request):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app_ = Flask(__name__, instance_path=instance_path)
    app_.config.update(
        CELERY_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND="memory",
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_RESULT_BACKEND="cache",
        SECRET_KEY="CHANGE_ME",
        SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO",
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        TESTING=True,
    )
    FlaskCeleryExt(app_)
    InvenioDB(app_)
    InvenioRecords(app_)
    InvenioPIDStore(app_)

    with app_.app_context():
        yield app_

    shutil.rmtree(instance_path)
Пример #7
0
def base_app():
    """Flask application fixture."""
    app_ = Flask('testapp')
    app_.config.update(
        TESTING=True,
        # Celery 3
        CELERY_ALWAYS_EAGER=True,
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        # Celery 4
        CELERY_TASK_ALWAYS_EAGER=True,
        CELERY_TASK_EAGER_PROPAGATES=True,
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///:memory:'),
        WTF_CSRF_ENABLED=False,
        SERVER_NAME='invenio.org',
        SECURITY_PASSWORD_SALT='TEST_SECURITY_PASSWORD_SALT',
        SECRET_KEY='TEST_SECRET_KEY',
        FILES_REST_MULTIPART_CHUNKSIZE_MIN=2,
        FILES_REST_MULTIPART_CHUNKSIZE_MAX=20,
        FILES_REST_MULTIPART_MAX_PARTS=100,
        FILES_REST_TASK_WAIT_INTERVAL=0.1,
        FILES_REST_TASK_WAIT_MAX_SECONDS=1,
    )

    FlaskCeleryExt(app_)
    InvenioDB(app_)
    Babel(app_)
    Menu(app_)

    return app_
Пример #8
0
def app(request):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)
    app.config.update(
        # HTTPretty doesn't play well with Redis.
        # See gabrielfalcao/HTTPretty#110
        CACHE_TYPE='simple',
        CELERY_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_RESULT_BACKEND='cache',
        GITHUB_SHIELDSIO_BASE_URL='http://example.org/badge/',
        GITHUB_APP_CREDENTIALS=dict(
            consumer_key='changeme',
            consumer_secret='changeme',
        ),
        LOGIN_DISABLED=False,
        OAUTHLIB_INSECURE_TRANSPORT=True,
        OAUTH2_CACHE_TYPE='simple',
        OAUTHCLIENT_REMOTE_APPS=dict(
            github=REMOTE_APP,
        ),
        SECRET_KEY='test_key',
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI',
                                          'sqlite:///test.db'),
        SECURITY_PASSWORD_HASH='plaintext',
        SECURITY_PASSWORD_SCHEMES=['plaintext'],
        TESTING=True,
        WTF_CSRF_ENABLED=False,
    )
    app.url_map.converters['pid'] = PIDConverter

    FlaskCLI(app)
    celeryext = FlaskCeleryExt(app)
    celeryext.celery.flask_app = app  # Make sure both apps are the same!
    Babel(app)
    Mail(app)
    Menu(app)
    Breadcrumbs(app)
    InvenioDB(app)
    InvenioAccounts(app)
    app.register_blueprint(accounts_blueprint)
    InvenioOAuthClient(app)
    app.register_blueprint(oauthclient_blueprint)
    InvenioOAuth2Server(app)
    app.register_blueprint(server_blueprint)
    app.register_blueprint(settings_blueprint)
    InvenioPIDStore(app)
    InvenioRecordsREST(app)
    InvenioDepositREST(app)
    InvenioWebhooks(app)
    app.register_blueprint(webhooks_blueprint)
    InvenioGitHub(app)

    with app.app_context():
        yield app

    shutil.rmtree(instance_path)
Пример #9
0
def app(request):
    """Flask application fixture."""
    app = Flask('testapp')
    app.config.update(
        TESTING=True,
        SQLALCHEMY_DATABASE_URI=os.environ.get(
            'SQLALCHEMY_DATABASE_URI', 'sqlite://'
        ),
        CELERY_ALWAYS_EAGER=True,
        CELERY_RESULT_BACKEND='cache',
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        RECORDS_UI_DEFAULT_PERMISSION_FACTORY=None,  # No permission checking
    )
    FlaskCeleryExt(app)
    Babel(app)
    InvenioDB(app)
    InvenioPIDStore(app)
    InvenioRecords(app)
    InvenioFilesREST(app)

    with app.app_context():
        db.create_all()

    def finalize():
        with app.app_context():
            db.drop_all()

    request.addfinalizer(finalize)
    return app
Пример #10
0
def app(request):
    """Flask application fixture."""
    app_ = Flask('testapp')
    app_.config.update(
        CELERY_ALWAYS_EAGER=True,
        CELERY_RESULT_BACKEND="cache",
        CELERY_CACHE_BACKEND="memory",
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        TESTING=True,
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SQLALCHEMY_DATABASE_URI=os.environ.get(
            'SQLALCHEMY_DATABASE_URI',
            'sqlite:///:memory:'),
        SECURITY_PASSWORD_SALT='TEST',
        SECRET_KEY='TEST',
    )
    FlaskCeleryExt(app_)
    InvenioDB(app_)
    InvenioRecords(app_)
    InvenioDeposit(app_)
    InvenioJSONSchemas(app_)
    InvenioAccounts(app_)
    InvenioCommunities(app_)
    InvenioPIDStore(app_)
    InvenioSIPStore(app_)
    Babel(app_)
    InvenioFilesREST(app_)
    InvenioMigrator(app_)
    FlaskOAuth(app_)
    InvenioOAuthClient(app_)

    with app_.app_context():
        yield app_
Пример #11
0
def app(env_config, default_config):
    """Flask application fixture."""
    app = create_api(**default_config)
    FlaskCeleryExt(app)

    with app.app_context():
        yield app
Пример #12
0
def app():
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)
    app.config.update(
        CELERY_ALWAYS_EAGER=True,
        CELERY_TASK_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_TASK_EAGER_PROPAGATES=True,
        CELERY_RESULT_BACKEND='cache',
        JSONSCHEMAS_HOST='inveniosoftware.org',
        TESTING=True,
        SECRET_KEY='CHANGE_ME',
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SERVER_NAME='app',
        OAISERVER_ID_PREFIX='oai:inveniosoftware.org:recid/',
        OAISERVER_QUERY_PARSER_FIELDS=["title_statement"],
        OAISERVER_RECORD_INDEX='_all',
        OAISERVER_REGISTER_SET_SIGNALS=True,
    )
    if not hasattr(app, 'cli'):
        from flask_cli import FlaskCLI
        FlaskCLI(app)
    InvenioDB(app)
    FlaskCeleryExt(app)
    InvenioJSONSchemas(app)
    InvenioRecords(app)
    InvenioPIDStore(app)
    InvenioMARC21(app)
    client = Elasticsearch(hosts=[os.environ.get('ES_HOST', 'localhost')])
    search = InvenioSearch(app, client=client)
    search.register_mappings('records', 'data')
    InvenioIndexer(app)
    InvenioOAIServer(app)

    app.register_blueprint(blueprint)

    with app.app_context():
        if str(db.engine.url) != 'sqlite://' and \
           not database_exists(str(db.engine.url)):
            create_database(str(db.engine.url))
        db.create_all()
        list(search.delete(ignore=[404]))
        list(search.create())
        search.flush_and_refresh('_all')

    with app.app_context():
        yield app

    with app.app_context():
        db.session.close()
        if str(db.engine.url) != 'sqlite://':
            drop_database(str(db.engine.url))
        list(search.delete(ignore=[404]))
        search.client.indices.delete("*-percolators")
    shutil.rmtree(instance_path)
Пример #13
0
def base_app():
    """Flask application fixture without InvenioStats."""
    from invenio_stats.config import STATS_EVENTS
    instance_path = tempfile.mkdtemp()
    app_ = Flask('testapp', instance_path=instance_path)
    stats_events = {
        'file-download': deepcopy(STATS_EVENTS['file-download']),
        'record-view': {
            'signal': 'invenio_records_ui.signals.record_viewed',
            'event_builders': ['invenio_stats.contrib.event_builders'
                               '.record_view_event_builder']
        }
    }
    stats_events.update({'event_{}'.format(idx): {} for idx in range(5)})
    app_.config.update(dict(
        CELERY_ALWAYS_EAGER=True,
        CELERY_TASK_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_TASK_EAGER_PROPAGATES=True,
        CELERY_RESULT_BACKEND='cache',
        SQLALCHEMY_DATABASE_URI=os.environ.get(
            'SQLALCHEMY_DATABASE_URI', 'sqlite://'),
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        TESTING=True,
        OAUTH2SERVER_CLIENT_ID_SALT_LEN=64,
        OAUTH2SERVER_CLIENT_SECRET_SALT_LEN=60,
        OAUTH2SERVER_TOKEN_PERSONAL_SALT_LEN=60,
        STATS_MQ_EXCHANGE=Exchange(
            'test_events',
            type='direct',
            delivery_mode='transient',  # in-memory queue
            durable=True,
        ),
        SECRET_KEY='asecretkey',
        SERVER_NAME='localhost',
        STATS_QUERIES={'bucket-file-download-histogram': {},
                       'bucket-file-download-total': {},
                       'test-query': {},
                       'test-query2': {}},
        STATS_EVENTS=stats_events,
        STATS_AGGREGATIONS={'file-download-agg': {}}
    ))
    FlaskCeleryExt(app_)
    InvenioAccounts(app_)
    InvenioAccountsREST(app_)
    InvenioDB(app_)
    InvenioRecords(app_)
    InvenioFilesREST(app_)
    InvenioPIDStore(app_)
    InvenioCache(app_)
    InvenioQueues(app_)
    InvenioOAuth2Server(app_)
    InvenioOAuth2ServerREST(app_)
    InvenioSearch(app_, entry_point_group=None)
    with app_.app_context():
        yield app_
    shutil.rmtree(instance_path)
Пример #14
0
def app():
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app_ = Flask(__name__, instance_path=instance_path)
    app_.config.update(
        CELERY_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_RESULT_BACKEND='cache',
        JSONSCHEMAS_URL_SCHEME='http',
        SECRET_KEY='CHANGE_ME',
        SECURITY_PASSWORD_SALT='CHANGE_ME_ALSO',
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SQLALCHEMY_ECHO=False,
        TESTING=True,
        WTF_CSRF_ENABLED=False,
        DEPOSIT_SEARCH_API='/api/search',
        SECURITY_PASSWORD_HASH='plaintext',
        SECURITY_PASSWORD_SCHEMES=['plaintext'],
        SECURITY_DEPRECATED_PASSWORD_SCHEMES=[],
        OAUTHLIB_INSECURE_TRANSPORT=True,
        OAUTH2_CACHE_TYPE='simple',
    )
    app_.url_map.converters['pid'] = PIDConverter
    FlaskCLI(app_)
    Babel(app_)
    FlaskCeleryExt(app_)
    InvenioDB(app_)
    Breadcrumbs(app_)
    InvenioAccounts(app_)
    InvenioAccess(app_)
    app_.register_blueprint(accounts_blueprint)
    InvenioAssets(app_)
    InvenioJSONSchemas(app_)
    InvenioSearch(app_)
    InvenioRecords(app_)
    app_.url_map.converters['pid'] = PIDConverter
    InvenioRecordsREST(app_)
    InvenioPIDStore(app_)
    InvenioIndexer(app_)
    InvenioDeposit(app_)
    InvenioSearchUI(app_)
    InvenioRecordsUI(app_)
    InvenioFilesREST(app_)
    OAuth2Provider(app_)
    InvenioOAuth2Server(app_)
    InvenioOAuth2ServerREST(app_)
    app_.register_blueprint(oauth2server_settings_blueprint)
    InvenioDepositREST(app_)

    with app_.app_context():
        yield app_

    shutil.rmtree(instance_path)
Пример #15
0
def task_app(request):
    """Flask application with Celery enabled."""
    app = _app_factory(dict(
        ACCOUNTS_USE_CELERY=True,
        MAIL_SUPPRESS_SEND=True,
    ))
    FlaskCeleryExt(app)
    InvenioAccounts(app)
    _database_setup(app, request)
    return app
Пример #16
0
    def init_app(self,
                 app,
                 assets=None,
                 entry_point_group='invenio_celery.tasks',
                 **kwargs):
        """Initialize application object."""
        self.init_config(app.config)
        self.celery = FlaskCeleryExt(app).celery

        if entry_point_group:
            task_packages = []
            for item in pkg_resources.iter_entry_points(
                    group=entry_point_group):
                task_packages.append(item.module_name)

            if task_packages:
                self.celery.autodiscover_tasks(task_packages,
                                               related_name='',
                                               force=True)

        app.extensions['invenio-celery'] = self
Пример #17
0
def test_ext_init():
    """Test of find_best_app."""
    app = Flask('exttest')
    ext = FlaskCeleryExt(app=app)
    assert ext.celery

    app = Flask('exttest')
    ext = FlaskCeleryExt()
    assert ext.celery is None
    ext.init_app(app)
    assert ext.celery

    def factory(flask_app):
        celery = Celery('myname')
        celery.flask_app = flask_app
        return celery

    app = Flask('exttest')
    ext = FlaskCeleryExt(app=app, create_celery_app=factory)
    ext.init_app(app)
    assert ext.celery
Пример #18
0
def app(request):
    """Flask application fixture."""
    app = Flask('testapp')
    app.config.update(
        ACCOUNTS_JWT_ENABLE=False,
        BROKER_TRANSPORT='redis',
        CELERY_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_RESULT_BACKEND='cache',
        CELERY_TRACK_STARTED=True,
        LOGIN_DISABLED=False,
        OAUTH2_CACHE_TYPE='simple',
        OAUTHLIB_INSECURE_TRANSPORT=True,
        SECRET_KEY='test_key',
        SECURITY_DEPRECATED_PASSWORD_SCHEMES=[],
        SECURITY_PASSWORD_HASH='plaintext',
        SECURITY_PASSWORD_SCHEMES=['plaintext'],
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI',
                                          'sqlite:///test.db'),
        TESTING=True,
        WTF_CSRF_ENABLED=False,
    )
    celeryext = FlaskCeleryExt(app)
    celeryext.celery.flask_app = app  # Make sure both apps are the same!
    Babel(app)
    Mail(app)
    Menu(app)
    Breadcrumbs(app)
    InvenioDB(app)
    InvenioAccounts(app)
    app.register_blueprint(accounts_blueprint)
    InvenioOAuth2Server(app)
    InvenioOAuth2ServerREST(app)
    app.register_blueprint(server_blueprint)
    app.register_blueprint(settings_blueprint)
    InvenioWebhooks(app)
    app.register_blueprint(blueprint)

    with app.app_context():
        if not database_exists(str(db.engine.url)):
            create_database(str(db.engine.url))
        db.create_all()

    def teardown():
        with app.app_context():
            drop_database(str(db.engine.url))

    request.addfinalizer(teardown)
    return app
Пример #19
0
def email_task_app(request):
    """Flask application fixture."""
    app = Flask('testapp')
    app.config.update(SQLALCHEMY_DATABASE_URI=os.environ.get(
        'SQLALCHEMY_DATABASE_URI', 'sqlite://'),
                      CELERY_ALWAYS_EAGER=True,
                      CELERY_RESULT_BACKEND="cache",
                      CELERY_CACHE_BACKEND="memory",
                      CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
                      MAIL_SUPPRESS_SEND=True)
    FlaskCeleryExt(app)

    InvenioMail(app, StringIO())

    return app
def app(od_licenses_json):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)
    app.config.update(
        JSONSCHEMAS_HOST='localhost',
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite://'),
        TESTING=True,
        RECORDS_REST_DEFAULT_READ_PERMISSION_FACTORY=None,
        CELERY_ALWAYS_EAGER=True,
        CELERY_RESULT_BACKEND="cache",
        CELERY_CACHE_BACKEND="memory",
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
    )

    app.url_map.converters['pid'] = PIDConverter

    FlaskCeleryExt(app)
    InvenioDB(app)
    InvenioJSONSchemas(app)
    InvenioRecords(app)
    InvenioIndexer(app)
    InvenioPIDStore(app)
    InvenioOpenDefinition(app)

    with app.app_context():
        if str(db.engine.url) != "sqlite://" and \
           not database_exists(str(db.engine.url)):
            create_database(str(db.engine.url))

        # MySQL has case-sensitivity issues with its default collation. To fix
        # this, we alter the created database's charset and collation.
        if str(db.engine.url).startswith('mysql'):
            conn = db.engine.connect()
            conn.execute('COMMIT')  # close the current transaction
            conn.execute('ALTER DATABASE invenio '
                         'CHARACTER SET utf8 COLLATE utf8_bin')
            conn.close()
        db.drop_all()
        db.create_all()

    def teardown():
        drop_database(str(db.engine.url))

    yield app

    shutil.rmtree(instance_path)
Пример #21
0
def oai_server(sender, app=None, **kwargs):
    #app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
    app.config['CELERY_TASK_ALWAYS_EAGER'] = True
    if not hasattr(app, 'cli'):
        from flask_cli import FlaskCLI
        ext_cli = FlaskCLI(app)
    ext_db = InvenioDB(app)
    ext_indexer = InvenioIndexer(app)
    ext_pidstore = InvenioPIDStore(app)
    ext_records = InvenioRecords(app)
    ext_search = InvenioSearch(app)
    ext_celery = FlaskCeleryExt(app)
    app.config['OAISERVER_RECORD_INDEX'] = ['authors', 'records']
    app.config['OAISERVER_ID_PREFIX'] = 'oai:example:'
    app.config['OAISERVER_QUERY_PARSER_FIELDS'] = ["title"]
    app.config['OAISERVER_METADATA_FORMATS'] = {
        'oai_dc': {
            'serializer': ('invenio_oaiserver.utils:dumps_etree', {
                'xslt_filename':
                pkg_resources.resource_filename(
                    'testinvenio.records', 'static/xsl/MARC21slim2OAIDC.xsl'),
                'xslt_filename':
                pathlib.Path(
                    "/home/alzbeta/testinvenio/testinvenio/records/static/xsl/MARC21slim2OAIDC.xsl"
                )
            }),
            'schema':
            'http://json-schema.org/draft-04/schema#',
            'namespace':
            'http://json-schema.org/draft-04/schema#',
        }
    }

    ext_oaiserver = InvenioOAIServer(app)
    app.register_blueprint(blueprint)
    ctx = app.app_context()
    ctx.push()
    db.create_all()
    ext_search.flush_and_refresh('_all')
    oaiset = OAISet(spec='pattern', name='Pattern', description='...')
    #nefunguje pokud search pattern ma pole ktere neni v obouch modelech...
    oaiset.search_pattern = 'title:Some title1'
    try:
        print("1")
        db.session.add(oaiset)
        db.session.commit()
    except:
        pass
Пример #22
0
def base_app(events_config, aggregations_config):
    """Flask application fixture without InvenioStats."""
    instance_path = tempfile.mkdtemp()
    app_ = Flask('testapp', instance_path=instance_path)
    app_.config.update(dict(
        CELERY_ALWAYS_EAGER=True,
        CELERY_TASK_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_TASK_EAGER_PROPAGATES=True,
        CELERY_RESULT_BACKEND='cache',
        SQLALCHEMY_DATABASE_URI=os.environ.get(
            'SQLALCHEMY_DATABASE_URI', 'sqlite://'),
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        # Bump the ES client timeout for slower environments (like Travis CI)
        SEARCH_CLIENT_CONFIG={'timeout': 30, 'max_retries': 5},
        TESTING=True,
        OAUTH2SERVER_CLIENT_ID_SALT_LEN=64,
        OAUTH2SERVER_CLIENT_SECRET_SALT_LEN=60,
        OAUTH2SERVER_TOKEN_PERSONAL_SALT_LEN=60,
        STATS_MQ_EXCHANGE=Exchange(
            'test_events',
            type='direct',
            delivery_mode='transient',  # in-memory queue
            durable=True,
        ),
        SECRET_KEY='asecretkey',
        SERVER_NAME='localhost',
        STATS_QUERIES={},
        STATS_EVENTS=events_config,
        STATS_AGGREGATIONS=aggregations_config,
    ))
    FlaskCeleryExt(app_)
    InvenioAccounts(app_)
    InvenioAccountsREST(app_)
    InvenioDB(app_)
    InvenioRecords(app_)
    InvenioFilesREST(app_)
    InvenioPIDStore(app_)
    InvenioCache(app_)
    InvenioQueues(app_)
    InvenioOAuth2Server(app_)
    InvenioOAuth2ServerREST(app_)
    InvenioSearch(app_, entry_point_group=None)
    with app_.app_context():
        yield app_
    shutil.rmtree(instance_path)
Пример #23
0
def email_app(request):
    """Email-aware Flask application fixture."""
    app = create_app(
        CFG_SITE_SUPPORT_EMAIL='*****@*****.**',
        INSPIRELABS_FEEDBACK_EMAIL='*****@*****.**',
        CELERY_ALWAYS_EAGER=True,
        CELERY_RESULT_BACKEND='cache',
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        MAIL_SUPPRESS_SEND=True,
    )
    FlaskCeleryExt(app)

    InvenioMail(app, StringIO())

    with app.app_context():
        yield app
Пример #24
0
    def init_app(self, app, assets=None,
                 entry_point_group='invenio_celery.tasks', **kwargs):
        """Initialize application object."""
        self.init_config(app.config)
        self.celery = FlaskCeleryExt(app).celery

        if entry_point_group:
            task_packages = []
            for item in pkg_resources.iter_entry_points(
                    group=entry_point_group):
                task_packages.append(item.module_name)

            if task_packages:
                self.celery.autodiscover_tasks(
                    task_packages, related_name='', force=True
                )

        app.extensions['invenio-celery'] = self
Пример #25
0
def app(request):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)
    app.config.update(
        TESTING=True,
        CELERY_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND="memory",
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_RESULT_BACKEND="cache",
        COMMUNITIES_MAIL_ENABLED=False,
        SECRET_KEY='CHANGE_ME',
        SECURITY_PASSWORD_SALT='CHANGE_ME_ALSO',
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        SEARCH_ELASTIC_HOSTS=os.environ.get('SEARCH_ELASTIC_HOSTS', None),
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        OAISERVER_REGISTER_RECORD_SIGNALS=True,
        OAISERVER_REGISTER_SET_SIGNALS=False,
        OAISERVER_ID_PREFIX='oai:localhost:recid/',
        SERVER_NAME='inveniosoftware.org',
        THEME_SITEURL='https://inveniosoftware.org',
        MAIL_SUPPRESS_SEND=True,
    )
    FlaskCeleryExt(app)
    Menu(app)
    Babel(app)
    InvenioDB(app)
    InvenioAccounts(app)
    InvenioAssets(app)
    InvenioSearch(app)
    InvenioRecords(app)
    InvenioIndexer(app)
    InvenioOAIServer(app)
    InvenioCommunities(app)
    InvenioMail(app)

    app.register_blueprint(ui_blueprint)
    app.register_blueprint(api_blueprint, url_prefix='/api/communities')

    with app.app_context():
        yield app

    shutil.rmtree(instance_path)
Пример #26
0
def task_app():
    """Flask application fixture."""
    app = Flask('testapp')
    app.config.update(ACCOUNTS_USE_CELERY=True,
                      SECRET_KEY="CHANGE_ME",
                      SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO",
                      SQLALCHEMY_DATABASE_URI=os.environ.get(
                          'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'),
                      CELERY_ALWAYS_EAGER=True,
                      CELERY_RESULT_BACKEND="cache",
                      CELERY_CACHE_BACKEND="memory",
                      CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
                      MAIL_SUPPRESS_SEND=True)
    FlaskCeleryExt(app)
    FlaskCLI(app)
    Mail(app)
    InvenioAccounts(app)

    return app
Пример #27
0
def base_app():
    """Flask applicat-ion fixture."""
    instance_path = os.path.join(sys.prefix, 'var', 'test-instance')

    # empty the instance path
    if os.path.exists(instance_path):
        shutil.rmtree(instance_path)
    os.makedirs(instance_path)

    os.environ['INVENIO_INSTANCE_PATH'] = instance_path

    app_ = Flask('oai-testapp', instance_path=instance_path)
    app_.config.update(
        TESTING=True,
        JSON_AS_ASCII=True,
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        #SQLALCHEMY_DATABASE_URI=os.environ.get(
        #    'SQLALCHEMY_DATABASE_URI',
        #    'sqlite:///:memory:'),
        SQLALCHEMY_DATABASE_URI='sqlite://',
        SERVER_NAME='localhost:5000',
        SECURITY_PASSWORD_SALT='TEST_SECURITY_PASSWORD_SALT',
        SECRET_KEY='TEST_SECRET_KEY',
        INVENIO_INSTANCE_PATH=instance_path,
        SEARCH_INDEX_PREFIX='test-',
        JSONSCHEMAS_HOST='localhost:5000',
        SEARCH_ELASTIC_HOSTS=os.environ.get('SEARCH_ELASTIC_HOSTS', None),
        PIDSTORE_RECID_FIELD='id',
        FILES_REST_PERMISSION_FACTORY=allow_all,
        CELERY_ALWAYS_EAGER=True,
        OAISERVER_RECORDS_INDEX='max21',
        OAISERVER_ID_PREFIX='oai:example:',
    )
    app_.register_blueprint(blueprint)
    InvenioDB(app_)
    InvenioIndexer(app_)
    InvenioSearch(app_)
    FlaskCeleryExt(app_)
    InvenioOAIServer(app_)
    if not hasattr(app_, 'cli'):
        ext_cli = FlaskCLI(app_)

    return app_
Пример #28
0
 def init_app(app_):
     app_.config.update(
         CELERY_ALWAYS_EAGER=False,
         CELERY_CACHE_BACKEND="memory",
         CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
         CELERY_RESULT_BACKEND="cache",
         JSONSCHEMAS_URL_SCHEME="http",
         SECRET_KEY="CHANGE_ME",
         SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO",
         SQLALCHEMY_DATABASE_URI=os.environ.get("SQLALCHEMY_DATABASE_URI",
                                                "sqlite:///test.db"),
         SQLALCHEMY_TRACK_MODIFICATIONS=True,
         SQLALCHEMY_ECHO=False,
         TESTING=True,
         WTF_CSRF_ENABLED=False,
         DEPOSIT_SEARCH_API="/api/search",
         SECURITY_PASSWORD_HASH="plaintext",
         SECURITY_PASSWORD_SCHEMES=["plaintext"],
         SECURITY_DEPRECATED_PASSWORD_SCHEMES=[],
         THEME_SITENAME="Test Site",
         OAUTHLIB_INSECURE_TRANSPORT=True,
         OAUTH2_CACHE_TYPE="simple",
         ACCOUNTS_JWT_ENABLE=False,
         # This allows access to files across all of invenio-files-rest
         FILES_REST_PERMISSION_FACTORY=lambda *a, **kw: type(
             "Allow", (object, ), {"can": lambda self: True})(),
         FILES_REST_MULTIPART_CHUNKSIZE_MIN=10,
     )
     Babel(app_)
     FlaskCeleryExt(app_)
     Breadcrumbs(app_)
     OAuth2Provider(app_)
     InvenioDB(app_)
     InvenioAccounts(app_)
     InvenioAccess(app_)
     InvenioIndexer(app_)
     InvenioJSONSchemas(app_)
     InvenioOAuth2Server(app_)
     InvenioPIDStore(app_)
     InvenioRecords(app_)
     search = InvenioSearch(app_)
     search.register_mappings("deposits", "invenio_deposit.mappings")
Пример #29
0
def app(env_config, zenodo_config, config, instance_path):
    """Flask application fixture."""
    app_ = Flask(__name__, instance_path=instance_path)
    app_.config.update(zenodo_config)
    app_.config.update(config)

    FlaskCeleryExt(app_)
    InvenioDB(app_)
    InvenioAccounts(app_)
    InvenioOAuthClient(app_)
    InvenioOAuth2Server(app_)
    InvenioRecords(app_)
    InvenioIndexer(app_)
    InvenioJSONSchemas(app_)
    InvenioSearch(app_)
    InvenioPIDStore(app_)
    ZenodoMigrator(app_)

    with app_.app_context():
        yield app_
Пример #30
0
 def init_app(app_):
     app_.config.update(
         CELERY_ALWAYS_EAGER=True,
         CELERY_CACHE_BACKEND='memory',
         CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
         CELERY_RESULT_BACKEND='cache',
         JSONSCHEMAS_URL_SCHEME='http',
         SECRET_KEY='CHANGE_ME',
         SECURITY_PASSWORD_SALT='CHANGE_ME_ALSO',
         SQLALCHEMY_DATABASE_URI=os.environ.get(
             'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'),
         SQLALCHEMY_TRACK_MODIFICATIONS=True,
         SQLALCHEMY_ECHO=False,
         TESTING=True,
         WTF_CSRF_ENABLED=False,
         DEPOSIT_SEARCH_API='/api/search',
         SECURITY_PASSWORD_HASH='plaintext',
         SECURITY_PASSWORD_SCHEMES=['plaintext'],
         SECURITY_DEPRECATED_PASSWORD_SCHEMES=[],
         OAUTHLIB_INSECURE_TRANSPORT=True,
         OAUTH2_CACHE_TYPE='simple',
         ACCOUNTS_JWT_ENABLE=False,
     )
     Babel(app_)
     FlaskCeleryExt(app_)
     Breadcrumbs(app_)
     OAuth2Provider(app_)
     InvenioDB(app_)
     InvenioAccounts(app_)
     InvenioAccess(app_)
     InvenioIndexer(app_)
     InvenioJSONSchemas(app_)
     InvenioOAuth2Server(app_)
     InvenioFilesREST(app_)
     InvenioPIDStore(app_)
     InvenioRecords(app_)
     search = InvenioSearch(app_)
     search.register_mappings('deposits', 'invenio_deposit.mappings')
Пример #31
0
def test_ext_init():
    """Test of find_best_app."""
    app = Flask('exttest')
    ext = FlaskCeleryExt(app=app)
    assert ext.celery

    app = Flask('exttest')
    ext = FlaskCeleryExt()
    assert ext.celery is None
    ext.init_app(app)
    assert ext.celery

    def factory(flask_app):
        celery = Celery('myname')
        celery.flask_app = flask_app
        return celery

    app = Flask('exttest')
    ext = FlaskCeleryExt(app=app, create_celery_app=factory)
    ext.init_app(app)
    assert ext.celery
Пример #32
0
def app(licenses_example):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)
    app.config.update(
        JSONSCHEMAS_HOST='localhost',
        SQLALCHEMY_DATABASE_URI=os.environ.get(
            'SQLALCHEMY_DATABASE_URI', 'sqlite://'),
        TESTING=True,
        RECORDS_REST_DEFAULT_READ_PERMISSION_FACTORY=None,
        CELERY_ALWAYS_EAGER=True,
        CELERY_RESULT_BACKEND="cache",
        CELERY_CACHE_BACKEND="memory",
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
    )

    app.url_map.converters['pid'] = PIDConverter

    FlaskCeleryExt(app)
    InvenioDB(app)
    InvenioJSONSchemas(app)
    InvenioRecords(app)
    InvenioPIDStore(app)
    InvenioOpenDefinition(app)

    with app.app_context():
        if str(db.engine.url) != "sqlite://" and \
           not database_exists(str(db.engine.url)):
            create_database(str(db.engine.url))
        db.drop_all()
        db.create_all()

    def teardown():
        drop_database(str(db.engine.url))

    yield app

    shutil.rmtree(instance_path)
Пример #33
0
def base_app():
    """Flask application fixture without InvenioStats."""
    from invenio_stats.config import STATS_EVENTS
    app_ = Flask('testapp')
    stats_events = {'file-download': deepcopy(STATS_EVENTS['file-download'])}
    stats_events.update({'event_{}'.format(idx): {} for idx in range(5)})
    app_.config.update(
        dict(
            CELERY_ALWAYS_EAGER=True,
            CELERY_TASK_ALWAYS_EAGER=True,
            CELERY_CACHE_BACKEND='memory',
            CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
            CELERY_TASK_EAGER_PROPAGATES=True,
            CELERY_RESULT_BACKEND='cache',
            SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                                   'sqlite://'),
            SQLALCHEMY_TRACK_MODIFICATIONS=True,
            TESTING=True,
            STATS_MQ_EXCHANGE=Exchange(
                'test_events',
                type='direct',
                delivery_mode='transient',  # in-memory queue
                durable=True,
            ),
            STATS_EVENTS=stats_events,
            STATS_AGGREGATIONS=['file-download-agg']))
    FlaskCeleryExt(app_)
    InvenioDB(app_)
    InvenioRecords(app_)
    InvenioRecordsUI(app_)
    InvenioPIDStore(app_)
    InvenioQueues(app_)
    InvenioFilesREST(app_)
    InvenioSearch(app_, entry_point_group=None)
    with app_.app_context():
        yield app_
Пример #34
0
class InvenioCelery(object):
    """Invenio celery extension."""

    def __init__(self, app=None, **kwargs):
        """Extension initialization."""
        self.celery = None

        if app:
            self.init_app(app, **kwargs)

    def init_app(self, app, assets=None,
                 entry_point_group='invenio_celery.tasks', **kwargs):
        """Initialize application object."""
        self.init_config(app)
        self.celery = FlaskCeleryExt(app).celery
        self.entry_point_group = entry_point_group
        app.extensions['invenio-celery'] = self

    def load_entry_points(self):
        """Load tasks from entry points."""
        if self.entry_point_group:
            task_packages = []
            for item in pkg_resources.iter_entry_points(
                    group=self.entry_point_group):
                task_packages.append(item.module_name)

            if task_packages:
                self.celery.autodiscover_tasks(
                    task_packages, related_name='', force=True
                )

    def init_config(self, app):
        """Initialize configuration."""
        for k in dir(config):
            if k.startswith('CELERY_') or k.startswith('BROKER_'):
                app.config.setdefault(k, getattr(config, k))

    def get_queues(self):
        """Return a list of current active Celery queues."""
        res = self.celery.control.inspect().active_queues() or dict()
        return [result.get('name') for host in res.values() for result in host]

    def disable_queue(self, name):
        """Disable given Celery queue."""
        self.celery.control.cancel_consumer(name)

    def enable_queue(self, name):
        """Enable given Celery queue."""
        self.celery.control.add_consumer(name)

    def get_active_tasks(self):
        """Return a list of UUIDs of active tasks."""
        current_tasks = self.celery.control.inspect().active() or dict()
        return [
            task.get('id') for host in current_tasks.values() for task in host]

    def suspend_queues(self, active_queues, sleep_time=10.0):
        """Suspend Celery queues and wait for running tasks to complete."""
        for queue in active_queues:
            self.disable_queue(queue)
        while self.get_active_tasks():
            time.sleep(sleep_time)
Пример #35
0
class InvenioCelery(object):
    """Invenio celery extension."""

    def __init__(self, app=None, **kwargs):
        """Extension initialization."""
        self.celery = None

        if app:
            self.init_app(app, **kwargs)

    def init_app(self, app, assets=None,
                 entry_point_group='invenio_celery.tasks', **kwargs):
        """Initialize application object."""
        self.init_config(app.config)
        self.celery = FlaskCeleryExt(app).celery

        if entry_point_group:
            task_packages = []
            for item in pkg_resources.iter_entry_points(
                    group=entry_point_group):
                task_packages.append(item.module_name)

            if task_packages:
                self.celery.autodiscover_tasks(
                    task_packages, related_name='', force=True
                )

        app.extensions['invenio-celery'] = self

    def init_config(self, config):
        """Initialize configuration."""
        config.setdefault('BROKER_URL', 'redis://localhost:6379/0')
        config.setdefault('CELERY_RESULT_BACKEND', 'redis://localhost:6379/1')
        config.setdefault('CELERY_ACCEPT_CONTENT', ['json', 'msgpack', 'yaml'])
        config.setdefault('CELERY_RESULT_SERIALIZER', 'msgpack')
        config.setdefault('CELERY_TASK_SERIALIZER', 'msgpack')

    def get_queues(self):
        """Return a list of current active Celery queues."""
        res = self.celery.control.inspect().active_queues() or dict()
        return [result.get('name') for host in res.values() for result in host]

    def disable_queue(self, name):
        """Disable given Celery queue."""
        self.celery.control.cancel_consumer(name)

    def enable_queue(self, name):
        """Enable given Celery queue."""
        self.celery.control.add_consumer(name)

    def get_active_tasks(self):
        """Return a list of UUIDs of active tasks."""
        current_tasks = self.celery.control.inspect().active() or dict()
        return [
            task.get('id') for host in current_tasks.values() for task in host]

    def suspend_queues(self, active_queues, sleep_time=10.0):
        """Suspend Celery queues and wait for running tasks to complete."""
        for queue in active_queues:
            self.disable_queue(queue)
        while self.get_active_tasks():
            time.sleep(sleep_time)