Skip to content

Commit

Permalink
views: traversal views don't show infinite nested data
Browse files Browse the repository at this point in the history
the flatten function will likely get moved after

closes #43
  • Loading branch information
robvdl committed Mar 16, 2024
1 parent 4e9716b commit 905bcd5
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/sambal/views/domain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
from pyramid.view import view_config

from sambal.resources import Resource, RootFactory
from sambal.resources import ContainerResource, Resource, RootFactory


def flatten(resource: Resource) -> dict:
"""Remove nested containers at the view level.
This can't easily be done at the Resource level otherwise it stops
traversal completely.
"""
# Filter children of containers to stop it returning infinite nested data.
if isinstance(resource, ContainerResource):
return {k: obj for k, obj in resource.items() if not isinstance(obj, Resource)}

return resource


@view_config(context=Resource, permission="read", renderer="json")
Expand All @@ -11,4 +24,4 @@ def resource_view(context, request):
For this to work a custom JSON encoder is used to deal with the
various objects that aren't JSON encode-able out of the box.
"""
return context
return {k: flatten(obj) for k, obj in context.items() if isinstance(obj, Resource)}

0 comments on commit 905bcd5

Please sign in to comment.