From f22aeeb0a3e12f377b4523865b0c519f1d01f1b5 Mon Sep 17 00:00:00 2001 From: Ian Foote Date: Tue, 18 Feb 2014 12:08:12 +0000 Subject: [PATCH 1/2] Refactor UpdateModelMixin.update Reduce nesting, return early in error cases. --- rest_framework/mixins.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index 5fbcf700f..536303433 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -116,30 +116,27 @@ class UpdateModelMixin(object): partial = kwargs.pop('partial', False) self.object = self.get_object_or_none() - if self.object is None: - created = True - save_kwargs = {'force_insert': True} - success_status_code = status.HTTP_201_CREATED - else: - created = False - save_kwargs = {'force_update': True} - success_status_code = status.HTTP_200_OK - serializer = self.get_serializer(self.object, data=request.DATA, files=request.FILES, partial=partial) - if serializer.is_valid(): - try: - self.pre_save(serializer.object) - except ValidationError as err: - # full_clean on model instance may be called in pre_save, so we - # have to handle eventual errors. - return Response(err.message_dict, status=status.HTTP_400_BAD_REQUEST) - self.object = serializer.save(**save_kwargs) - self.post_save(self.object, created=created) - return Response(serializer.data, status=success_status_code) + if not serializer.is_valid(): + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + try: + self.pre_save(serializer.object) + except ValidationError as err: + # full_clean on model instance may be called in pre_save, so we + # have to handle eventual errors. + return Response(err.message_dict, status=status.HTTP_400_BAD_REQUEST) + + if self.object is None: + self.object = serializer.save(force_insert=True) + self.post_save(self.object, created=True) + return Response(serializer.data, status=status.HTTP_201_CREATED) + else: + self.object = serializer.save(force_update=True) + self.post_save(self.object, created=False) + return Response(serializer.data, status=status.HTTP_200_OK) def partial_update(self, request, *args, **kwargs): kwargs['partial'] = True From dca8b983568e1ca7534ad7244e6eb57e1b87cc68 Mon Sep 17 00:00:00 2001 From: Ian Foote Date: Tue, 18 Feb 2014 12:28:02 +0000 Subject: [PATCH 2/2] Remove unnecessary else --- rest_framework/mixins.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index 536303433..7722d5bed 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -133,10 +133,10 @@ class UpdateModelMixin(object): self.object = serializer.save(force_insert=True) self.post_save(self.object, created=True) return Response(serializer.data, status=status.HTTP_201_CREATED) - else: - self.object = serializer.save(force_update=True) - self.post_save(self.object, created=False) - return Response(serializer.data, status=status.HTTP_200_OK) + + self.object = serializer.save(force_update=True) + self.post_save(self.object, created=False) + return Response(serializer.data, status=status.HTTP_200_OK) def partial_update(self, request, *args, **kwargs): kwargs['partial'] = True