def test_citation_count(self):
     from invenio_ext.es import es
     from invenio_search.api import Query
     recid = u'1196797'
     count = len(Query('refersto:'+recid).search())
     record = es.get(index='hep', id=recid)
     self.assertEqual(count, record['_source'].get('citation_count'))
Example #2
0
def ajax_references(recid, collection):
    """Handler for datatables references view"""

    from elasticsearch import TransportError
    from flask import jsonify
    from inspirehep.utils.references import render_references
    from invenio_ext.es import es
    from invenio_base.globals import cfg

    index = cfg['SEARCH_ELASTIC_COLLECTION_INDEX_MAPPING'].get(collection, 'hep')

    try:
        record = es.get(index=index, id=recid)['_source']
    except TransportError:
        current_app.logger.exception('Cannot render references for record no. {0}. Could not find record in ES.'
                                     .format(str(recid)))
        return jsonify(
            {
                "data": []
            }
        )

    return jsonify(
        {
            "data": render_references(record)
        }
    )
Example #3
0
 def test_name_variations_existance(self):
     """Test if name variations are saved in the record """
     from invenio_ext.es import es
     from inspirehep.modules.authors.utils import author_tokenize
     rec = es.get(index='hep', id=1343791)
     rec_list = rec['_source']['authors'][0]['name_variations']
     name = rec['_source']['authors'][0]['full_name']
     token_list = author_tokenize(name)
     self.assertEqual(token_list, rec_list)
 def test_facets_experiments(self):
     """Test if misspelled experiments are corrected."""
     from invenio_ext.es import es
     record = es.get(index='hep', id='480267')
     source = record.get("_source")
     exp = source.get("accelerator_experiments")
     # Gets misspelled experiment
     experiment = exp[0].get("experiment")
     # This should contain the corected experiment name
     facet_experiment = exp[0].get("facet_experiment")
     self.assertEqual(experiment, "CERN-LHC-LHCB")
     self.assertEqual(facet_experiment, "CERN-LHC-LHCb")
Example #5
0
def ajax_citations(recid, collection):
    """Handler for datatables citations view"""
    from flask import jsonify
    from inspirehep.utils.citations import Citation
    from invenio.base.globals import cfg
    from invenio_ext.es import es

    index = cfg['SEARCH_ELASTIC_COLLECTION_INDEX_MAPPING'].get(collection, 'hep')
    return jsonify(
        {
            "data": Citation(es.get(index=index, id=recid)['_source']).citations()
        }
    )
Example #6
0
 def _get_citation_number(self):
     """Returns how many times record was cited. If 0, returns nothing"""
     from invenio_ext.es import es
     import time
     today = time.strftime("%d %b %Y")
     record = es.get(index='hep', id=self.record['control_number'])
     citations = ''
     try:
         times_cited = record['_source']['citation_count']
         if times_cited != 0:
             if times_cited > 1:
                 citations = '%d citations counted in INSPIRE as of %s' \
                             % (times_cited, today)
             else:
                 citations = '%d citation counted in INSPIRE as of %s'\
                             % (times_cited, today)
     except KeyError:
         pass
     return citations
Example #7
0
    def decorated(recid, *args, **kwargs):
        from invenio_collections.models import Collection

        from .api import get_record
        from .access import check_user_can_view_record
        from invenio_records.tasks.index import get_record_index
        from invenio_records.api import Record
        from invenio.base.globals import cfg
        from invenio_ext.es import es
        from elasticsearch import TransportError

        # ensure recid to be integer
        recid = int(recid)

        # get record from db and the one from es
        db_record = get_record(recid)
        if db_record is None:
            abort(404)

        index = get_record_index(db_record) or \
                   cfg['SEARCH_ELASTIC_DEFAULT_INDEX']

        try:
            es_record = es.get(index=index, doc_type='record', id=recid)
        except TransportError:
            abort(404)

        g.record = record = Record(data=es_record['_source'])

        g.collection = collection = Collection.query.filter(
            Collection.name.in_(record['_collections'])).first()

        (auth_code, auth_msg) = check_user_can_view_record(
            current_user, record)

        # only superadmins can use verbose parameter for obtaining debug
        # information
        if not current_user.is_super_admin and 'verbose' in kwargs:
            kwargs['verbose'] = 0

        if auth_code:
            flash(auth_msg, 'error')
            abort(apache.HTTP_UNAUTHORIZED)

        if Query(cfg['RECORDS_DELETED_FIELD_QUERY']).match(record):
            # Record is deleted. Check for referred master recid if merged or 404
            if record.get(cfg['RECORDS_MERGED_MASTER_RECID_KEY']):
                return redirect(url_for('.' + f.func_name, recid=record.get(cfg['RECORDS_MERGED_MASTER_RECID_KEY'])))
            abort(404)

        title = record.get(cfg.get('RECORDS_BREADCRUMB_TITLE_KEY'), '')
        tabs = []

        def _format_record(record, of='hd', user_info=current_user, *args,
                           **kwargs):
            from invenio_formatter import format_record
            return format_record(record, of, user_info=user_info, *args,
                                 **kwargs)

        @register_template_context_processor
        def record_context():
            # from invenio.modules.comments.api import get_mini_reviews
            return dict(recid=recid,
                        record=record,
                        tabs=tabs,
                        title=title,
                        get_mini_reviews=lambda *args, **kwargs: '',
                        # FIXME get_mini_reviews,
                        collection=collection,
                        format_record=_format_record
                        )

        pre_template_render.send(
            "%s.%s" % (blueprint.name, f.__name__),
            recid=recid,
        )
        return f(recid, *args, **kwargs)
Example #8
0
    def decorated(recid, *args, **kwargs):
        from invenio_collections.models import Collection

        from .api import get_record
        from .access import check_user_can_view_record
        from invenio_records.tasks.index import get_record_index
        from invenio_records.api import Record
        from invenio.base.globals import cfg
        from invenio_ext.es import es
        from elasticsearch import TransportError

        # ensure recid to be integer
        recid = int(recid)

        # get record from db and the one from es
        db_record = get_record(recid)
        if db_record is None:
            abort(404)

        index = get_record_index(db_record) or \
                   cfg['SEARCH_ELASTIC_DEFAULT_INDEX']

        try:
            es_record = es.get(index=index, doc_type='record', id=recid)
        except TransportError:
            abort(404)

        g.record = record = Record(data=es_record['_source'])

        g.collection = collection = Collection.query.filter(
            Collection.name.in_(record['_collections'])).first()

        (auth_code,
         auth_msg) = check_user_can_view_record(current_user, record)

        # only superadmins can use verbose parameter for obtaining debug
        # information
        if not current_user.is_super_admin and 'verbose' in kwargs:
            kwargs['verbose'] = 0

        if auth_code:
            flash(auth_msg, 'error')
            abort(apache.HTTP_UNAUTHORIZED)

        # TODO check record status (exists, merged, deleted)

        title = record.get(cfg.get('RECORDS_BREADCRUMB_TITLE_KEY'), '')
        tabs = []

        def _format_record(record,
                           of='hd',
                           user_info=current_user,
                           *args,
                           **kwargs):
            from invenio_formatter import format_record
            return format_record(record,
                                 of,
                                 user_info=user_info,
                                 *args,
                                 **kwargs)

        @register_template_context_processor
        def record_context():
            # from invenio.modules.comments.api import get_mini_reviews
            return dict(
                recid=recid,
                record=record,
                tabs=tabs,
                title=title,
                get_mini_reviews=lambda *args, **kwargs: '',
                # FIXME get_mini_reviews,
                collection=collection,
                format_record=_format_record)

        pre_template_render.send(
            "%s.%s" % (blueprint.name, f.__name__),
            recid=recid,
        )
        return f(recid, *args, **kwargs)