Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update flask_restplus_patched to have a possibility to use non model … #37

Merged
merged 1 commit into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions flask_restplus_patched/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
except ImportError:
class HTTPStatus:
NO_CONTENT = 204
ACCEPTED = 202
19 changes: 10 additions & 9 deletions flask_restplus_patched/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ def response(self, model=None, code=200, description=None, **kwargs):
... abort(403)
... return Team.query.all()
"""
if model is None and code != HTTPStatus.NO_CONTENT:
ALLOWED_EMPTY_BODY_STATUSES = (HTTPStatus.NO_CONTENT, HTTPStatus.ACCEPTED)

if model is None and code not in ALLOWED_EMPTY_BODY_STATUSES:
if code not in http_exceptions.default_exceptions:
raise ValueError("`model` parameter is required for code %d" % code)
model = self.model(
Expand All @@ -126,7 +128,7 @@ def response(self, model=None, code=200, description=None, **kwargs):
if description is None:
if code in http_exceptions.default_exceptions:
description = http_exceptions.default_exceptions[code].description
elif code == HTTPStatus.NO_CONTENT:
elif code in ALLOWED_EMPTY_BODY_STATUSES:
description = 'Request fulfilled, nothing follows'

def response_serializer_decorator(func):
Expand All @@ -138,16 +140,15 @@ def dump_wrapper(*args, **kwargs):
# pylint: disable=missing-docstring
response = func(*args, **kwargs)

if isinstance(response, flask.Response):
return response

elif response is None:
if code == HTTPStatus.NO_CONTENT:
if response is None:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove one blank line above.

if code in ALLOWED_EMPTY_BODY_STATUSES:
return flask.Response(
status=HTTPStatus.NO_CONTENT,
status=code,
content_type='application/json'
)
raise ValueError("Reponse must not be empty with code 200")
elif isinstance(response, flask.Response) or model is None:
return response

return model.dump(response).data

Expand All @@ -169,7 +170,7 @@ def decorator(func_or_class):
response_serializer_decorator(func_or_class)
)

if code == HTTPStatus.NO_CONTENT:
if code in ALLOWED_EMPTY_BODY_STATUSES:
api_model = None
else:
if isinstance(model, Model):
Expand Down