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
|
code = e.code
|
||||||
|
|
||||||
metrics.send('{}_status_code'.format(code), 'counter', 1)
|
metrics.send('{}_status_code'.format(code), 'counter', 1)
|
||||||
|
|
||||||
|
app.logger.exception(e)
|
||||||
return jsonify(error=str(e)), code
|
return jsonify(error=str(e)), code
|
||||||
|
|
|
@ -13,7 +13,7 @@ from flask import request, current_app
|
||||||
from sqlalchemy.orm.collections import InstrumentedList
|
from sqlalchemy.orm.collections import InstrumentedList
|
||||||
|
|
||||||
from inflection import camelize, underscore
|
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):
|
class LemurSchema(Schema):
|
||||||
|
@ -68,10 +68,9 @@ class LemurOutputSchema(LemurSchema):
|
||||||
data = self.unwrap_envelope(data, many)
|
data = self.unwrap_envelope(data, many)
|
||||||
return self.under(data, many=many)
|
return self.under(data, many=many)
|
||||||
|
|
||||||
@pre_dump(pass_many=True)
|
|
||||||
def unwrap_envelope(self, data, many):
|
def unwrap_envelope(self, data, many):
|
||||||
if many:
|
if many:
|
||||||
if data:
|
if data['items']:
|
||||||
if isinstance(data, InstrumentedList) or isinstance(data, list):
|
if isinstance(data, InstrumentedList) or isinstance(data, list):
|
||||||
self.context['total'] = len(data)
|
self.context['total'] = len(data)
|
||||||
return data
|
return data
|
||||||
|
@ -115,6 +114,18 @@ def wrap_errors(messages):
|
||||||
return errors
|
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 validate_schema(input_schema, output_schema):
|
||||||
def decorator(f):
|
def decorator(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
|
@ -144,10 +155,7 @@ def validate_schema(input_schema, output_schema):
|
||||||
if not resp:
|
if not resp:
|
||||||
return dict(message="No data found"), 404
|
return dict(message="No data found"), 404
|
||||||
|
|
||||||
if output_schema:
|
return unwrap_pagination(resp, output_schema), 200
|
||||||
data = output_schema.dump(resp)
|
|
||||||
return data.data, 200
|
|
||||||
return resp, 200
|
|
||||||
|
|
||||||
return decorated_function
|
return decorated_function
|
||||||
return decorator
|
return decorator
|
||||||
|
|
|
@ -29,7 +29,8 @@ class DestinationOutputSchema(LemurOutputSchema):
|
||||||
|
|
||||||
@post_dump
|
@post_dump
|
||||||
def fill_object(self, data):
|
def fill_object(self, data):
|
||||||
data['plugin']['pluginOptions'] = data['options']
|
if data:
|
||||||
|
data['plugin']['pluginOptions'] = data['options']
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,8 @@ class NotificationOutputSchema(LemurOutputSchema):
|
||||||
|
|
||||||
@post_dump
|
@post_dump
|
||||||
def fill_object(self, data):
|
def fill_object(self, data):
|
||||||
data['plugin']['pluginOptions'] = data['options']
|
if data:
|
||||||
|
data['plugin']['pluginOptions'] = data['options']
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,8 @@ class SourceOutputSchema(LemurOutputSchema):
|
||||||
|
|
||||||
@post_dump
|
@post_dump
|
||||||
def fill_object(self, data):
|
def fill_object(self, data):
|
||||||
data['plugin']['pluginOptions'] = data['options']
|
if data:
|
||||||
|
data['plugin']['pluginOptions'] = data['options']
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ def test_authority_input_schema(client, role):
|
||||||
|
|
||||||
|
|
||||||
def test_user_authority(session, client, authority, role, user, issuer_plugin):
|
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 = user['user']
|
||||||
u.roles.append(role)
|
u.roles.append(role)
|
||||||
authority.roles.append(role)
|
authority.roles.append(role)
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -49,7 +49,7 @@ install_requires = [
|
||||||
'six==1.10.0',
|
'six==1.10.0',
|
||||||
'gunicorn==19.6.0',
|
'gunicorn==19.6.0',
|
||||||
'marshmallow-sqlalchemy==0.12.0',
|
'marshmallow-sqlalchemy==0.12.0',
|
||||||
'marshmallow==2.4.0',
|
'marshmallow==2.10.4',
|
||||||
'pycrypto==2.6.1',
|
'pycrypto==2.6.1',
|
||||||
'cryptography==1.6',
|
'cryptography==1.6',
|
||||||
'pyopenssl==16.1.0',
|
'pyopenssl==16.1.0',
|
||||||
|
|
Loading…
Reference in New Issue