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 django.http import Http404
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
from rest_framework.exceptions import PreconditionFailed
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.request import clone_request
|
from rest_framework.request import clone_request
|
||||||
|
|
||||||
|
@ -114,10 +115,6 @@ class UpdateModelMixin(object):
|
||||||
Should be mixed in with `SingleObjectAPIView`.
|
Should be mixed in with `SingleObjectAPIView`.
|
||||||
"""
|
"""
|
||||||
def update(self, request, *args, **kwargs):
|
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)
|
partial = kwargs.pop('partial', False)
|
||||||
self.object = None
|
self.object = None
|
||||||
try:
|
try:
|
||||||
|
@ -129,8 +126,8 @@ class UpdateModelMixin(object):
|
||||||
created = True
|
created = True
|
||||||
success_status_code = status.HTTP_201_CREATED
|
success_status_code = status.HTTP_201_CREATED
|
||||||
else:
|
else:
|
||||||
if self.object.etag != header_etag:
|
if getattr(self, 'use_etags', False) and self.object.etag != self.etag_header:
|
||||||
return Response({'error': 'object has been updated since you last saw it'}, status=412)
|
raise PreconditionFailed
|
||||||
created = False
|
created = False
|
||||||
success_status_code = status.HTTP_200_OK
|
success_status_code = status.HTTP_200_OK
|
||||||
|
|
||||||
|
@ -174,7 +171,7 @@ class DestroyModelMixin(object):
|
||||||
"""
|
"""
|
||||||
def destroy(self, request, *args, **kwargs):
|
def destroy(self, request, *args, **kwargs):
|
||||||
obj = self.get_object()
|
obj = self.get_object()
|
||||||
if self.get_etag(obj) != self.header_etag:
|
if self.get_etag(obj) != self.etag_header:
|
||||||
return Response({'error': 'object has been updated since you last saw it'}, status=412)
|
raise PreconditionFailed
|
||||||
obj.delete()
|
obj.delete()
|
||||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user