Commit be4dbbea authored by Pablo Panero's avatar Pablo Panero Committed by Pablo Panero
Browse files

serializer: remove base64 content from the response

parent d695e7ed
......@@ -93,7 +93,7 @@ RECORDS_REST_ENDPOINTS = dict(
links_factory_imp='invenio_records_rest.links:default_links_factory',
record_class='cern_search_rest_api.modules.cernsearch.api:CernSearchRecord',
record_serializers={
'application/json': ('invenio_records_rest.serializers'
'application/json': ('cern_search_rest_api.modules.cernsearch.serializers'
':json_v1_response'),
},
record_loaders={
......@@ -104,7 +104,7 @@ RECORDS_REST_ENDPOINTS = dict(
search_class='cern_search_rest_api.modules.cernsearch.search.RecordCERNSearch',
search_index=os.getenv('CERN_SEARCH_INSTANCE', 'cernsearch-test'),
search_serializers={
'application/json': ('invenio_records_rest.serializers'
'application/json': ('cern_search_rest_api.modules.cernsearch.serializers'
':json_v1_search'),
},
search_factory_imp='cern_search_rest_api.modules.cernsearch.search.csas_search_factory',
......
......@@ -16,7 +16,7 @@ from .providers import CERNSearchRecordIdProvider
def recid_fetcher(record_uuid, data):
"""Fetch PID from record."""
return FetchedPID(
provider=CERNSearchRecordIdProvider,
# provider=CERNSearchRecordIdProvider,
pid_type='recid',
pid_value=str(data['recid'])
)
\ No newline at end of file
......@@ -9,6 +9,6 @@
from __future__ import absolute_import, print_function
from .json import CSASRecordSchemaV1
from .json import CSASRecordSchemaV1, CSASRecordSearchSchemaJSONV1
__all__ = ('CSASRecordSchemaV1',)
\ No newline at end of file
__all__ = ('CSASRecordSchemaV1', 'CSASRecordSearchSchemaJSONV1')
\ No newline at end of file
......@@ -11,7 +11,10 @@
from flask import current_app
from invenio_records_rest.schemas import RecordMetadataSchemaJSONV1
from invenio_indexer.utils import default_record_to_index
from marshmallow import validates_schema, ValidationError
from invenio_records_rest.schemas.json import RecordSchemaJSONV1
from marshmallow import validates_schema, ValidationError, post_dump
from cern_search_rest_api.modules.cernsearch.utils import record_from_index
def has_and_needs_binary(original_data):
......@@ -46,3 +49,15 @@ class CSASRecordSchemaV1(RecordMetadataSchemaJSONV1):
raise ValidationError('Record to be index belongs to binary index '
'but does not contain the [b64] field')
return
class CSASRecordSearchSchemaJSONV1(RecordSchemaJSONV1):
@post_dump()
def remove_base64(self, data):
""" If needed remove the base64 data from the response """
es_index, doc = record_from_index(data)
binary_index_list = current_app.config['SEARCH_DOC_PIPELINES']
if doc in binary_index_list:
data.get('metadata').get("_data").pop('b64')
return data
\ No newline at end of file
......@@ -14,20 +14,21 @@ from invenio_records_rest.serializers.response import record_responsify, \
search_responsify
from cern_search_rest_api.modules.cernsearch.marshmallow import \
CSASRecordSchemaV1
CSASRecordSchemaV1, CSASRecordSearchSchemaJSONV1
# Serializers
# ===========
#: JSON serializer definition.
json_v1 = JSONSerializer(CSASRecordSchemaV1, replace_refs=True)
json_v1_records = JSONSerializer(CSASRecordSearchSchemaJSONV1)
# Records-REST serializers
# ========================
#: JSON record serializer for individual records.
json_v1_response = record_responsify(json_v1, 'application/json')
json_v1_response = record_responsify(json_v1_records, 'application/json')
#: JSON record serializer for search results.
json_v1_search = search_responsify(json_v1, 'application/json')
json_v1_search = search_responsify(json_v1_records, 'application/json')
__all__ = (
'json_v1',
......
......@@ -9,9 +9,34 @@
"""Helper methods for CERN Search records."""
from flask import g
from flask import current_app, g
from invenio_search import current_search
from invenio_search.utils import schema_to_index
def get_user_provides():
"""Extract the user's provides from g."""
return [need.value for need in g.identity.provides]
def record_from_index(record):
"""Get index/doc_type given a record.
It tries to extract from `record['$schema']` the index and doc_type.
If it fails, return the default values.
:param record: The record object.
:returns: Tuple (index, doc_type).
"""
index_names = current_search.mappings.keys()
schema = record.get('metadata').get('$schema', '')
if isinstance(schema, dict):
schema = schema.get('$ref', '')
index, doc_type = schema_to_index(schema, index_names=index_names)
if index and doc_type:
return index, doc_type
else:
return (current_app.config['INDEXER_DEFAULT_INDEX'],
current_app.config['INDEXER_DEFAULT_DOC_TYPE'])
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment