mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 06:14:47 +03:00
Merge e52df6b549
into 6634ce0da5
This commit is contained in:
commit
85ec9a8bdf
|
@ -74,19 +74,23 @@ class SimpleMetadata(BaseMetadata):
|
|||
actions = {}
|
||||
for method in set(['PUT', 'POST']) & set(view.allowed_methods):
|
||||
view.request = clone_request(request, method)
|
||||
instance = None
|
||||
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()
|
||||
instance = 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()
|
||||
kwargs = {}
|
||||
if instance is not None:
|
||||
kwargs['instance'] = instance
|
||||
serializer = view.get_serializer(**kwargs)
|
||||
actions[method] = self.get_serializer_info(serializer)
|
||||
finally:
|
||||
view.request = request
|
||||
|
|
|
@ -212,3 +212,27 @@ class TestMetadata:
|
|||
options = metadata.SimpleMetadata()
|
||||
field_info = options.get_field_info(serializers.NullBooleanField())
|
||||
assert field_info['type'] == 'boolean'
|
||||
|
||||
def test_instance_passed_to_serializer(self):
|
||||
dummy_obj = object()
|
||||
|
||||
class ExampleSerializer(serializers.Serializer):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(ExampleSerializer, self).__init__(*args, **kwargs)
|
||||
assert self.instance == dummy_obj
|
||||
|
||||
class ExampleView(views.APIView):
|
||||
"""Example view."""
|
||||
def put(self, request):
|
||||
pass
|
||||
|
||||
def get_object(self):
|
||||
return dummy_obj
|
||||
|
||||
def get_serializer(self, *args, **kwargs):
|
||||
assert kwargs['instance'] == self.get_object()
|
||||
return ExampleSerializer(*args, **kwargs)
|
||||
|
||||
view = ExampleView.as_view()
|
||||
response = view(request=request)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
|
Loading…
Reference in New Issue
Block a user