diff options
| author | Tom Christie | 2013-09-05 12:24:17 -0700 | 
|---|---|---|
| committer | Tom Christie | 2013-09-05 12:24:17 -0700 | 
| commit | 196a895fe4835ecefe9fbb30133cf842336a9d8e (patch) | |
| tree | 9b60b21404534a99b485c5fc6a2add4684ebc631 | |
| parent | 551fe92078698005fd5a35cc14cef35baf1d2d71 (diff) | |
| parent | 6e7e4fc01c5ddaf668f17f1d1f201a14a26f72f3 (diff) | |
| download | django-rest-framework-196a895fe4835ecefe9fbb30133cf842336a9d8e.tar.bz2 | |
Merge pull request #1076 from edmondwong/master
Allow OPTIONS to retrieve PUT field metadata on empty objects
| -rw-r--r-- | rest_framework/generics.py | 11 | ||||
| -rw-r--r-- | rest_framework/tests/test_generics.py | 42 | 
2 files changed, 51 insertions, 2 deletions
| diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 14feed20..7d1bf794 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -356,8 +356,15 @@ class GenericAPIView(views.APIView):                  self.check_permissions(cloned_request)                  # Test object permissions                  if method == 'PUT': -                    self.get_object() -            except (exceptions.APIException, PermissionDenied, Http404): +                    try: +                        self.get_object() +                    except Http404: +                        # Http404 should be acceptable and the serializer +                        # metadata should be populated. Except this so the +                        # outer "else" clause of the try-except-else block +                        # will be executed. +                        pass +            except (exceptions.APIException, PermissionDenied):                  pass              else:                  # If user has appropriate permissions for the view, include diff --git a/rest_framework/tests/test_generics.py b/rest_framework/tests/test_generics.py index 7a87d389..79cd99ac 100644 --- a/rest_framework/tests/test_generics.py +++ b/rest_framework/tests/test_generics.py @@ -272,6 +272,48 @@ class TestInstanceView(TestCase):          self.assertEqual(response.status_code, status.HTTP_200_OK)          self.assertEqual(response.data, expected) +    def test_options_before_instance_create(self): +        """ +        OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata +        before the instance has been created +        """ +        request = factory.options('/999') +        with self.assertNumQueries(1): +            response = self.view(request, pk=999).render() +        expected = { +            'parses': [ +                'application/json', +                'application/x-www-form-urlencoded', +                'multipart/form-data' +            ], +            'renders': [ +                'application/json', +                'text/html' +            ], +            'name': 'Instance', +            'description': 'Example description for OPTIONS.', +            'actions': { +                'PUT': { +                    'text': { +                        'max_length': 100, +                        'read_only': False, +                        'required': True, +                        'type': 'string', +                        'label': 'Text comes here', +                        'help_text': 'Text description.' +                    }, +                    'id': { +                        'read_only': True, +                        'required': False, +                        'type': 'integer', +                        'label': 'ID', +                    }, +                } +            } +        } +        self.assertEqual(response.status_code, status.HTTP_200_OK) +        self.assertEqual(response.data, expected) +      def test_get_instance_view_incorrect_arg(self):          """          GET requests with an incorrect pk type, should raise 404, not 500. | 
