mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
* Add pointer from API docs to tutorial #650 * Sorting by clicking table headers should use local-compare #356 * Mark IXP peering LAN as bogon #352 * Add help text to "Add (Facility, Network, Exchange)" tab #669 * Add Looking Glass field to the IX object #672 * Add read-only Superuser #679 * Make spelling of traffic levels consistent #519 (#723) * Offer 2FA (#290) * Show "Last Updated" fields on fac, ix, org records (#526) * Enable sort and reverse sort of IP column in IX display (#72) * IRR validation not handling unexpected characters gracefully (#712) * Support alternative direction of writing, e.g. Arabic (#618) * Undeleting an ixlan with an emtpy IPv4 XOR IPv6 field throws a silly error (#644) * Changing org while adding net results in 500 #654 * missing delete button for organisations (#121) * When changing owner of an ix admin GUI borks because of "Ixlan for exchange already exists" #666 * Selection should only present undeleted objects (#664) * change default encoding of API calls to 'utf-8' #663 * Posting https://www.peeringdb.com onto social media doesn't select a good preview image #537 * Revert "Add Looking Glass field to the IX object #672" This reverts commit 4daf2520043c241fabe9a521757efa86a274e28a. Conflicts: peeringdb_server/migrations/0037_ix_looking_glass.py peeringdb_server/views.py * 500 Internal Error when creating IX where prefix already exists elsewhere #718 * Fix graceful restore of soft-deleted objects with translation active (#580) * Don't return any POC data with status=deleted #569 Hard delete soft-deleted pocs after grace period #566 * django-peeringdb from github@2.0.0.2-beta Co-authored-by: Stefan Pratter <stefan@20c.com>
99 lines
2.7 KiB
Python
99 lines
2.7 KiB
Python
###############################################################################
|
|
# RENDERERS
|
|
|
|
from rest_framework import renderers
|
|
from rest_framework.utils import encoders
|
|
import json
|
|
|
|
|
|
class JSONEncoder(encoders.JSONEncoder):
|
|
"""
|
|
Im defining our own json encoder here in order to be able to encode
|
|
datatime and django countryfields.
|
|
|
|
Im making the munge renderer use this encoder to encode json, this approach
|
|
may need to be tidied up a bit.
|
|
"""
|
|
|
|
def default(self, obj):
|
|
"""Default JSON serializer."""
|
|
import datetime
|
|
import django_countries.fields
|
|
|
|
if isinstance(obj, datetime.datetime):
|
|
return obj.isoformat()
|
|
|
|
if isinstance(obj, django_countries.fields.Country):
|
|
return str(obj)
|
|
|
|
return encoders.JSONEncoder.default(self, obj)
|
|
|
|
|
|
class MungeRenderer(renderers.BaseRenderer):
|
|
media_type = "text/plain"
|
|
format = "txt"
|
|
charset = "utf-8"
|
|
|
|
def render(self, data, media_type=None, renderer_context=None):
|
|
# TODO use munge:
|
|
indent = None
|
|
if "request" in renderer_context:
|
|
request = renderer_context.get("request")
|
|
if "pretty" in request.GET:
|
|
indent = 2
|
|
return json.dumps(data, cls=JSONEncoder, indent=indent)
|
|
|
|
|
|
class MetaJSONRenderer(MungeRenderer):
|
|
"""
|
|
Renderer which serializes to JSON.
|
|
Does *not* apply JSON's character escaping for non-ascii characters.
|
|
"""
|
|
|
|
ensure_ascii = False
|
|
|
|
media_type = "application/json"
|
|
format = "json"
|
|
|
|
def render(self, data, accepted_media_type=None, renderer_context=None):
|
|
"""
|
|
Tweak output rendering and pass to parent
|
|
"""
|
|
|
|
if data is None:
|
|
return bytes()
|
|
|
|
result = {}
|
|
|
|
if "__meta" in data:
|
|
meta = data.pop("__meta")
|
|
else:
|
|
meta = dict()
|
|
|
|
if "request" in renderer_context:
|
|
request = renderer_context.get("request")
|
|
meta.update(getattr(request, "meta_response", {}))
|
|
|
|
res = renderer_context["response"]
|
|
if res.status_code < 400:
|
|
if "results" in data:
|
|
result["data"] = data.pop("results")
|
|
elif data:
|
|
if isinstance(data, dict):
|
|
result["data"] = [data]
|
|
else:
|
|
result["data"] = [r for r in data if r is not None]
|
|
else:
|
|
result["data"] = []
|
|
|
|
elif res.status_code < 500:
|
|
meta["error"] = data.pop("detail", res.reason_phrase)
|
|
|
|
result.update(**data)
|
|
|
|
result["meta"] = meta
|
|
|
|
return super(self.__class__, self).render(
|
|
result, accepted_media_type, renderer_context
|
|
)
|