Implement SUPPORT_PATCH setting

This commit is contained in:
José Padilla 2015-06-26 10:03:17 -04:00
parent 3e1687282b
commit b428a6cafd
3 changed files with 22 additions and 2 deletions

View File

@ -54,6 +54,7 @@ DEFAULTS = {
# Generic view behavior
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'DEFAULT_FILTER_BACKENDS': (),
'SUPPORT_PATCH': True,
# Throttling
'DEFAULT_THROTTLE_RATES': {

View File

@ -129,7 +129,12 @@ class APIView(View):
"""
Wrap Django's private `_allowed_methods` interface in a public property.
"""
return self._allowed_methods()
allowed_methods = self._allowed_methods()
if not api_settings.SUPPORT_PATCH and 'PATCH' in allowed_methods:
allowed_methods.remove('PATCH')
return allowed_methods
@property
def default_response_headers(self):
@ -451,7 +456,7 @@ class APIView(View):
self.initial(request, *args, **kwargs)
# Get the appropriate handler method
if request.method.lower() in self.http_method_names:
if request.method in self.allowed_methods:
handler = getattr(self, request.method.lower(),
self.http_method_not_allowed)
else:

View File

@ -7,6 +7,7 @@ from django.test import TestCase
from django.utils import six
from rest_framework import generics, renderers, serializers, status
from rest_framework.settings import api_settings
from rest_framework.test import APIRequestFactory
from tests.models import (
BasicModel, ForeignKeySource, ForeignKeyTarget, RESTFrameworkModel
@ -213,6 +214,19 @@ class TestInstanceView(TestCase):
updated = self.objects.get(id=1)
self.assertEqual(updated.text, 'foobar')
def test_patch_instance_view_support_patch(self):
"""
PATCH requests with SUPPORT_PATCH=False should return 405
"""
data = {'text': 'foobar'}
request = factory.patch('/1', data, format='json')
api_settings.SUPPORT_PATCH = False
response = self.view(request, pk=1).render()
api_settings.SUPPORT_PATCH = True
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
def test_delete_instance_view(self):
"""
DELETE requests to RetrieveUpdateDestroyAPIView should delete an object.