Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Update tailbone #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions tailbone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,22 @@ def json_extras(obj):
return {"lat": obj.lat, "lon": obj.lon}
if isinstance(obj, ndb.Key):
r = webapp2.get_request()
if r.get("recurse", default_value=False):
current_level = [key for key in recurse_class.keys() if obj.kind() in recurse_class[key]] or 1
current_level = current_level[0] if current_level != 1 else 1
if r.get("recurse", default_value=False) and current_level <= recurse_depth:
item = obj.get()
if item == None:
if item is None:
return obj.urlsafe()
item = item.to_dict()
item["Id"] = obj.urlsafe()
item["$class"] = obj.kind()
recurse_class[current_level].add(item["$class"])
for key in item.keys():
if isinstance(item[key], ndb.Key) or (type(item[key]) == list and len(item[key]) > 0 and isinstance(item[key][0], ndb.Key)):
child_class = item[key].kind() if isinstance(item[key], ndb.Key) else item[key][0].kind()
if not current_level+1 in recurse_class.keys():
recurse_class.update({current_level+1: set()})
recurse_class[current_level+1].add(child_class)
return item
return obj.urlsafe()
return None
Expand All @@ -111,6 +121,9 @@ def as_json(func):
"""Returns json when callback in url"""
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
global recurse_excute, recurse_depth
recurse_depth = int(self.request.get("depth", default_value=2))
recurse_class = {1: set()}
self.response.headers["Content-Type"] = "application/json"
if DEBUG:
self.response.headers["Access-Control-Allow-Origin"] = "*"
Expand Down