Skip to content

Commit

Permalink
is private bug (#8)
Browse files Browse the repository at this point in the history
Fix is private not passed to to_jsonable
  • Loading branch information
melodylovepug committed Jul 1, 2020
1 parent 0b7d865 commit ec47617
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
4 changes: 2 additions & 2 deletions rest_helpers/responses.py
Expand Up @@ -69,7 +69,7 @@ def ok(framework_adapter, data, page_size=None, id_only=False, is_private=None):
Returns:
[dict] -- A json serializable representation of a JSONAPI response.
"""
return success(framework_adapter, data, 200, page_size = page_size, id_only=id_only)
return success(framework_adapter, data, 200, page_size = page_size, id_only=id_only, is_private=is_private)


def created(framework_adapter, data):
Expand Down Expand Up @@ -100,7 +100,7 @@ def success(framework_adapter, data, status_code, meta=None, links=None, page_si
jsonable = to_jsonable(data, is_private=is_private)
else:
resp = SuccessResponse(data=data, meta=meta, links=links)
jsonable = response_to_jsonable(resp, id_only=id_only)
jsonable = response_to_jsonable(resp, id_only=id_only,is_private=is_private)


if "json_path" in request_args:
Expand Down
13 changes: 6 additions & 7 deletions rest_helpers/type_serializers.py
Expand Up @@ -26,7 +26,6 @@ def to_jsonable(obj, no_empty_field=False, is_private=None):
Returns:
dict -- A dictionary that can be used by json.dumps
"""

if is_private is None:
is_private = lambda k: True if str(k)[0] != '_' else False

Expand All @@ -43,7 +42,7 @@ def to_jsonable(obj, no_empty_field=False, is_private=None):
return obj
return {str(k): to_jsonable(v, no_empty_field, is_private)for k, v in dic.items() if is_private(k) and (not no_empty_field or v is not None and v != "")}

def response_to_jsonable(response, generate_self_links=True, id_only=False):
def response_to_jsonable(response, generate_self_links=True, id_only=False,is_private=None):
"""
Transform a response object into a json serializable (jsonable) object that
matches the jsonapi requirements.
Expand All @@ -64,18 +63,18 @@ def response_to_jsonable(response, generate_self_links=True, id_only=False):
# hence it needs some special serialization logic)
dic = response.__dict__.copy()
dic.pop("data")
return_value = to_jsonable(dic, no_empty_field=True)
return_value = to_jsonable(dic, no_empty_field=True,is_private=is_private)

if response.data is not None:
jsonable_data = resource_to_jsonable(response.data, generate_self_links)
jsonable_data = resource_to_jsonable(response.data, generate_self_links,is_private=is_private)
if id_only:
jsonable_data = jsonable_data["id"] if not isinstance(jsonable_data, Iterable) else [x["id"] for x in jsonable_data]
return_value["data"] = jsonable_data

return return_value


def resource_to_jsonable(resource, generate_self_links=True):
def resource_to_jsonable(resource, generate_self_links=True,is_private=None):
"""
Transform a resource object or a resource object list into
a json serializable (jsonable) object that matches the jsonapi
Expand All @@ -94,11 +93,11 @@ def resource_to_jsonable(resource, generate_self_links=True):
"""

if isinstance(resource, list):
return [resource_to_jsonable(x) for x in resource]
return [resource_to_jsonable(x,is_private) for x in resource]

assert isinstance(resource, Resource)

json_resource = resource.to_primitive() if (hasattr(resource, "to_primitive") and callable(resource,to_primitive)) else to_jsonable(resource)
json_resource = resource.to_primitive() if (hasattr(resource, "to_primitive") and callable(resource,to_primitive)) else to_jsonable(resource, is_private=is_private)
special = ["id", "type", "relationships", "links", "meta"]
for key in special:
json_resource.pop(key, None)
Expand Down

0 comments on commit ec47617

Please sign in to comment.