def test_get_records_list(test_custom_endpoints_app, indexed_records):
    """Test the creation of a custom endpoint using RecordsListResource."""
    blueprint = Blueprint(
        'test_invenio_records_rest',
        __name__,
    )
    json_v1 = JSONSerializer(RecordSchemaJSONV1)

    blueprint.add_url_rule(
        '/records/',
        view_func=RecordsListResource.as_view(
            'recid_list',
            minter_name='recid',
            pid_fetcher='recid',
            pid_type='recid',
            search_serializers={
                'application/json': search_responsify(
                    json_v1, 'application/json'
                )
            },
            search_class=RecordsSearch,
            read_permission_factory=allow_all,
            create_permission_factory=allow_all,
            search_factory=default_search_factory,
            default_media_type='application/json',
        )
    )
    test_custom_endpoints_app.register_blueprint(blueprint)

    with test_custom_endpoints_app.test_request_context():
        search_url = url_for('test_invenio_records_rest.recid_list')
    with test_custom_endpoints_app.test_client() as client:
        # Get a query with only one record
        res = client.get(search_url, query_string={'q': 'year:2015'})
        record = next(iter(
            [rec for rec in indexed_records if rec[1]['year'] == 2015]
        ))
        assert res.status_code == 200
        data = get_json(res)
        assert len(data['hits']['hits']) == 1
        # We need to check only for select record keys, since the ES
        # result contains manually-injected 'suggest' properties
        for k in ['title', 'year', 'stars', 'control_number']:
            assert record[1][k] == data['hits']['hits'][0]['metadata'][k]
Beispiel #2
0
def test_search_responsify(app):
    """Test JSON serialize."""
    search_serializer = search_responsify(
        TestSerializer(), 'application/x-custom')

    def fetcher():
        pass

    result = ['a'] * 5

    resp = search_serializer(fetcher, result)
    assert resp.status_code == 200
    assert resp.content_type == 'application/x-custom'
    assert resp.get_data(as_text=True) == "5"

    resp = search_serializer(
        fetcher, result, code=201, headers=[('X-Test', 'test')])
    assert resp.status_code == 201
    assert resp.headers['X-Test'] == 'test'
def test_search_responsify(app):
    """Test JSON serialize."""
    search_serializer = search_responsify(
        TestSerializer(), 'application/x-custom')

    def fetcher():
        pass

    result = ['a']*5

    resp = search_serializer(fetcher, result)
    assert resp.status_code == 200
    assert resp.content_type == 'application/x-custom'
    assert resp.get_data(as_text=True) == "5"

    resp = search_serializer(
        fetcher, result, code=201, headers=[('X-Test', 'test')])
    assert resp.status_code == 201
    assert resp.headers['X-Test'] == 'test'
Beispiel #4
0
def test_get_records_list(test_custom_endpoints_app, indexed_records):
    """Test the creation of a custom endpoint using RecordsListResource."""
    blueprint = Blueprint(
        'test_invenio_records_rest',
        __name__,
    )
    json_v1 = JSONSerializer(RecordSchemaJSONV1)

    blueprint.add_url_rule(
        '/records/',
        view_func=RecordsListResource.as_view(
            'recid_list',
            minter_name='recid',
            pid_fetcher='recid',
            pid_type='recid',
            search_serializers={
                'application/json': search_responsify(
                    json_v1, 'application/json'
                )
            },
            search_class=RecordsSearch,
            read_permission_factory=allow_all,
            create_permission_factory=allow_all,
            search_factory=default_search_factory,
            default_media_type='application/json',
        )
    )
    test_custom_endpoints_app.register_blueprint(blueprint)

    with test_custom_endpoints_app.test_request_context():
        search_url = url_for('test_invenio_records_rest.recid_list')
    with test_custom_endpoints_app.test_client() as client:
        # Get a query with only one record
        res = client.get(search_url, query_string={'q': 'year:2015'})
        record = next(iter(
            [rec for rec in indexed_records if rec[1]['year'] == 2015]
        ))
        assert res.status_code == 200
        data = get_json(res)
        assert len(data['hits']['hits']) == 1
        assert record[1] == data['hits']['hits'][0]['metadata']
Beispiel #5
0
from .impactgraph_serializer import ImpactGraphSerializer
from .json import JSONBriefSerializer
from .bibtex_serializer import BIBTEXSerializer
from .latexeu_serializer import LATEXEUSerializer
from .latexus_serializer import LATEXUSSerializer
from .cvformatlatex_serializer import CVFORMATLATEXSerializer
from .cvformathtml_serializer import CVFORMATHTMLSerializer
from .cvformattext_serializer import CVFORMATTEXTSerializer
from .orcid_serializer import ORCIDSerializer
from .schemas.json import RecordSchemaJSONBRIEFV1

from .response import record_responsify_nocache

json_brief_v1 = JSONBriefSerializer(RecordSchemaJSONBRIEFV1)
json_brief_v1_search = search_responsify(json_brief_v1, "application/vnd+inspire.brief+json")

bibtex_v1 = BIBTEXSerializer()
latexeu_v1 = LATEXEUSerializer()
latexus_v1 = LATEXUSSerializer()
cvformatlatex_v1 = CVFORMATLATEXSerializer()
cvformathtml_v1 = CVFORMATHTMLSerializer()
cvformattext_v1 = CVFORMATTEXTSerializer()
orcid = ORCIDSerializer()

bibtex_v1_response = record_responsify_nocache(bibtex_v1, "application/x-bibtex")
latexeu_v1_response = record_responsify_nocache(latexeu_v1, "application/x-latexeu")
latexus_v1_response = record_responsify_nocache(latexus_v1, "application/x-latexus")
cvformatlatex_v1_response = record_responsify_nocache(cvformatlatex_v1, "application/x-cvformatlatex")
cvformathtml_v1_response = record_responsify_nocache(cvformathtml_v1, "application/x-cvformathtml")
cvformattext_v1_response = record_responsify_nocache(cvformattext_v1, "application/x-cvformattext")
Beispiel #6
0
#
# invenio-app-ils is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""Loan serializers."""

from invenio_records_rest.serializers.response import record_responsify, \
    search_responsify

from invenio_app_ils.records.schemas.json import ILSRecordSchemaJSONV1

from .csv import LoanCSVSerializer
from .json import LoanJSONSerializer

csv_v1 = LoanCSVSerializer(ILSRecordSchemaJSONV1)
"""CSV serializer"""

csv_v1_response = record_responsify(csv_v1, "text/csv")
"""CSV response builder that uses the CSV serializer"""

csv_v1_search = search_responsify(csv_v1, "text/csv")
"""CSV search response builder that uses the CSV serializer"""

json_v1 = LoanJSONSerializer(ILSRecordSchemaJSONV1, replace_refs=True)
"""JSON serializer."""

json_v1_response = record_responsify(json_v1, "application/json")
"""JSON response builder decorates loan."""

json_v1_search = search_responsify(json_v1, "application/json")
"""JSON response builder decorates loan."""
Beispiel #7
0
            holding = Holding.get_record_by_pid(metadata.get('pid'))
            # available
            metadata['available'] = holding.available
            # Location name
            self._populate_data(metadata, Location, 'location')
            # Library name
            self._populate_data(metadata, Library, 'library')
            # Circulation category
            circ_category_pid = metadata['circulation_category']['pid']
            circ_category = ItemType.get_record_by_pid(circ_category_pid)
            metadata['circulation_category'] = circ_category.dumps()

        return super(HoldingsJSONSerializer, self)\
            .post_process_serialize_search(results, pid_fetcher)

    @classmethod
    def _populate_data(cls, metadata, resource, field):
        """Populate the current object with the name of resource."""
        field_data = metadata.get(field, {})
        field_pid = field_data.get('pid')
        if field_pid:
            data = resource.get_record_by_pid(field_pid)
            field_data['name'] = data.get('name')


json_holdings = HoldingsJSONSerializer(RecordSchemaJSONV1)
"""JSON v1 serializer."""

json_holdings_search = search_responsify(json_holdings,
                                         'application/rero+json')
Beispiel #8
0
            facet_config = current_app.config.get(
                'RERO_ILS_APP_CONFIG_FACETS', {}
            )
            if search_result['hits']['hits']:
                index_name = \
                    search_result['hits']['hits'][0]['_index'].split('-')[0]
                facet_config = facet_config.get(index_name, {})
                results['aggregations']['_settings'] = facet_config

        return json.dumps(results, **self._format_args())


json_v1 = ReroIlsSerializer(RecordSchemaJSONV1)
"""JSON v1 serializer."""

json_v1_search = search_responsify(json_v1, 'application/json')
json_v1_response = record_responsify(json_v1, 'application/json')


class ReroIlsCanDeleteSerializer(ReroIlsSerializer):
    """Mixin serializing records as JSON."""

    def serialize(self, pid, record, links_factory=None, **kwargs):
        """Serialize a single record and persistent identifier.

        :param pid: Persistent identifier instance.
        :param record: Record instance.
        :param links_factory: Factory function for record links.
        """
        if request and request.args.get('resolve'):
            record = record.replace_refs()
Beispiel #9
0
"""Collection serialization."""

from invenio_records_rest.serializers.response import search_responsify

from ..libraries.api import Library
from ..serializers import JSONSerializer, RecordSchemaJSONV1


class CollectionJSONSerializer(JSONSerializer):
    """Mixin serializing records as JSON."""
    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results."""
        lib_buckets = []
        # Add organisation name
        for lib_term in results.get('aggregations',
                                    {}).get('library', {}).get('buckets', []):
            pid = lib_term.get('key')
            lib = Library.get_record_by_pid(pid)
            name = lib.get('name')
            lib_term['name'] = name
            lib_buckets.append(lib_term)

        return super(CollectionJSONSerializer,
                     self).post_process_serialize_search(results, pid_fetcher)


json_coll = CollectionJSONSerializer(RecordSchemaJSONV1)
"""JSON v1 serializer."""

json_coll_search = search_responsify(json_coll, 'application/rero+json')
Beispiel #10
0
    record_responsify,
    search_responsify
)

from .impactgraph_serializer import ImpactGraphSerializer
from .json import JSONBriefSerializer
from .bibtex_serializer import BIBTEXSerializer
from .latexeu_serializer import LATEXEUSerializer
from .latexus_serializer import LATEXUSSerializer
from .cvformatlatex_serializer import CVFORMATLATEXSerializer
from .cvformathtml_serializer import CVFORMATHTMLSerializer
from .cvformattext_serializer import CVFORMATTEXTSerializer
from .schemas.json import RecordSchemaJSONBRIEFV1

json_brief_v1 = JSONBriefSerializer(RecordSchemaJSONBRIEFV1)
json_brief_v1_search = search_responsify(json_brief_v1,
                                         'application/vnd+inspire.brief+json')

bibtex_v1 = BIBTEXSerializer()
latexeu_v1 = LATEXEUSerializer()
latexus_v1 = LATEXUSSerializer()
cvformatlatex_v1 = CVFORMATLATEXSerializer()
cvformathtml_v1 = CVFORMATHTMLSerializer()
cvformattext_v1 = CVFORMATTEXTSerializer()

bibtex_v1_response = record_responsify(bibtex_v1, 'application/x-bibtex')
latexeu_v1_response = record_responsify(latexeu_v1, 'application/x-latexeu')
latexus_v1_response = record_responsify(latexus_v1, 'application/x-latexus')
cvformatlatex_v1_response = record_responsify(cvformatlatex_v1,
                                              'application/x-cvformatlatex')
cvformathtml_v1_response = record_responsify(cvformathtml_v1,
                                             'application/x-cvformathtml')
Beispiel #11
0
from ..libraries.api import Library
from ..locations.api import Location
from ..serializers import JSONSerializer, RecordSchemaJSONV1


class ILLRequestJSONSerializer(JSONSerializer):
    """Mixin serializing records as JSON."""
    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results."""
        aggrs = results.get('aggregations', {})
        # replace the library pid by the library name for faceting
        for lib_term in aggrs.get('library', {}).get('buckets', []):
            pid = lib_term.get('key')
            lib_term['name'] = Library.get_record_by_pid(pid).get('name')
        # Populate record
        records = results.get('hits', {}).get('hits', {})
        for record in records:
            metadata = record.get('metadata', {})
            location_pid = metadata.get('pickup_location', {}).get('pid')
            location = Location.get_record_by_pid(location_pid)
            pickup_name = location.get('pickup_name', location.get('name'))
            metadata['pickup_location']['name'] = pickup_name

        return super().post_process_serialize_search(results, pid_fetcher)


json_ill_request = ILLRequestJSONSerializer(RecordSchemaJSONV1)
json_ill_request_search = search_responsify(json_ill_request,
                                            'application/rero+json')
Beispiel #12
0
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 CERN.
#
# inspirehep is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

from invenio_records_rest.serializers.response import search_responsify

from inspirehep.records.marshmallow.base import wrap_schema_class_with_metadata
from inspirehep.records.marshmallow.jobs import JobsPublicSchema
from inspirehep.records.serializers.response import record_responsify
from inspirehep.serializers import JSONSerializer

jobs_json = JSONSerializer(wrap_schema_class_with_metadata(JobsPublicSchema),
                           index_name="records-jobs")

jobs_json_response = record_responsify(jobs_json, "application/json")

jobs_json_response_search = search_responsify(jobs_json, "application/json")
Also, there are different serializers defined for output of individual record
requests (json_v1_response) and search results (json_v1_search), as the
internal objects may not have indentical structures.
For more information on serializers please see
https://invenio-records-rest.readthedocs.io/en/latest/usage.html#serialization/
.
"""

from invenio_records_rest.serializers.json import JSONSerializer
from invenio_records_rest.serializers.response import record_responsify, \
    search_responsify

from ..marshmallow import RecordSchemaV1

# Serializers
# ===========
json_v1 = JSONSerializer(RecordSchemaV1, replace_refs=True)

# Records-REST serializers
# ========================
# JSON record serializer for individual records.
json_v1_response = record_responsify(json_v1, 'application/json')
# JSON record serializer for search results.
json_v1_search = search_responsify(json_v1, 'application/json')

__all__ = (
    'json_v1',
    'json_v1_response',
    'json_v1_search',
)
from .impactgraph_serializer import ImpactGraphSerializer
from .json import JSONBriefSerializer
from .bibtex_serializer import BIBTEXSerializer
from .latexeu_serializer import LATEXEUSerializer
from .latexus_serializer import LATEXUSSerializer
from .cvformatlatex_serializer import CVFORMATLATEXSerializer
from .cvformathtml_serializer import CVFORMATHTMLSerializer
from .cvformattext_serializer import CVFORMATTEXTSerializer
from .orcid_serializer import ORCIDSerializer
from .schemas.json import RecordSchemaJSONBRIEFV1

from .response import record_responsify_nocache

json_brief_v1 = JSONBriefSerializer(RecordSchemaJSONBRIEFV1)
json_brief_v1_search = search_responsify(json_brief_v1,
                                         'application/vnd+inspire.brief+json')

bibtex_v1 = BIBTEXSerializer()
latexeu_v1 = LATEXEUSerializer()
latexus_v1 = LATEXUSSerializer()
cvformatlatex_v1 = CVFORMATLATEXSerializer()
cvformathtml_v1 = CVFORMATHTMLSerializer()
cvformattext_v1 = CVFORMATTEXTSerializer()
orcid = ORCIDSerializer()

bibtex_v1_response = record_responsify_nocache(
    bibtex_v1, 'application/x-bibtex')
latexeu_v1_response = record_responsify_nocache(
    latexeu_v1, 'application/x-latexeu')
latexus_v1_response = record_responsify_nocache(
    latexus_v1, 'application/x-latexus')
Beispiel #15
0
from .marcxml import MARCXMLSerializer

xslt_dublincore_oai = resource_filename(
    'invenio_marc21', 'xslts/MARC21slim2OAIDC.xsl')
xslt_dublincore_rdf = resource_filename(
    'invenio_marc21', 'xslts/MARC21slim2RDFDC.xsl')
xslt_dublincore_SRW = resource_filename(
    'invenio_marc21', 'xslts/MARC21slim2SRWDC.xsl')
xslt_mods = resource_filename('invenio_marc21', 'xslts/MARC21slim2MODS3-6.xsl')

#: MARCXML serializer.
marcxml_v1 = MARCXMLSerializer(to_marc21)
#: MARCXML record serializer
marcxml_v1_response = record_responsify(marcxml_v1, 'application/marcxml+xml')
#: MARCXML search serializer
marcxml_v1_search = search_responsify(marcxml_v1, 'application/marcxml+xml')

#: DublinCore serializer.
dublincore_v1 = MARCXMLSerializer(to_marc21, xslt_filename=xslt_dublincore_oai)
#: DublinCore record serializer.
dublincore_v1_response = record_responsify(
    dublincore_v1, 'application/xml')
#: DublinCore search serializer.
dublincore_v1_search = search_responsify(
    dublincore_v1, 'application/xml')

#: MODS serializer.
mods_v1 = MARCXMLSerializer(to_marc21, xslt_filename=xslt_mods)
#: DublinCore record serializer.
mods_v1_response = record_responsify(mods_v1, 'application/mods+xml')
#: DublinCore search serializer.
Beispiel #16
0
                dict(hits=dict(hits=to_return, total=total)))
            return native_json.dumps(to_return, indent=2)

        graph = Graph().parse(data=native_json.dumps(to_return, indent=2),
                              format="json-ld")
        # out = ''
        if self.output_format == 'xml':
            out = '<!--  Search-Engine-Total-Number-Of-Results: %s  -->\n' \
                  % total
        if self.output_format == 'turtle':
            out = '# Search-Engine-Total-Number-Of-Results: %s\n' % total
        return out + graph.serialize(format=self.output_format).decode('utf-8')


marcxml_v1 = MARCXMLSerializer(book2marc, schema_class=None, replace_refs=True)
marcxml_v1_search = search_responsify(marcxml_v1, 'application/marcxml+xml')
marcxml_v1_response = record_responsify(marcxml_v1, 'application/marcxml+xml')

ld_json_v1 = LDSerializer(RecordSchemaJSONV1, output_format='json-ld')
ld_turtle_v1 = LDSerializer(RecordSchemaJSONV1, output_format='turtle')
ld_xml_v1 = LDSerializer(RecordSchemaJSONV1, output_format='xml')

ld_json_v1_response = record_responsify(ld_json_v1, 'application/ld+json')
ld_json_v1_search = search_responsify(ld_json_v1, 'application/ld+json')

ld_turtle_v1_response = record_responsify(ld_turtle_v1, 'text/tutle')
ld_turtle_v1_search = search_responsify(ld_turtle_v1, 'text/turtle')

ld_xml_v1_response = record_responsify(ld_xml_v1, 'application/rdf+xml')
ld_xml_v1_search = search_responsify(ld_xml_v1, 'application/rdf+xml')
Beispiel #17
0
from invenio_records_rest.serializers.response import search_responsify

from rero_ils.modules.serializers import JSONSerializer, RecordSchemaJSONV1

from ..libraries.api import Library


class PatronTransactionEventsJSONSerializer(JSONSerializer):
    """Mixin serializing records as JSON."""

    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results."""
        records = results.get('hits', {}).get('hits', {})
        for record in records:
            metadata = record.get('metadata', {})
            # Library
            library_pid = metadata.get('library', {}).get('pid')
            if library_pid:
                library = Library.get_record_by_pid(library_pid)
                metadata['library']['name'] = library.get('name')
        return super(PatronTransactionEventsJSONSerializer, self)\
            .post_process_serialize_search(results, pid_fetcher)


json_patron_transaction_events = \
    PatronTransactionEventsJSONSerializer(RecordSchemaJSONV1)
"""JSON v1 serializer."""

json_patron_transaction_events_search = search_responsify(
    json_patron_transaction_events, 'application/rero+json')
Beispiel #18
0
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Record serialization."""

from __future__ import absolute_import, print_function

from .orcid_serializer import ORCIDSerializer

from invenio_records_rest.serializers.response import record_responsify, \
                                                      search_responsify

orcid = ORCIDSerializer()

orcid_response = record_responsify(orcid, 'application/x-orcid')

orcid_search = search_responsify(orcid, 'application/x-orcid')
# along with CERN Analysis Preservation Framework; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Record serialization."""

from __future__ import absolute_import, print_function

from invenio_records_rest.serializers.response import record_responsify, \
    search_responsify

from .json import CAPSchemaSerializer as JSONSerializer
from .schemas.json import RecordSchemaJSONV1

# Serializers
# ===========
#: CAP JSON serializer version 1.0.0
json_v1 = JSONSerializer(RecordSchemaJSONV1)

# Records-REST serializers
# ========================
#: JSON record serializer for individual records.
json_v1_response = record_responsify(json_v1, 'application/json')

#: JSON record serializer for search results.
json_v1_search = search_responsify(json_v1, 'application/json')
from .json import CAPSchemaSerializer as JSONSerializer, \
    BasicJSONSerializer, PermissionsJSONSerializer
from .schemas.json import RecordSchemaJSONV1, BasicDepositSchema, \
    PermissionsDepositSchema
from invenio_deposit.serializers import json_file_response

# Serializers
# ===========
# CAP JSON serializer version 1.0.0
json_v1 = JSONSerializer(RecordSchemaJSONV1)
basic_json_v1 = BasicJSONSerializer(BasicDepositSchema)
permissions_json_v1 = PermissionsJSONSerializer(PermissionsDepositSchema)

# Records-REST serializers
# ========================
# JSON record serializer for individual records.
json_v1_response = record_responsify(json_v1, 'application/json')
basic_json_v1_response = record_responsify(
    basic_json_v1, 'application/basic+json')
permissions_json_v1_response = record_responsify(
    permissions_json_v1, 'application/permissions+json')

# Files-REST serializers
# JSON Files serializers for deposit files
deposit_v1_files_response = json_file_response

# JSON record serializer for search results.
json_v1_search = search_responsify(json_v1, 'application/json')
basic_json_v1_search = search_responsify(
    basic_json_v1, 'application/basic+json')
Beispiel #21
0
        #     xml_links = element.links()
        #     self_link = links.get('self')
        #     if self_link:
        #         xml_links.append(element.self(f'{self_link}&format=dc'))
        #     next_link = links.get('next')
        #     if next_link:
        #         xml_links.append(element.next(f'{next_link}&format=dc'))
        #     xml_root.append(xml_links)
        return etree.tostring(xml_root,
                              encoding='utf-8',
                              method='xml',
                              pretty_print=True)


json_doc = DocumentJSONSerializer(RecordSchemaJSONV1)
"""JSON v1 serializer."""
xml_dc = DublinCoreSerializer(RecordSchemaJSONV1)
"""XML DUBLIN CORE v1 serializer."""
xml_marcxml = DocumentMARCXMLSerializer()
"""XML MARCXML v1 serializer."""
xml_marcxmlsru = DocumentMARCXMLSRUSerializer()
"""XML MARCXML SRU v1 serializer."""

json_doc_search = search_responsify(json_doc, 'application/rero+json')
json_doc_response = record_responsify(json_doc, 'application/rero+json')
xml_dc_search = search_responsify(xml_dc, 'application/xml')
xml_dc_response = record_responsify(xml_dc, 'application/xml')
xml_marcxml_search = search_responsify(xml_marcxml, 'application/xml')
xml_marcxml_response = record_responsify(xml_marcxml, 'application/xml')
xml_marcxmlsru_search = search_responsify(xml_marcxmlsru, 'application/xml')
Beispiel #22
0
        :param pid: Persistent identifier instance.
        :param record: Record instance.
        :param links_factory: Factory function for record links.
        """
        return self.format_record(record)

    def serialize_search(self,
                         pid_fetcher,
                         search_result,
                         links=None,
                         item_links_factory=None):
        """Serialize a search result.

        :param pid_fetcher: Persistent identifier fetcher.
        :param search_result: Elasticsearch search result.
        :param links: Dictionary of links to add to response.
        """
        records = []
        for hit in search_result['hits']['hits']:
            records.append(self.format_record(record=hit['_source']))

        return "\n".join(records)


documents_items_csv_v1 = TextSerializer()
documents_items_csv_v1_response = record_responsify(documents_items_csv_v1,
                                                    'text/csv')
documents_items_csv_v1_search = search_responsify(documents_items_csv_v1,
                                                  'text/csv')
Beispiel #23
0
        data = self.transform_record(pid, record, links_factory, **kwargs)
        return self.cv_template().render(
            record=data, host=current_app.config["SERVER_NAME"]
        )

    def serialize(self, pid, record, links_factory=None, **kwargs):
        body = self.serialize_inner(pid, record, links_factory=links_factory, **kwargs)
        return self.wrapping_html.format(body=body)

    def preprocess_record(self, pid, record, links_factory=None, **kwargs):
        return record

    def serialize_search(
        self, pid_fetcher, search_result, links=None, item_links_factory=None
    ):
        records = (
            hit["_source"].get("_cv_format", "")
            for hit in search_result["hits"]["hits"]
        )
        body = "\n  ".join(records)
        return self.wrapping_html.format(body=body)


literature_cv_html = CVHTMLSerializer(schema_class=CVSchema)
literature_cv_html_response = record_responsify(
    literature_cv_html, "text/vnd+inspire.html+html"
)
literature_cv_html_response_search = search_responsify(
    literature_cv_html, "text/vnd+inspire.html+html"
)
Beispiel #24
0
    InstitutionsListSchema,
)
from inspirehep.records.serializers.response import record_responsify
from inspirehep.serializers import ConditionalMultiSchemaJSONSerializer, JSONSerializer

institutions_json = ConditionalMultiSchemaJSONSerializer([
    (
        lambda _: is_superuser_or_cataloger_logged_in(),
        wrap_schema_class_with_metadata(InstitutionsAdminSchema),
    ),
    (None, wrap_schema_class_with_metadata(InstitutionsPublicSchema)),
])

institutions_json_response = record_responsify(institutions_json,
                                               "application/json")

institutions_json_response_search = search_responsify(institutions_json,
                                                      "application/json")

institutions_json_detail = JSONSerializer(
    wrap_schema_class_with_metadata(InstitutionsDetailSchema))
institutions_json_detail_response = record_responsify(
    institutions_json_detail, "application/vnd+inspire.record.ui+json")

institutions_json_list = JSONSerializer(
    wrap_schema_class_with_metadata(InstitutionsListSchema),
    index_name="records-institutions",
)
institutions_json_list_response = search_responsify(
    institutions_json_list, "application/vnd+inspire.record.ui+json")
)
from pkg_resources import resource_filename

from .marcxml import MARCXMLSerializer

xslt_dublincore_oai = resource_filename("invenio_marc21", "xslts/MARC21slim2OAIDC.xsl")
xslt_dublincore_rdf = resource_filename("invenio_marc21", "xslts/MARC21slim2RDFDC.xsl")
xslt_dublincore_SRW = resource_filename("invenio_marc21", "xslts/MARC21slim2SRWDC.xsl")
xslt_mods = resource_filename("invenio_marc21", "xslts/MARC21slim2MODS3-6.xsl")

#: MARCXML serializer.
marcxml_v1 = MARCXMLSerializer(to_marc21)
#: MARCXML record serializer
marcxml_v1_response = record_responsify(marcxml_v1, "application/marcxml+xml")
#: MARCXML search serializer
marcxml_v1_search = search_responsify(marcxml_v1, "application/marcxml+xml")

#: DublinCore serializer.
dublincore_v1 = MARCXMLSerializer(to_marc21, xslt_filename=xslt_dublincore_oai)
#: DublinCore record serializer.
dublincore_v1_response = record_responsify(dublincore_v1, "application/xml")
#: DublinCore search serializer.
dublincore_v1_search = search_responsify(dublincore_v1, "application/xml")

#: MODS serializer.
mods_v1 = MARCXMLSerializer(to_marc21, xslt_filename=xslt_mods)
#: DublinCore record serializer.
mods_v1_response = record_responsify(mods_v1, "application/mods+xml")
#: DublinCore search serializer.
mods_v1_search = search_responsify(mods_v1, "application/mods+xml")
Beispiel #26
0
        return writer.to_string(bib_data)

    def serialize(self, pid, record, links_factory=None):
        try:
            return self.create_bibliography(record)
        except Exception as e:
            LOGGER.exception(
                "Bibtex serialization error",
                recid=record.get("control_number"),
                error=e,
            )
            return f"% Bibtex generation failed for record {record.get('control_number','')}"

    def serialize_search(self,
                         pid_fetcher,
                         search_result,
                         links=None,
                         item_links_factory=None):
        records = [
            hit["_source"].get("_bibtex_display", "")
            for hit in search_result["hits"]["hits"]
        ]
        return "\n".join(records)


literature_bibtex = BibTexSerializer(BibTexCommonSchema)
literature_bibtex_response = record_responsify(literature_bibtex,
                                               "application/x-bibtex")
literature_bibtex_response_search = search_responsify(literature_bibtex,
                                                      "application/x-bibtex")
Beispiel #27
0
    ExperimentsDetailSchema,
    ExperimentsListSchema,
)
from inspirehep.serializers import ConditionalMultiSchemaJSONSerializer, JSONSerializer

experiments_json = ConditionalMultiSchemaJSONSerializer([
    (
        lambda _: is_superuser_or_cataloger_logged_in(),
        wrap_schema_class_with_metadata(ExperimentsAdminSchema),
    ),
    (None, wrap_schema_class_with_metadata(ExperimentsPublicSchema)),
])

experiments_json_response = record_responsify(experiments_json,
                                              "application/json")

experiments_json_response_search = search_responsify(experiments_json,
                                                     "application/json")

experiments_json_detail = JSONSerializer(
    wrap_schema_class_with_metadata(ExperimentsDetailSchema))
experiments_json_detail_response = record_responsify(
    experiments_json_detail, "application/vnd+inspire.record.ui+json")

experiments_json_list = JSONSerializer(
    wrap_schema_class_with_metadata(ExperimentsListSchema),
    index_name="records-experiments",
)
experiments_json_list_response = search_responsify(
    experiments_json_list, "application/vnd+inspire.record.ui+json")
Beispiel #28
0
            for key in keys:
                value = es_hit.get(key)
                if value is not None:
                    record[key] = value
        except StopIteration:
            # Should not happens... the account should always be indexed
            pass

        return super().preprocess_record(pid=pid,
                                         record=record,
                                         links_factory=links_factory,
                                         kwargs=kwargs)

    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results."""
        # Add library name
        for lib_term in results.get('aggregations',
                                    {}).get('library', {}).get('buckets', []):
            pid = lib_term.get('key')
            lib_term['name'] = Library.get_record_by_pid(pid).get('name')
        return super().post_process_serialize_search(results, pid_fetcher)


json_acq_account = AcqAccountJSONSerializer(RecordSchemaJSONV1)
"""JSON v1 serializer."""

json_acq_account_search = search_responsify(json_acq_account,
                                            'application/rero+json')
json_acq_account_response = record_responsify(json_acq_account,
                                              'application/rero+json')
Beispiel #29
0
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Patrons serialization."""

from invenio_records_rest.serializers.response import search_responsify

from ..patron_types.api import PatronType
from ..serializers import JSONSerializer, RecordSchemaJSONV1


class PatronJSONSerializer(JSONSerializer):
    """Mixin serializing records as JSON."""
    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results."""
        # Add patron type name
        for type_term in results.get('aggregations',
                                     {}).get('patron_type',
                                             {}).get('buckets', []):
            pid = type_term.get('key')
            name = PatronType.get_record_by_pid(pid).get('name')
            type_term['key'] = pid
            type_term['name'] = name

        return super().post_process_serialize_search(results, pid_fetcher)


json_patron = PatronJSONSerializer(RecordSchemaJSONV1)
"""JSON v1 serializer."""

json_patron_search = search_responsify(json_patron, 'application/rero+json')
Beispiel #30
0
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 CERN.
#
# inspirehep is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

from invenio_records_rest.serializers.response import search_responsify

from inspirehep.records.marshmallow.base import wrap_schema_class_with_metadata
from inspirehep.records.marshmallow.experiments import ExperimentsPublicSchema
from inspirehep.records.serializers.response import record_responsify
from inspirehep.serializers import JSONSerializer

experiments_json = JSONSerializer(
    wrap_schema_class_with_metadata(ExperimentsPublicSchema),
    index_name="records-experiments",
)

experiments_json_response = record_responsify(experiments_json,
                                              "application/json")

experiments_json_response_search = search_responsify(experiments_json,
                                                     "application/json")
Beispiel #31
0
        Args:
            pid_fetcher: Persistent identifier fetcher.
            search_result: Elasticsearch search result.
            links: Dictionary of links to add to response.

        Returns:
            str: serialized search result(s)
        """
        records = [
            hit["_source"].get(f"_latex_{self.format.lower()}_display", "")
            for hit in search_result["hits"]["hits"]
        ]
        return "\n\n".join(records)


latex_EU = LatexSerializer("EU", schema_class=LatexSchema)
latex_US = LatexSerializer("US", schema_class=LatexSchema)
latex_response_eu = record_responsify(
    latex_EU, "application/vnd+inspire.latex.eu+x-latex"
)
latex_response_us = record_responsify(
    latex_US, "application/vnd+inspire.latex.us+x-latex"
)
literature_latex_eu_response_search = search_responsify(
    latex_EU, "application/vnd+inspire.latex.eu+x-latex"
)
literature_latex_us_response_search = search_responsify(
    latex_US, "application/vnd+inspire.latex.us+x-latex"
)
Beispiel #32
0
    search_responsify

from rero_ils.modules.items.serializers.response import search_responsify_csv
from rero_ils.modules.serializers import JSONSerializer, RecordSchemaJSONV1

from .csv import ItemCSVSerializer
from .json import ItemsJSONSerializer

csv_item = ItemCSVSerializer(
    JSONSerializer,
    csv_included_fields=[
        'pid', 'document_pid', 'document_title', 'document_creator',
        'document_main_type', 'document_sub_type', 'library_name',
        'location_name', 'barcode', 'call_number', 'second_call_number',
        'enumerationAndChronology', 'item_type', 'temporary_item_type',
        'temporary_item_type_end_date', 'general_note', 'staff_note',
        'checkin_note', 'checkout_note', 'loans_count', 'checkout_date',
        'due_date', 'last_transaction_date', 'status', 'created',
        'issue_status', 'issue_status_date', 'issue_claims_count',
        'issue_expected_date', 'issue_regular'
    ])
csv_item_response = record_responsify(csv_item, "text/csv")
csv_item_search = search_responsify_csv(csv_item, "text/csv")
"""CSV serializer."""

json_item = ItemsJSONSerializer(RecordSchemaJSONV1)
"""JSON serializer."""

json_item_search = search_responsify(json_item, 'application/rero+json')
json_item_response = record_responsify(json_item, 'application/rero+json')
Beispiel #33
0
            aggregations=search_result.get('aggregations', dict()),
        )
        with current_app.app_context():
            facet_config = current_app.config.get('RERO_ILS_APP_CONFIG_FACETS',
                                                  {})
            try:
                type = search_result['hits']['hits'][0]['_index'].split('-')[0]
                for aggregation in results.get('aggregations'):
                    facet_config_type = facet_config.get(type, {})
                    facet_config_type_expand = facet_config_type.get(
                        'expand', '')
                    results['aggregations'][aggregation]['expand'] = False
                    if aggregation in facet_config_type_expand:
                        results['aggregations'][aggregation]['expand'] = True
                for lib_term in results.get('aggregations',
                                            {}).get('library',
                                                    {}).get('buckets', []):
                    pid = lib_term.get('key')
                    name = Library.get_record_by_pid(pid).get('name')
                    lib_term['name'] = name
            except Exception:
                pass
        return json.dumps(results, **self._format_args())


json_doc = ReroIlsSerializer(RecordSchemaJSONV1)
"""JSON v1 serializer."""

json_doc_search = search_responsify(json_doc, 'application/rero+json')
# json_v1_response = record_responsify(json_v1, 'application/rero+json')
Beispiel #34
0
    UIDisplayLiteratureRecordJsonUIV1,
)
from .marcxml import MARCXMLSerializer
from .latex import LatexSerializer
from .response import record_responsify_nocache, facets_responsify

json_literature_ui_v1 = LiteratureJSONUISerializer(
    LiteratureRecordSchemaJSONUIV1
)

json_literature_ui_v1_search = LiteratureJSONUISerializer(
    UIDisplayLiteratureRecordJsonUIV1
)

json_literature_ui_v1_search_response = search_responsify(
    json_literature_ui_v1_search,
    'application/vnd+inspire.literature.ui+json'
)

json_literature_ui_v1_response = record_responsify_nocache(
    json_literature_ui_v1,
    'application/vnd+inspire.literature.ui+json'
)

json_literature_references_v1 = JSONSerializer(
    LiteratureReferencesSchemaJSONUIV1
)
json_literature_references_v1_search = search_responsify(
    json_literature_references_v1,
    'application/json',
)
json_literature_references_v1_response = record_responsify_nocache(
Beispiel #35
0
from .json import (BasicJSONSerializer, PermissionsJSONSerializer,
                   RecordSerializer)
from .schemas.json import (BasicDepositSchema, PermissionsDepositSchema,
                           RecordSchema, RecordFormSchema)

# Serializers
# ===========
# CAP JSON serializer version 1.0.0
record_json_v1 = RecordSerializer(RecordSchema)
record_form_json_v1 = RecordSerializer(RecordFormSchema)

basic_json_v1 = BasicJSONSerializer(BasicDepositSchema)
permissions_json_v1 = PermissionsJSONSerializer(PermissionsDepositSchema)

# Records-REST serializers
# ========================
# JSON record serializer for individual records.
record_json_v1_response = record_responsify(record_json_v1, 'application/json')
record_form_json_v1_response = record_responsify(record_form_json_v1,
                                                 'application/json')
record_json_v1_search = search_responsify(record_json_v1, 'application/json')
basic_json_v1_response = record_responsify(basic_json_v1,
                                           'application/basic+json')
permissions_json_v1_response = record_responsify(
    permissions_json_v1, 'application/permissions+json')

# JSON record serializer for search results.
basic_json_v1_search = search_responsify(basic_json_v1,
                                         'application/basic+json')
Beispiel #36
0
            self, metadata, item_pid):
        """Process for PENDING, ITEM_AT_DESK, ITEM_IN_TRANSIT_FOR_PICKUP."""
        pickup_loc = Location.get_record_by_pid(
            metadata['pickup_location_pid'])
        metadata['pickup_name'] = \
            pickup_loc.get('pickup_name', pickup_loc.get('name'))
        if metadata['state'] == LoanState.ITEM_AT_DESK:
            metadata['rank'] = 0
        if metadata['state'] in [
                LoanState.PENDING, LoanState.ITEM_IN_TRANSIT_FOR_PICKUP
        ]:
            patron = Patron.get_record_by_pid(metadata['patron_pid'])
            item = Item.get_record_by_pid(item_pid)
            metadata['rank'] = item.patron_request_rank(patron)

    def _process_loan_returned_in_transit_to_house_cancelled(
            self, metadata, loan):
        """Process for ITEM_RETURNED, ITEM_IN_TRANSIT_TO_HOUSE, CANCELLED."""
        metadata['pickup_library_name'] = Location\
            .get_record_by_pid(loan['pickup_location_pid'])\
            .get_library().get('name')
        metadata['transaction_library_name'] = Location\
            .get_record_by_pid(loan['transaction_location_pid'])\
            .get_library().get('name')


json_loan = LoanJSONSerializer(RecordSchemaJSONV1)
"""JSON v1 serializer."""

json_loan_search = search_responsify(json_loan, 'application/rero+json')
    AuthorsOnlyControlNumberSchema,
    AuthorsPublicSchema,
)
from inspirehep.records.marshmallow.base import wrap_schema_class_with_metadata
from inspirehep.records.serializers.response import record_responsify
from inspirehep.serializers import ConditionalMultiSchemaJSONSerializer, JSONSerializer

authors_json = ConditionalMultiSchemaJSONSerializer([
    (
        lambda _: is_superuser_or_cataloger_logged_in(),
        wrap_schema_class_with_metadata(AuthorsAdminSchema),
    ),
    (None, wrap_schema_class_with_metadata(AuthorsPublicSchema)),
])
authors_json_response = record_responsify(authors_json, "application/json")
authors_json_response_search = search_responsify(authors_json,
                                                 "application/json")

authors_json_detail = JSONSerializer(
    wrap_schema_class_with_metadata(AuthorsDetailSchema))
authors_json_detail_response = record_responsify(
    authors_json_detail, "application/vnd+inspire.record.ui+json")

authors_json_list = JSONSerializer(
    wrap_schema_class_with_metadata(AuthorsListSchema))
authors_json_list_response = search_responsify(
    authors_json_list, "application/vnd+inspire.record.ui+json")

authors_control_number_only_json = JSONSerializer(
    wrap_schema_class_with_metadata(AuthorsOnlyControlNumberSchema))
authors_control_number_only_json_response = record_responsify(
    authors_control_number_only_json,
Beispiel #38
0
#: DublinCore record serializer for individual records.
dc_v1_response = record_responsify(dc_v1, 'application/x-dc+xml')
#: CSL-JSON record serializer for individual records.
csl_v1_response = record_responsify(
    csl_v1, 'application/vnd.citationstyles.csl+json')
#: CSL Citation Formatter serializer for individual records.
citeproc_v1_response = record_responsify(citeproc_v1, 'text/x-bibliography')
#: OpenAIRE JSON serializer for individual records.
openaire_json_v1_response = record_responsify(openaire_json_v1,
                                              'application/x-openaire+json')
schemaorg_jsonld_v1_response = record_responsify(schemaorg_jsonld_v1,
                                                 'application/ld+json')


#: JSON record serializer for search results.
json_v1_search = search_responsify(json_v1, 'application/json')
#: JSON record legacy serializer for search results.
legacyjson_v1_search = search_responsify(legacyjson_v1, 'application/json')
#: MARCXML record serializer for search records.
marcxml_v1_search = search_responsify(marcxml_v1, 'application/marcxml+xml')
#: BibTeX serializer for search records.
bibtex_v1_search = search_responsify(bibtex_v1, 'application/x-bibtex')
#: DataCite v3.1 record serializer for search records.
datacite_v31_search = search_responsify(
    datacite_v31, 'application/x-datacite+xml')
#: DublinCore record serializer for search records.
dc_v1_search = search_responsify(dc_v1, 'application/x-dc+xml')
schemaorg_jsonld_v1_search = record_responsify(schemaorg_jsonld_v1, 'application/ld+json')

# Deposit serializers
# ===================
Beispiel #39
0
    LiteratureDetailSchema,
    LiteratureListWrappedSchema,
    LiteraturePublicListSchema,
    LiteraturePublicSchema,
)
from inspirehep.records.serializers.response import record_responsify
from inspirehep.serializers import (
    ConditionalMultiSchemaJSONSerializer,
    JSONSerializer,
    JSONSerializerFacets,
    JSONSerializerLiteratureSearch,
)

# Facets
facets_json = JSONSerializerFacets(Schema)
facets_json_response_search = search_responsify(facets_json, "application/json")

# Literature
literature_json_search = ConditionalMultiSchemaJSONSerializer(
    [
        (
            lambda _: is_superuser_or_cataloger_logged_in(),
            wrap_schema_class_with_metadata(LiteratureAdminSchema),
        ),
        (None, wrap_schema_class_with_metadata(LiteraturePublicListSchema)),
    ]
)

literature_json = ConditionalMultiSchemaJSONSerializer(
    [
        (
Beispiel #40
0
#: BibTeX record serializer for individual records.
bibtex_v1_response = record_responsify(bibtex_v1, 'application/x-bibtex')
#: DataCite v3.1 record serializer for individual records.
datacite_v31_response = record_responsify(
    datacite_v31, 'application/x-datacite+xml')
#: DublinCore record serializer for individual records.
dc_v1_response = record_responsify(dc_v1, 'application/x-dc+xml')
#: CSL-JSON record serializer for individual records.
csl_v1_response = record_responsify(
    csl_v1, 'application/vnd.citationstyles.csl+json')
#: CSL Citation Formatter serializer for individual records.
citeproc_v1_response = record_responsify(citeproc_v1, 'text/x-bibliography')


#: JSON record serializer for search results.
json_v1_search = search_responsify(json_v1, 'application/json')
#: JSON record legacy serializer for search results.
legacyjson_v1_search = search_responsify(legacyjson_v1, 'application/json')
#: MARCXML record serializer for search records.
marcxml_v1_search = search_responsify(marcxml_v1, 'application/marcxml+xml')
#: BibTeX serializer for search records.
bibtex_v1_search = search_responsify(bibtex_v1, 'application/x-bibtex')
#: DataCite v3.1 record serializer for search records.
datacite_v31_search = search_responsify(
    datacite_v31, 'application/x-datacite+xml')
#: DublinCore record serializer for search records.
dc_v1_search = search_responsify(dc_v1, 'application/x-dc+xml')

# Deposit serializers
# ===================
#: JSON record legacy serializer for individual deposits.
# granted to it by virtue of its status as an Intergovernmental Organization
# or submit itself to any jurisdiction.

"""Common (to all collections) API of INSPIRE."""

from __future__ import absolute_import, division, print_function

import json

from invenio_records_rest.serializers.response import search_responsify


class APIRecidsSerializer(object):
    """Recids serializer."""

    def serialize_search(self, pid_fetcher, search_result, item_links_factory=None, links=None):
        return json.dumps(dict(
            hits=dict(
                recids=[record['_source']['control_number'] for record in search_result['hits']['hits']],
                total=search_result['hits']['total'],
            ),
            links=links or {},
        ))


json_recids = APIRecidsSerializer()
json_recids_response = search_responsify(
    json_recids,
    'application/vnd+inspire.ids+json'
)