Skip to content

Commit

Permalink
Merge pull request #863 from etianen/postgres-rollback
Browse files Browse the repository at this point in the history
Not rolling back revision on 400 status code
  • Loading branch information
etianen committed Mar 20, 2021
2 parents a284fab + 406f721 commit 8ac3b83
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
django-reversion changelog
==========================

4.0.0 - IN DEVELOPMENT
----------------------

- **Breaking:** The ``create_revision`` view decorator and ``RevisionMiddleware`` no longer roll back the revision and
database transaction on response status code >= 400. It's the responsibility of the view to use `transaction.atomic()`
to roll back any invalid data. This can be enabled globally by setting ATOMIC_REQUESTS=True.

https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-DATABASE-ATOMIC_REQUESTS


3.0.9 - 2021-01-22
------------------

Expand Down
7 changes: 6 additions & 1 deletion reversion/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
from reversion.errors import RevertError
from reversion.models import Version
from reversion.revisions import is_active, register, is_registered, set_comment, create_revision, set_user
from reversion.views import _RollBackRevisionView


class _RollBackRevisionView(Exception):

def __init__(self, response):
self.response = response


class VersionAdmin(admin.ModelAdmin):
Expand Down
22 changes: 5 additions & 17 deletions reversion/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
from reversion.revisions import create_revision as create_revision_base, set_user, get_user


class _RollBackRevisionView(Exception):

def __init__(self, response):
self.response = response


def _request_creates_revision(request):
return request.method not in ("OPTIONS", "GET", "HEAD")

Expand All @@ -30,17 +24,11 @@ def decorator(func):
@wraps(func)
def do_revision_view(request, *args, **kwargs):
if request_creates_revision(request):
try:
with create_revision_base(manage_manually=manage_manually, using=using, atomic=atomic):
response = func(request, *args, **kwargs)
# Check for an error response.
if response.status_code >= 400:
raise _RollBackRevisionView(response)
# Otherwise, we're good.
_set_user_from_request(request)
return response
except _RollBackRevisionView as ex:
return ex.response
with create_revision_base(manage_manually=manage_manually, using=using, atomic=atomic):
response = func(request, *args, **kwargs)
# Otherwise, we're good.
_set_user_from_request(request)
return response
return func(request, *args, **kwargs)
return do_revision_view
return decorator
Expand Down
6 changes: 4 additions & 2 deletions tests/test_app/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db import transaction
from django.http import HttpResponse
from django.views.generic.base import View
from reversion.views import create_revision, RevisionMixin
Expand All @@ -9,8 +10,9 @@ def save_obj_view(request):


def save_obj_error_view(request):
TestModel.objects.create()
raise Exception("Boom!")
with transaction.atomic():
TestModel.objects.create()
raise Exception("Boom!")


@create_revision()
Expand Down

0 comments on commit 8ac3b83

Please sign in to comment.