/usr/lib/python3/dist-packages/reversion/views.py is in python3-django-reversion 2.0.13-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | from functools import wraps
from reversion.compat import is_authenticated
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")
def _set_user_from_request(request):
if getattr(request, "user", None) and is_authenticated(request.user) and get_user() is None:
set_user(request.user)
def create_revision(manage_manually=False, using=None, atomic=True):
"""
View decorator that wraps the request in a revision.
The revision will have it's user set from the request automatically.
"""
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
return func(request, *args, **kwargs)
return do_revision_view
return decorator
class RevisionMixin(object):
"""
A class-based view mixin that wraps the request in a revision.
The revision will have it's user set from the request automatically.
"""
revision_manage_manually = False
revision_using = None
revision_atomic = True
def __init__(self, *args, **kwargs):
super(RevisionMixin, self).__init__(*args, **kwargs)
self.dispatch = create_revision(
manage_manually=self.revision_manage_manually,
using=self.revision_using,
atomic=self.revision_atomic
)(self.dispatch)
|