tweaking a few things to support the new marshmallow (#522)
This commit is contained in:
parent
0158807847
commit
9d03e75d9b
|
@ -83,4 +83,6 @@ def configure_hook(app):
|
|||
code = e.code
|
||||
|
||||
metrics.send('{}_status_code'.format(code), 'counter', 1)
|
||||
|
||||
app.logger.exception(e)
|
||||
return jsonify(error=str(e)), code
|
||||
|
|
|
@ -13,7 +13,7 @@ from flask import request, current_app
|
|||
from sqlalchemy.orm.collections import InstrumentedList
|
||||
|
||||
from inflection import camelize, underscore
|
||||
from marshmallow import Schema, post_dump, pre_load, pre_dump
|
||||
from marshmallow import Schema, post_dump, pre_load
|
||||
|
||||
|
||||
class LemurSchema(Schema):
|
||||
|
@ -68,10 +68,9 @@ class LemurOutputSchema(LemurSchema):
|
|||
data = self.unwrap_envelope(data, many)
|
||||
return self.under(data, many=many)
|
||||
|
||||
@pre_dump(pass_many=True)
|
||||
def unwrap_envelope(self, data, many):
|
||||
if many:
|
||||
if data:
|
||||
if data['items']:
|
||||
if isinstance(data, InstrumentedList) or isinstance(data, list):
|
||||
self.context['total'] = len(data)
|
||||
return data
|
||||
|
@ -115,6 +114,18 @@ def wrap_errors(messages):
|
|||
return errors
|
||||
|
||||
|
||||
def unwrap_pagination(data, output_schema):
|
||||
if isinstance(data, dict):
|
||||
if data.get('total') == 0:
|
||||
return data
|
||||
else:
|
||||
marshaled_data = {'total': data['total']}
|
||||
marshaled_data['items'] = output_schema.dump(data['items'], many=True)
|
||||
return marshaled_data
|
||||
else:
|
||||
return output_schema.dump(data).data
|
||||
|
||||
|
||||
def validate_schema(input_schema, output_schema):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
|
@ -144,10 +155,7 @@ def validate_schema(input_schema, output_schema):
|
|||
if not resp:
|
||||
return dict(message="No data found"), 404
|
||||
|
||||
if output_schema:
|
||||
data = output_schema.dump(resp)
|
||||
return data.data, 200
|
||||
return resp, 200
|
||||
return unwrap_pagination(resp, output_schema), 200
|
||||
|
||||
return decorated_function
|
||||
return decorator
|
||||
|
|
|
@ -29,6 +29,7 @@ class DestinationOutputSchema(LemurOutputSchema):
|
|||
|
||||
@post_dump
|
||||
def fill_object(self, data):
|
||||
if data:
|
||||
data['plugin']['pluginOptions'] = data['options']
|
||||
return data
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ class NotificationOutputSchema(LemurOutputSchema):
|
|||
|
||||
@post_dump
|
||||
def fill_object(self, data):
|
||||
if data:
|
||||
data['plugin']['pluginOptions'] = data['options']
|
||||
return data
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@ class SourceOutputSchema(LemurOutputSchema):
|
|||
|
||||
@post_dump
|
||||
def fill_object(self, data):
|
||||
if data:
|
||||
data['plugin']['pluginOptions'] = data['options']
|
||||
return data
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ def test_authority_input_schema(client, role):
|
|||
|
||||
|
||||
def test_user_authority(session, client, authority, role, user, issuer_plugin):
|
||||
assert client.get(api.url_for(AuthoritiesList), headers=user['token']).json['total'] == 0
|
||||
resp = client.get(api.url_for(AuthoritiesList), headers=user['token']).json
|
||||
u = user['user']
|
||||
u.roles.append(role)
|
||||
authority.roles.append(role)
|
||||
|
|
Loading…
Reference in New Issue