Skip to content

Commit

Permalink
Merge 96693d5 into ee02d8c
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiaraBi committed Feb 14, 2019
2 parents ee02d8c + 96693d5 commit 29aba79
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
9 changes: 1 addition & 8 deletions invenio_files_rest/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,7 @@ def schema_from_context(context):


def _format_args():
try:
pretty_format = \
current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and \
not request.is_xhr
except RuntimeError:
pretty_format = False

if pretty_format:
if request and request.args.get('prettyprint'):
return dict(
indent=2,
separators=(', ', ': '),
Expand Down
1 change: 1 addition & 0 deletions invenio_files_rest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ def load_or_import_from_config(key, app=None, default=None):
app = app or current_app
imp = app.config.get(key)
return obj_or_import_string(imp, default=default)

52 changes: 52 additions & 0 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2019 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# 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.

"""Test serializer."""

from __future__ import absolute_import, print_function

from marshmallow import Schema, fields

from invenio_files_rest.serializer import json_serializer, serializer_mapping


def test_serialize_pretty(app):
"""Test pretty JSON."""
class TestSchema(Schema):
title = fields.Str(attribute='title')

data = {'title': 'test'}
context = {'bucket': '11111111-1111-1111-1111-111111111111',
'class': 'TestSchema', 'many': False}

serializer_mapping['TestSchema'] = TestSchema

with app.test_request_context():
# import wdb; wdb.set_trace()
assert json_serializer(data=data, context=context).data == \
'{"title":"test"}'

with app.test_request_context('/?prettyprint=1'):
assert json_serializer(data=data, context=context).data == \
'{\n "title": "test"\n}'

0 comments on commit 29aba79

Please sign in to comment.