def init_app(self, app): """Flask application initialization.""" self.init_config(app) app.extensions['invenio-communities'] = self # TODO: Remove when merged in invenio-records-rest app.url_map.converters['lazy_pid'] = LazyPIDConverter # TODO: Make configurable or move into separate extension in ".records" from invenio_communities.records.indexer import record_indexer_receiver before_record_index.dynamic_connect( record_indexer_receiver, sender=app, weak=False, index=app.config.get('COMMUNITIES_RECORD_INDEX', 'records-record-v1.0.0'), ) @app.context_processor def record_context_processors(): """Jinja context processors for communities record integration.""" def record_communities(record): from invenio_communities.records.api import RecordCommunitiesCollection, Record return RecordCommunitiesCollection( Record(record, model=record.model)) return dict(record_communities=record_communities)
def init_loan_indexer_hook(self, app): """Custom loan indexer hook init.""" before_record_index.dynamic_connect( before_loan_index_hook, sender=app, weak=False, index="{0}s-{0}-v1.0.0".format("loan"), )
def _register_signals(self, app): """Register signals.""" before_record_index.dynamic_connect(indexer.indexer_receiver, sender=app, index="records-record-v1.0.0") file_deleted.connect(update_record_files_async, weak=False) file_uploaded.connect(update_record_files_async, weak=False)
def _register_signals(self, app): """Register signals.""" before_record_index.dynamic_connect(before_record_index_hook, sender=app, weak=False, index='records-record-v1.0.0') file_deleted.connect(update_record_files_async, weak=False) file_uploaded.connect(update_record_files_async, weak=False)
def init_metadata_extensions(self, app): """Metadata extensions initialization.""" for rec_type in app.config["ILS_RECORDS_METADATA_EXTENSIONS"].keys(): namespaces = app.config["ILS_RECORDS_METADATA_NAMESPACES"].get( rec_type, {}) extensions = app.config["ILS_RECORDS_METADATA_EXTENSIONS"].get( rec_type, {}) setattr( app.extensions["invenio-app-ils"], "{}_metadata_extensions".format(rec_type), MetadataExtensions(namespaces, extensions), ) before_record_index.dynamic_connect( before_record_index_hook, sender=app, weak=False, index="{0}s-{0}-v1.0.0".format(rec_type), )
def test_before_record_index_dynamic_connect(app): """Test before_record_index.dynamic_connect.""" with app.app_context(): with patch("invenio_records.api._records_state.validate"): auth_record = Record.create( { "$schema": "/records/authorities/authority-v1.0.0.json", "title": "Test", } ) bib_record = Record.create( { "$schema": "/records/bibliographic/bibliographic-v1.0.0.json", "title": "Test", } ) db.session.commit() def _simple(sender, json=None, **kwargs): json["simple"] = "simple" def _custom(sender, json=None, **kwargs): json["custom"] = "custom" def _cond(sender, connect_kwargs, index=None, **kwargs): return "bibliographic" in index _receiver1 = before_record_index.dynamic_connect( _simple, index="records-authorities-authority-v1.0.0" ) _receiver2 = before_record_index.dynamic_connect(_custom, condition_func=_cond) action = RecordIndexer()._index_action(dict(id=str(auth_record.id), op="index")) assert "title" in action["_source"] assert action["_source"]["simple"] == "simple" action = RecordIndexer()._index_action( dict(id=str(bib_record.id), index="foo", op="index") ) assert "title" in action["_source"] assert action["_source"]["custom"] == "custom" before_record_index.disconnect(_receiver1) before_record_index.disconnect(_receiver2)
def test_before_record_index_dynamic_connect(app): """Test before_record_index.dynamic_connect.""" with app.app_context(): with patch('invenio_records.api.Record.validate'): auth_record = Record.create({ '$schema': '/records/authorities/authority-v1.0.0.json', 'title': 'Test' }) bib_record = Record.create({ '$schema': '/records/bibliographic/bibliographic-v1.0.0.json', 'title': 'Test' }) db.session.commit() def _simple(sender, json=None, **kwargs): json['simple'] = 'simple' def _custom(sender, json=None, **kwargs): json['custom'] = 'custom' def _cond(sender, connect_kwargs, index=None, **kwargs): return 'bibliographic' in index _receiver1 = before_record_index.dynamic_connect( _simple, index='records-authorities-authority-v1.0.0') _receiver2 = before_record_index.dynamic_connect(_custom, condition_func=_cond) action = RecordIndexer()._index_action( dict(id=str(auth_record.id), op='index')) assert 'title' in action['_source'] assert action['_source']['simple'] == 'simple' action = RecordIndexer()._index_action( dict(id=str(bib_record.id), index='foo', op='index')) assert 'title' in action['_source'] assert action['_source']['custom'] == 'custom' before_record_index.disconnect(_receiver1) before_record_index.disconnect(_receiver2)