mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-29 17:39:48 +03:00
Tidy up etag precondition checks
This commit is contained in:
parent
bceca2364a
commit
cfd083d759
|
@ -8,6 +8,7 @@ from __future__ import unicode_literals
|
|||
|
||||
from django.http import Http404
|
||||
from rest_framework import status
|
||||
from rest_framework.exceptions import PreconditionFailed
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.request import clone_request
|
||||
|
||||
|
@ -114,10 +115,6 @@ class UpdateModelMixin(object):
|
|||
Should be mixed in with `SingleObjectAPIView`.
|
||||
"""
|
||||
def update(self, request, *args, **kwargs):
|
||||
header_etag = request.META.get('HTTP_IF_MATCH')
|
||||
if header_etag is None:
|
||||
return Response({'error': 'IF_MATCH header is required'}, status=400)
|
||||
|
||||
partial = kwargs.pop('partial', False)
|
||||
self.object = None
|
||||
try:
|
||||
|
@ -129,8 +126,8 @@ class UpdateModelMixin(object):
|
|||
created = True
|
||||
success_status_code = status.HTTP_201_CREATED
|
||||
else:
|
||||
if self.object.etag != header_etag:
|
||||
return Response({'error': 'object has been updated since you last saw it'}, status=412)
|
||||
if getattr(self, 'use_etags', False) and self.object.etag != self.etag_header:
|
||||
raise PreconditionFailed
|
||||
created = False
|
||||
success_status_code = status.HTTP_200_OK
|
||||
|
||||
|
@ -174,7 +171,7 @@ class DestroyModelMixin(object):
|
|||
"""
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
obj = self.get_object()
|
||||
if self.get_etag(obj) != self.header_etag:
|
||||
return Response({'error': 'object has been updated since you last saw it'}, status=412)
|
||||
if self.get_etag(obj) != self.etag_header:
|
||||
raise PreconditionFailed
|
||||
obj.delete()
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
|
Loading…
Reference in New Issue
Block a user