Skip to content

Commit

Permalink
support .geojson with Accept: application/json
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Nov 20, 2020
1 parent fa8fa59 commit 0f35c54
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@

'DEFAULT_FILTER_BACKENDS': (
'wq.db.rest.filters.FilterBackend',
)
),

'DEFAULT_CONTENT_NEGOTIATION_CLASS':
'wq.db.rest.negotiation.ContentNegotiation'
}

# wq.db settings
Expand Down
21 changes: 21 additions & 0 deletions rest/negotiation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from rest_framework.negotiation import DefaultContentNegotiation


JSON = 'application/json'
GEOJSON = 'application/geo+json'


class ContentNegotiation(DefaultContentNegotiation):
"""
Some clients (such as Mapbox GL) might only Accept: application/json
"""
def filter_renderers(self, renderers, format):
self._format = format
return super().filter_renderers(renderers, format)

def get_accept_list(self, request):
accepts = super().get_accept_list(request)
if getattr(self, '_format', None) == 'geojson':
if JSON in accepts and GEOJSON not in accepts:
accepts.append(GEOJSON)
return accepts

0 comments on commit 0f35c54

Please sign in to comment.