def test_uses_first_schema_that_returns_true_for_condition_that_uses_data(): class BaseMetadataSchema(Schema): metadata = fields.Method("get_metadata") class Schema1(BaseMetadataSchema): def get_metadata(self, data): return {"field1": data["metadata"]["field1"]} class Schema2(BaseMetadataSchema): def get_metadata(self, data): return {"field2": data["metadata"]["field2"]} class Schema3(BaseMetadataSchema): def get_metadata(self, data): return {"field3": data["metadata"]["field3"]} data = { "field1": "value1", "field2": "value2", "field3": "value3", "types": [2, 3] } serializer = ConditionalMultiSchemaJSONSerializer([ (lambda data: 1 in data["metadata"]["types"], Schema1), (lambda data: 2 in data["metadata"]["types"], Schema2), (lambda data: 3 in data["metadata"]["types"], Schema3), ]) serialized = json.loads( serializer.serialize( PersistentIdentifier(pid_type="recid", pid_value="1"), InspireRecord(data))) assert serialized["metadata"] == {"field2": "value2"}
def test_first_conditionless_schema_if_others_do_not_return_true(): class BaseMetadataSchema(Schema): metadata = fields.Method("get_metadata") class Schema1(BaseMetadataSchema): def get_metadata(self, data): return {"field1": data["metadata"]["field1"]} class Schema2(BaseMetadataSchema): def get_metadata(self, data): return {"field2": data["metadata"]["field2"]} data = {"field1": "value1", "field2": "value2"} serializer = ConditionalMultiSchemaJSONSerializer([(lambda _: False, Schema1), (None, Schema2)]) serialized = json.loads( serializer.serialize( PersistentIdentifier(pid_type="recid", pid_value="1"), InspireRecord(data))) assert serialized["metadata"] == {"field2": "value2"}
# -*- 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 record_responsify from inspirehep.accounts.api import is_superuser_or_cataloger_logged_in from inspirehep.records.marshmallow.base import EnvelopeSchema, ForbiddenSchema from inspirehep.serializers import ConditionalMultiSchemaJSONSerializer records_raw_json = ConditionalMultiSchemaJSONSerializer( [ ( lambda _: is_superuser_or_cataloger_logged_in(), EnvelopeSchema, ), (None, ForbiddenSchema), ] ) raw_json_detail_response = record_responsify( records_raw_json, "application/vnd+inspire.record.raw+json", )
from inspirehep.accounts.api import is_superuser_or_cataloger_logged_in from inspirehep.records.marshmallow.base import wrap_schema_class_with_metadata from inspirehep.records.marshmallow.institutions import InstitutionsPublicSchema from inspirehep.records.marshmallow.institutions.base import InstitutionsAdminSchema from inspirehep.records.marshmallow.institutions.ui import ( InstitutionsDetailSchema, 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")
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( [ ( lambda _: is_superuser_or_cataloger_logged_in(), wrap_schema_class_with_metadata(LiteratureAdminSchema), ), (None, wrap_schema_class_with_metadata(LiteraturePublicSchema)), ] )
# # 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)