From b807f3d52a68dbf657c6437f71ecbfcba0695972 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 2 Jan 2013 13:39:24 +0000 Subject: [PATCH] Keep API backwards compatible. --- rest_framework/generics.py | 16 ++++++++++------ rest_framework/mixins.py | 21 +++++++++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 14e4430e3..3a38fab4f 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -47,14 +47,16 @@ class GenericAPIView(views.APIView): return serializer_class - def get_serializer(self, instance=None, data=None, files=None, partial=False): + def get_serializer(self, instance=None, data=None, + files=None, partial=False): """ Return the serializer instance that should be used for validating and deserializing input, and for serializing output. """ serializer_class = self.get_serializer_class() context = self.get_serializer_context() - return serializer_class(instance, data=data, files=files, partial=partial, context=context) + return serializer_class(instance, data=data, files=files, + partial=partial, context=context) class MultipleObjectAPIView(MultipleObjectMixin, GenericAPIView): @@ -169,10 +171,11 @@ class UpdateAPIView(mixins.UpdateModelMixin, Concrete view for updating a model instance. """ def put(self, request, *args, **kwargs): - return self.update(request, partial=False, *args, **kwargs) + return self.update(request, *args, **kwargs) def patch(self, request, *args, **kwargs): - return self.update(request, partial=True, *args, **kwargs) + kwargs['partial'] = True + return self.update(request, *args, **kwargs) class ListCreateAPIView(mixins.ListModelMixin, @@ -212,10 +215,11 @@ class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin, return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs): - return self.update(request, partial=False, *args, **kwargs) + return self.update(request, *args, **kwargs) def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs) def patch(self, request, *args, **kwargs): - return self.update(request, partial=True, *args, **kwargs) + kwargs['partial'] = True + return self.update(request, *args, **kwargs) diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index d828078d7..43581ae9b 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -16,11 +16,14 @@ class CreateModelMixin(object): """ def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.DATA, files=request.FILES) + if serializer.is_valid(): self.pre_save(serializer.object) self.object = serializer.save() headers = self.get_success_headers(serializer.data) - return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) + return Response(serializer.data, status=status.HTTP_201_CREATED, + headers=headers) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def get_success_headers(self, data): @@ -81,21 +84,22 @@ class UpdateModelMixin(object): Update a model instance. Should be mixed in with `SingleObjectBaseView`. """ - def update(self, request, partial=False, *args, **kwargs): + def update(self, request, *args, **kwargs): + partial = kwargs.pop('partial', False) try: self.object = self.get_object() - created = False + success_status_code = status.HTTP_200_OK except Http404: self.object = None - created = True + success_status_code = status.HTTP_201_CREATED - serializer = self.get_serializer(self.object, data=request.DATA, files=request.FILES, partial=partial) + serializer = self.get_serializer(self.object, data=request.DATA, + files=request.FILES, partial=partial) if serializer.is_valid(): self.pre_save(serializer.object) self.object = serializer.save() - status_code = created and status.HTTP_201_CREATED or status.HTTP_200_OK - return Response(serializer.data, status=status_code) + return Response(serializer.data, status=success_status_code) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @@ -115,7 +119,8 @@ class UpdateModelMixin(object): # Ensure we clean the attributes so that we don't eg return integer # pk using a string representation, as provided by the url conf kwarg. - obj.full_clean() + if hasattr(obj, 'full_clean'): + obj.full_clean() class DestroyModelMixin(object):