custom metadata for GET options
This commit is contained in:
parent
3b148551db
commit
87e0236ec1
33
apps/api/metadata.py
Normal file
33
apps/api/metadata.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from rest_framework.metadata import SimpleMetadata
|
||||||
|
from rest_framework.request import clone_request
|
||||||
|
|
||||||
|
|
||||||
|
class ExpandedMetadata(SimpleMetadata):
|
||||||
|
|
||||||
|
def determine_actions(self, request, view):
|
||||||
|
"""
|
||||||
|
For generic class based views we return information about
|
||||||
|
the fields that are accepted for 'PUT' and 'POST' methods.
|
||||||
|
"""
|
||||||
|
actions = {}
|
||||||
|
for method in {'PUT', 'POST', "GET"} & set(view.allowed_methods):
|
||||||
|
view.request = clone_request(request, method)
|
||||||
|
try:
|
||||||
|
# Test global permissions
|
||||||
|
if hasattr(view, 'check_permissions'):
|
||||||
|
view.check_permissions(view.request)
|
||||||
|
# Test object permissions
|
||||||
|
if method == 'PUT' and hasattr(view, 'get_object'):
|
||||||
|
view.get_object()
|
||||||
|
except (exceptions.APIException, PermissionDenied, Http404):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
# If user has appropriate permissions for the view, include
|
||||||
|
# appropriate metadata about the fields that should be supplied.
|
||||||
|
serializer = view.get_serializer()
|
||||||
|
actions[method] = self.get_serializer_info(serializer)
|
||||||
|
finally:
|
||||||
|
view.request = request
|
||||||
|
|
||||||
|
return actions
|
Loading…
x
Reference in New Issue
Block a user