def test_serialize_pretty(app): """Test pretty JSON.""" class TestSchema(Schema): title = fields.Str(attribute='metadata.title') pid = PersistentIdentifier(pid_type='recid', pid_value='2'), rec = Record({'title': 'test'}) app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False assert JSONSerializer(TestSchema).serialize(pid, rec) == \ '{"title":"test"}' app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True assert JSONSerializer(TestSchema).serialize(pid, rec) == \ '{\n "title": "test"\n}'
def test_serialize_pretty(app): """Test pretty JSON.""" class TestSchema(Schema): title = fields.Str(attribute='metadata.title') pid = PersistentIdentifier(pid_type='recid', pid_value='2'), rec = Record({'title': 'test'}) with app.test_request_context(): assert JSONSerializer(TestSchema).serialize(pid, rec) == \ '{"title":"test"}' with app.test_request_context('/?prettyprint=1'): assert JSONSerializer(TestSchema).serialize(pid, rec) == \ '{\n "title": "test"\n}'
def test_serialize_search(): """Test JSON serialize.""" class TestSchema(Schema): title = fields.Str(attribute='metadata.mytitle') id = PIDField(attribute='pid.pid_value') def fetcher(obj_uuid, data): assert obj_uuid in ['a', 'b'] return PersistentIdentifier(pid_type='recid', pid_value=data['pid']) data = json.loads(JSONSerializer(TestSchema).serialize_search( fetcher, dict( hits=dict( hits=[ {'_source': dict(mytitle='test1', pid='1'), '_id': 'a', '_version': 1}, {'_source': dict(mytitle='test2', pid='2'), '_id': 'b', '_version': 1}, ], total=2, ), aggregations={}, ) )) assert data['aggregations'] == {} assert 'links' in data assert data['hits'] == dict( hits=[ dict(title='test1', id='1'), dict(title='test2', id='2'), ], total=2, )
def serialize_search(self, pid_fetcher, search_result, links=None, item_links_factory=None, **kwargs): """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. """ format = request.values.get('format') if format and format == 'atom': mimetype = 'application/atom+xml' atom_v1 = AtomSerializer(RecordSchemaJSONV1) return atom_v1.serialize_search(pid_fetcher, search_result, links=None, item_links_factory=None, **kwargs), mimetype elif format and format == 'rss': mimetype = 'application/rss+xml' rss_v1 = RssSerializer(RecordSchemaJSONV1) return rss_v1.serialize_search(pid_fetcher, search_result, links=None, item_links_factory=None, **kwargs), mimetype elif format and format == 'jpcoar': mimetype = 'application/xml' jpcoar_v1 = JpcoarSerializer(RecordSchemaJSONV1) return jpcoar_v1.serialize_search(pid_fetcher, search_result, links=None, item_links_factory=None, **kwargs), mimetype else: mimetype = 'application/json' json_v1 = JSONSerializer(RecordSchemaJSONV1) return json_v1.serialize_search(pid_fetcher, search_result, links=None, item_links_factory=None, **kwargs), mimetype
def preprocess_search_hit(pid, record_hit, links_factory=None, **kwargs): """Prepare a record hit from Elasticsearch for serialization.""" record = _JSONSerializer.preprocess_search_hit( pid=pid, record_hit=record_hit, links_factory=links_factory, kwargs=kwargs) if record.get('_explanation'): record[1:] = record.get('_explanation') del record['_explanation'] return record
def test_serialize(): """Test JSON serialize.""" class TestSchema(Schema): title = fields.Str(attribute='metadata.mytitle') id = fields.Str(attribute='pid.pid_value') data = json.loads( JSONSerializer(TestSchema).serialize( PersistentIdentifier(pid_type='recid', pid_value='2'), Record({'mytitle': 'test'}))) assert data['title'] == 'test' assert data['id'] == '2'
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']
def test_get_record(test_custom_endpoints_app, test_records): """Test the creation of a custom endpoint using RecordResource.""" test_records = test_records """Test creation of a RecordResource view.""" blueprint = Blueprint( 'test_invenio_records_rest', __name__, ) json_v1 = JSONSerializer(RecordSchemaJSONV1) blueprint.add_url_rule( '/records/<pid(recid):pid_value>', view_func=RecordResource.as_view( 'recid_item', serializers={ 'application/json': record_responsify( json_v1, 'application/json' ) }, default_media_type='application/json', read_permission_factory=allow_all, update_permission_factory=allow_all, delete_permission_factory=allow_all, ) ) test_custom_endpoints_app.register_blueprint(blueprint) with test_custom_endpoints_app.app_context(): pid, record = test_records[0] url = url_for('test_invenio_records_rest.recid_item', pid_value=pid.pid_value, user=1) with test_custom_endpoints_app.test_client() as client: res = client.get(url) assert res.status_code == 200 # Check metadata data = get_json(res) assert record == data['metadata']
# Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. """Community serializers.""" from __future__ import absolute_import, print_function from invenio_records_rest.serializers.json import JSONSerializer from invenio_records_rest.serializers.response import record_responsify, \ search_responsify from ..marshmallow import CommunitySchemaV1 # Serializers # =========== #: JSON serializer definition. json_v1 = JSONSerializer(CommunitySchemaV1, 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', )
However, it looks like that browsers are not sending the If-Match/ETag headers on PUT requests. """ def view(pid, record, code=200, headers=None, links_factory=None): response = current_app.response_class( serializer.serialize(pid, record, links_factory=links_factory), mimetype=mimetype) response.status_code = code response.cache_control.no_cache = True # etag/last-modified headers removed if headers is not None: response.headers.extend(headers) if links_factory is not None: add_link_header(response, links_factory(pid)) return response return view csv_v1 = CSVSerializer(ILSRecordSchemaJSONV1, csv_excluded_fields=[]) csv_v1_response = record_responsify_no_etag(csv_v1, "text/csv") csv_v1_search = search_responsify(csv_v1, "text/csv") json_v1 = JSONSerializer(ILSRecordSchemaJSONV1, replace_refs=True) json_v1_response = record_responsify_no_etag(json_v1, "application/json") json_v1_search = search_responsify(json_v1, "application/json")
# # 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. """Submissions serializers.""" from inspire_utils.record import get_values_for_schema from invenio_records_rest.serializers.json import JSONSerializer from inspirehep.accounts.api import get_current_user_orcid from inspirehep.serializers import ConditionalMultiSchemaJSONSerializer from inspirehep.submissions.marshmallow.job import Job from .marshmallow import Author, Conference, Literature, SameAuthor, Seminar def does_current_user_own_author_record(author): author_orcids = get_values_for_schema(author.get("ids", []), "ORCID") if author_orcids: author_orcid = author_orcids.pop() return get_current_user_orcid() == author_orcid return False literature_v1 = JSONSerializer(Literature) author_v1 = ConditionalMultiSchemaJSONSerializer([ (does_current_user_own_author_record, SameAuthor), (None, Author) ]) job_v1 = JSONSerializer(Job) conference_v1 = JSONSerializer(Conference) seminar_v1 = JSONSerializer(Seminar)
# Swiss Open Access Repository is free software; you can redistribute it # and/or modify it under the terms of the MIT License; see LICENSE file for # more details. """Document serializers.""" from __future__ import absolute_import, print_function from invenio_records_rest.serializers.json import JSONSerializer from invenio_records_rest.serializers.response import record_responsify, \ search_responsify from ..marshmallow import DocumentSchemaV1 # Serializers # =========== #: JSON serializer definition. json_v1 = JSONSerializer(DocumentSchemaV1, 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 __future__ import absolute_import, print_function from invenio_records_rest.serializers.citeproc import CiteprocSerializer 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 serializer definition. from ..marshmallow.json import CitationSchemaV1 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') # Citation serializers csl_v1 = JSONSerializer(CitationSchemaV1, replace_refs=True) citeproc_v1 = CiteprocSerializer(csl_v1) citeproc_v1_response = record_responsify(citeproc_v1, 'text/x-bibliography') __all__ = ('json_v1', 'json_v1_response', 'json_v1_search', 'csl_v1', 'citeproc_v1', 'citeproc_v1_response')
# Copyright (C) 2019 Northwestern University. # # Invenio-RDM-Records is free software; you can redistribute it and/or modify # it under the terms of the MIT License; see LICENSE file for more details. """Record serializers.""" from invenio_records_rest.serializers.json import JSONSerializer from invenio_records_rest.serializers.response import record_responsify, \ search_responsify from ..marshmallow import BibliographicRecordSchemaV1 # Serializers # =========== #: JSON serializer definition. json_v1 = JSONSerializer(BibliographicRecordSchemaV1, 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', )
updated = fields.Str(dump_only=True) links = fields.Dict(dump_only=True) id = PersistentIdentifier() # @post_dump # def dump_metadata(self, source, **kwargs): # if source['metadata']['source_type'] == SourceType.JOURNAL.value: # print('SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsss') # print(source) # print('SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsss') # source['metadata'] = journal_data_schema.dump(source['metadata']) # return source source_data_schema_many = SourceDataSchemaV1(many=True) source_data_schema = SourceDataSchemaV1(many=False) source_loader_v1 = marshmallow_loader(SourceDataSchemaV1) # Serializers # =========== #: JSON serializer definition. source_v1 = JSONSerializer(SourceSchemaV1, replace_refs=True) # Records-REST serializers # ======================== #: JSON record serializer for individual records. source_v1_response = record_responsify(source_v1, 'application/json') #: JSON record serializer for search results. source_v1_search = search_responsify(source_v1, 'application/json')
# # 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. """Submissions serializers.""" from inspire_utils.record import get_values_for_schema from invenio_records_rest.serializers.json import JSONSerializer from inspirehep.accounts.api import get_current_user_orcid from inspirehep.serializers import ConditionalMultiSchemaJSONSerializer from inspirehep.submissions.marshmallow.job import Job from .marshmallow import Author, Literature, SameAuthor def does_current_user_own_author_record(author): author_orcids = get_values_for_schema(author.get("ids", []), "ORCID") if author_orcids: author_orcid = author_orcids.pop() return get_current_user_orcid() == author_orcid return False literature_v1 = JSONSerializer(Literature) author_v1 = ConditionalMultiSchemaJSONSerializer([ (does_current_user_own_author_record, SameAuthor), (None, Author) ]) job_v1 = JSONSerializer(Job)
from __future__ import absolute_import, print_function from invenio_records_rest.serializers.json import JSONSerializer from invenio_records_rest.serializers.response import ( record_responsify, search_responsify, ) from cuor.organizations.marshmallow import RecordSearchSchemaV1, MetadataSchemaBaseV1 from ..marshmallow import RecordSchemaV1 # Serializers # =========== #: JSON serializer definition. json_v1 = JSONSerializer(RecordSchemaV1, replace_refs=True) org_json_v1 = JSONSerializer(MetadataSchemaBaseV1, replace_refs=True) json_v1_for_search = JSONSerializer(RecordSearchSchemaV1, replace_refs=True) # Records-REST serializers # ======================== #: JSON record serializer for individual organizations. json_v1_response = record_responsify(json_v1, 'application/json') #: JSON record serializer for search results. json_v1_search = search_responsify(json_v1_for_search, 'application/json') __all__ = ( 'json_v1', 'json_v1_for_search', 'json_v1_response', 'json_v1_search',
from invenio_records_rest.serializers.json import JSONSerializer from invenio_records_rest.serializers.response import (record_responsify, search_responsify) from .json import RecordSerializer from .schemas.json import (BasicDepositSchema, PermissionsDepositSchema, RecordFormSchema, RecordSchema, RepositoriesDepositSchema) # Serializers # =========== # CAP JSON serializer version 1.0.0 record_json_v1 = RecordSerializer(RecordSchema) record_form_json_v1 = RecordSerializer(RecordFormSchema) basic_json_v1 = JSONSerializer(BasicDepositSchema) permissions_json_v1 = JSONSerializer(PermissionsDepositSchema) repositories_json_v1 = JSONSerializer(RepositoriesDepositSchema) # 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')
"""Record serializers.""" from __future__ import absolute_import, print_function from invenio_records_rest.serializers.json import JSONSerializer from invenio_records_rest.serializers.response import record_responsify, \ search_responsify from ..marshmallow import AuthorSchemaV1 # Serializers # =========== #: JSON serializer definition. json_v1 = JSONSerializer(AuthorSchemaV1, 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',
"""Record serialization.""" from __future__ import absolute_import, print_function from invenio_records_rest.serializers.json import JSONSerializer from invenio_records_rest.serializers.response import record_responsify, \ search_responsify 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')
# =========== #: CDS SMIL serializer version 1.0.0 smil_v1 = SmilSerializer() #: CDS VTT serializer version 1.0.0 vtt_v1 = VTTSerializer() #: Drupal JSON serializer drupal_v1 = DrupalSerializer(RecordSchemaJSONV1) #: DataCite serializer datacite_v31 = DataCite31Serializer(DataCiteSchemaV1, replace_refs=True) #: CDSDeposit serializer cdsdeposit_json_v1 = JSONSerializer(RecordSchemaJSONV1, replace_refs=True) #: CDS JSON serializer v1 json_v1 = JSONSerializer(RecordSchemaJSONV1, replace_refs=True) # Records-REST response serializers # ================================= #: SMIL record serializer for individual records. smil_v1_response = record_responsify(smil_v1, 'application/smil') #: VTT record serializer for individual records. vtt_v1_response = record_responsify(vtt_v1, 'text/vtt') #: Drupal record serializer for individual records. drupal_v1_response = record_responsify(drupal_v1, 'application/json')
from invenio_records_rest.serializers.citeproc import CiteprocSerializer from invenio_records_rest.serializers.json import JSONSerializer from invenio_records_rest.serializers.response import record_responsify, \ search_responsify from invenio_records_rest.serializers.schemas.json import RecordSchemaJSONV1 from pkg_resources import resource_filename from .depositschema import DepositSchemaV1 from .json import WekoJSONSerializer from .opensearchresponse import oepnsearch_responsify from .opensearchserializer import OpenSearchSerializer from .schemas.csl import RecordSchemaCSLJSON from .searchserializer import SearchSerializer deposit_json_v1 = JSONSerializer(DepositSchemaV1, replace_refs=True) #: JSON record serializer for individual records. deposit_json_v1_response = record_responsify(deposit_json_v1, 'application/json') # For search result list json_v1 = SearchSerializer(RecordSchemaJSONV1) json_v1_search = search_responsify(json_v1, 'application/json') # For opensearch serialize opensearch_v1 = OpenSearchSerializer(RecordSchemaJSONV1) opensearch_v1_search = oepnsearch_responsify(opensearch_v1) #: CSL-JSON serializer csl_v1 = WekoJSONSerializer(RecordSchemaCSLJSON, replace_refs=True) #: CSL Citation Formatter serializer
# # My datamodel is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. """Record serializers.""" from __future__ import absolute_import, print_function from invenio_records_rest.serializers.json import JSONSerializer from invenio_records_rest.serializers.response import record_responsify, \ search_responsify from ..marshmallow import BookSchemaV1 # Serializers # =========== #: JSON serializer definition. json_v1 = JSONSerializer(BookSchemaV1, 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', )
# -*- 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. """Submissions serializers.""" from invenio_records_rest.serializers.json import JSONSerializer from inspirehep.submissions.marshmallow.job import Job from .marshmallow import ( Author, Conference, Experiment, Institution, Literature, Seminar, ) literature_v1 = JSONSerializer(Literature) author_v1 = JSONSerializer(Author) job_v1 = JSONSerializer(Job) conference_v1 = JSONSerializer(Conference) seminar_v1 = JSONSerializer(Seminar) experiment_v1 = JSONSerializer(Experiment) institution_v1 = JSONSerializer(Institution)
# # My site is free software; you can redistribute it and/or modify it under # the terms of the MIT License; see LICENSE file for more details. """Record serializers.""" from __future__ import absolute_import, print_function from invenio_records_rest.serializers.json import JSONSerializer from invenio_records_rest.serializers.response import record_responsify, \ search_responsify from ..marshmallow import InstitutionSchemaV1 # Serializers # =========== #: JSON serializer definition. json_v1 = JSONSerializer(InstitutionSchemaV1, 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', )
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', )
LiteratureAuthorsSchemaJSONUIV1, LiteratureRecordSchemaJSONUIV1, LiteratureReferencesSchemaJSONUIV1, CitationItemSchemaV1, ) from .marcxml import MARCXMLSerializer from .response import record_responsify_nocache json_literature_ui_v1 = LiteratureJSONUISerializer( LiteratureRecordSchemaJSONUIV1) json_literature_ui_v1_search = search_responsify( json_literature_ui_v1, '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( json_literature_references_v1, 'application/json', ) json_literature_citations_v1 = LiteratureCitationsJSONSerializer( CitationItemSchemaV1) json_literature_citations_v1_response = record_responsify_nocache( json_literature_citations_v1, 'application/json', )