Skip to content

Commit f589c74

Browse files
committed
Move get_encoding to descriptor.
1 parent ceea9ab commit f589c74

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

rest_framework/schemas.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,8 @@ def get_link(self, path, method, generator):
282282
fields += self.get_pagination_fields(path, method)
283283
fields += self.get_filter_fields(path, method)
284284

285-
# TEMP: now we proxy back to the generator
286-
287285
if fields and any([field.location in ('form', 'body') for field in fields]):
288-
encoding = generator.get_encoding(path, method, view)
286+
encoding = self.get_encoding(path, method)
289287
else:
290288
encoding = None
291289

@@ -456,6 +454,29 @@ def get_filter_fields(self, path, method):
456454
fields += filter_backend().get_schema_fields(view)
457455
return fields
458456

457+
def get_encoding(self, path, method):
458+
"""
459+
Return the 'encoding' parameter to use for a given endpoint.
460+
"""
461+
view = self.view
462+
463+
# Core API supports the following request encodings over HTTP...
464+
supported_media_types = set((
465+
'application/json',
466+
'application/x-www-form-urlencoded',
467+
'multipart/form-data',
468+
))
469+
parser_classes = getattr(view, 'parser_classes', [])
470+
for parser_class in parser_classes:
471+
media_type = getattr(parser_class, 'media_type', None)
472+
if media_type in supported_media_types:
473+
return media_type
474+
# Raw binary uploads are supported with "application/octet-stream"
475+
if media_type == '*/*':
476+
return 'application/octet-stream'
477+
478+
return None
479+
459480
# TODO: Where should this live?
460481
# - We import APIView here. So we can't import the descriptor into `views`
461482
# - APIView is only used by SchemaView.
@@ -640,29 +661,6 @@ def coerce_path(self, path, method, view):
640661
field_name = 'id'
641662
return path.replace('{pk}', '{%s}' % field_name)
642663

643-
# Methods for generating each individual `Link` instance...
644-
645-
def get_encoding(self, path, method, view):
646-
"""
647-
Return the 'encoding' parameter to use for a given endpoint.
648-
"""
649-
# Core API supports the following request encodings over HTTP...
650-
supported_media_types = set((
651-
'application/json',
652-
'application/x-www-form-urlencoded',
653-
'multipart/form-data',
654-
))
655-
parser_classes = getattr(view, 'parser_classes', [])
656-
for parser_class in parser_classes:
657-
media_type = getattr(parser_class, 'media_type', None)
658-
if media_type in supported_media_types:
659-
return media_type
660-
# Raw binary uploads are supported with "application/octet-stream"
661-
if media_type == '*/*':
662-
return 'application/octet-stream'
663-
664-
return None
665-
666664
# Method for generating the link layout....
667665

668666
def get_keys(self, subpath, method, view):

0 commit comments

Comments
 (0)