1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

dcim: added json flat renderer.

This commit is contained in:
Zach Moody
2016-03-07 23:57:49 -06:00
parent 8baa7ee410
commit 8f5543fe88
3 changed files with 73 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import json
from rest_framework import renderers
# IP address family designations
AF = {
4: 'A',
@ -29,3 +29,23 @@ class BINDZoneRenderer(renderers.BaseRenderer):
except KeyError:
pass
return '\n'.join(records)
class FlatJSONRenderer(renderers.BaseRenderer):
"""
Flattens a nested JSON reponse.
"""
format = 'json_flat'
media_type = 'application/json'
def render(self, data, media_type=None, renderer_context=None):
def flatten(entry):
for key, val in entry.iteritems():
if isinstance(val, dict):
for child_key, child_val in flatten(val):
yield "{}_{}".format(key, child_key), child_val
else:
yield key, val
return json.dumps([dict(flatten(i)) for i in data])