/usr/lib/python3/dist-packages/reversion/revisions.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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | from __future__ import unicode_literals
from collections import namedtuple, defaultdict
from contextlib import contextmanager
from functools import wraps
from threading import local
from django.apps import apps
from django.core import serializers
from django.core.exceptions import ObjectDoesNotExist
from django.db import models, transaction, router
from django.db.models.query import QuerySet
from django.db.models.signals import post_save, m2m_changed
from django.utils.encoding import force_text
from django.utils import timezone, six
from reversion.compat import remote_field
from reversion.errors import RevisionManagementError, RegistrationError
from reversion.signals import pre_revision_commit, post_revision_commit
_VersionOptions = namedtuple("VersionOptions", (
"fields",
"follow",
"format",
"for_concrete_model",
"ignore_duplicates",
))
_StackFrame = namedtuple("StackFrame", (
"manage_manually",
"user",
"comment",
"date_created",
"db_versions",
"meta",
))
class _Local(local):
def __init__(self):
self.stack = ()
_local = _Local()
def is_active():
return bool(_local.stack)
def _current_frame():
if not is_active():
raise RevisionManagementError("There is no active revision for this thread")
return _local.stack[-1]
def _copy_db_versions(db_versions):
return {
db: versions.copy()
for db, versions
in db_versions.items()
}
def _push_frame(manage_manually, using):
if is_active():
current_frame = _current_frame()
db_versions = _copy_db_versions(current_frame.db_versions)
db_versions.setdefault(using, {})
stack_frame = current_frame._replace(
manage_manually=manage_manually,
db_versions=db_versions,
)
else:
stack_frame = _StackFrame(
manage_manually=manage_manually,
user=None,
comment="",
date_created=timezone.now(),
db_versions={using: {}},
meta=(),
)
_local.stack += (stack_frame,)
def _update_frame(**kwargs):
_local.stack = _local.stack[:-1] + (_current_frame()._replace(**kwargs),)
def _pop_frame():
prev_frame = _current_frame()
_local.stack = _local.stack[:-1]
if is_active():
current_frame = _current_frame()
db_versions = {
db: prev_frame.db_versions[db]
for db
in current_frame.db_versions.keys()
}
_update_frame(
user=prev_frame.user,
comment=prev_frame.comment,
date_created=prev_frame.date_created,
db_versions=db_versions,
meta=prev_frame.meta,
)
def is_manage_manually():
return _current_frame().manage_manually
def set_user(user):
_update_frame(user=user)
def get_user():
return _current_frame().user
def set_comment(comment):
_update_frame(comment=comment)
def get_comment():
return _current_frame().comment
def set_date_created(date_created):
_update_frame(date_created=date_created)
def get_date_created():
return _current_frame().date_created
def add_meta(model, **values):
_update_frame(meta=_current_frame().meta + ((model, values),))
def _follow_relations(obj):
version_options = _get_options(obj.__class__)
for follow_name in version_options.follow:
try:
follow_obj = getattr(obj, follow_name)
except ObjectDoesNotExist:
continue
if isinstance(follow_obj, models.Model):
yield follow_obj
elif isinstance(follow_obj, (models.Manager, QuerySet)):
for follow_obj_instance in follow_obj.all():
yield follow_obj_instance
elif follow_obj is not None:
raise RegistrationError("{name}.{follow_name} should be a Model or QuerySet".format(
name=obj.__class__.__name__,
follow_name=follow_name,
))
def _follow_relations_recursive(obj):
def do_follow(obj):
if obj not in relations:
relations.add(obj)
for related in _follow_relations(obj):
do_follow(related)
relations = set()
do_follow(obj)
return relations
def _add_to_revision(obj, using, model_db, explicit):
from reversion.models import Version
# Exit early if the object is not fully-formed.
if obj.pk is None:
return
version_options = _get_options(obj.__class__)
content_type = _get_content_type(obj.__class__, using)
object_id = force_text(obj.pk)
version_key = (content_type, object_id)
# If the obj is already in the revision, stop now.
db_versions = _current_frame().db_versions
versions = db_versions[using]
if version_key in versions and not explicit:
return
# Get the version data.
version = Version(
content_type=content_type,
object_id=object_id,
db=model_db,
format=version_options.format,
serialized_data=serializers.serialize(
version_options.format,
(obj,),
fields=version_options.fields,
),
object_repr=force_text(obj),
)
# If the version is a duplicate, stop now.
if version_options.ignore_duplicates and explicit:
previous_version = Version.objects.using(using).get_for_object(obj, model_db=model_db).first()
if previous_version and previous_version._local_field_dict == version._local_field_dict:
return
# Store the version.
db_versions = _copy_db_versions(db_versions)
db_versions[using][version_key] = version
_update_frame(db_versions=db_versions)
# Follow relations.
for follow_obj in _follow_relations(obj):
_add_to_revision(follow_obj, using, model_db, False)
def add_to_revision(obj, model_db=None):
model_db = model_db or router.db_for_write(obj.__class__, instance=obj)
for db in _current_frame().db_versions.keys():
_add_to_revision(obj, db, model_db, True)
def _save_revision(versions, user=None, comment="", meta=(), date_created=None, using=None):
from reversion.models import Revision
# Only save versions that exist in the database.
model_db_pks = defaultdict(lambda: defaultdict(set))
for version in versions:
model_db_pks[version._model][version.db].add(version.object_id)
model_db_existing_pks = {
model: {
db: frozenset(map(
force_text,
model._default_manager.using(db).filter(pk__in=pks).values_list("pk", flat=True),
))
for db, pks in db_pks.items()
}
for model, db_pks in model_db_pks.items()
}
versions = [
version for version in versions
if version.object_id in model_db_existing_pks[version._model][version.db]
]
# Bail early if there are no objects to save.
if not versions:
return
# Save a new revision.
revision = Revision(
date_created=date_created,
user=user,
comment=comment,
)
# Send the pre_revision_commit signal.
pre_revision_commit.send(
sender=create_revision,
revision=revision,
versions=versions,
)
# Save the revision.
revision.save(using=using)
# Save version models.
for version in versions:
version.revision = revision
version.save(using=using)
# Save the meta information.
for meta_model, meta_fields in meta:
meta_model._default_manager.db_manager(using=using).create(
revision=revision,
**meta_fields
)
# Send the post_revision_commit signal.
post_revision_commit.send(
sender=create_revision,
revision=revision,
versions=versions,
)
@contextmanager
def _dummy_context():
yield
@contextmanager
def _create_revision_context(manage_manually, using, atomic):
_push_frame(manage_manually, using)
try:
context = transaction.atomic(using=using) if atomic else _dummy_context()
with context:
yield
# Only save for a db if that's the last stack frame for that db.
if not any(using in frame.db_versions for frame in _local.stack[:-1]):
current_frame = _current_frame()
_save_revision(
versions=current_frame.db_versions[using].values(),
user=current_frame.user,
comment=current_frame.comment,
meta=current_frame.meta,
date_created=current_frame.date_created,
using=using,
)
finally:
_pop_frame()
def create_revision(manage_manually=False, using=None, atomic=True):
from reversion.models import Revision
using = using or router.db_for_write(Revision)
return _ContextWrapper(_create_revision_context, (manage_manually, using, atomic))
class _ContextWrapper(object):
def __init__(self, func, args):
self._func = func
self._args = args
self._context = func(*args)
def __enter__(self):
return self._context.__enter__()
def __exit__(self, exc_type, exc_value, traceback):
return self._context.__exit__(exc_type, exc_value, traceback)
def __call__(self, func):
@wraps(func)
def do_revision_context(*args, **kwargs):
with self._func(*self._args):
return func(*args, **kwargs)
return do_revision_context
def _post_save_receiver(sender, instance, using, **kwargs):
if is_registered(sender) and is_active() and not is_manage_manually():
add_to_revision(instance, model_db=using)
def _m2m_changed_receiver(instance, using, action, model, reverse, **kwargs):
if action.startswith("post_") and not reverse:
if is_registered(instance) and is_active() and not is_manage_manually():
add_to_revision(instance, model_db=using)
def _get_registration_key(model):
return (model._meta.app_label, model._meta.model_name)
_registered_models = {}
def is_registered(model):
return _get_registration_key(model) in _registered_models
def get_registered_models():
return (apps.get_model(*key) for key in _registered_models.keys())
def _get_senders_and_signals(model):
yield model, post_save, _post_save_receiver
opts = model._meta.concrete_model._meta
for field in opts.local_many_to_many:
m2m_model = remote_field(field).through
if isinstance(m2m_model, six.string_types):
if "." not in m2m_model:
m2m_model = "{app_label}.{m2m_model}".format(
app_label=opts.app_label,
m2m_model=m2m_model
)
yield m2m_model, m2m_changed, _m2m_changed_receiver
def register(model=None, fields=None, exclude=(), follow=(), format="json",
for_concrete_model=True, ignore_duplicates=False):
def register(model):
# Prevent multiple registration.
if is_registered(model):
raise RegistrationError("{model} has already been registered with django-reversion".format(
model=model,
))
# Parse fields.
opts = model._meta.concrete_model._meta
version_options = _VersionOptions(
fields=tuple(
field_name
for field_name
in ([
field.name
for field
in opts.local_fields + opts.local_many_to_many
] if fields is None else fields)
if field_name not in exclude
),
follow=tuple(follow),
format=format,
for_concrete_model=for_concrete_model,
ignore_duplicates=ignore_duplicates,
)
# Register the model.
_registered_models[_get_registration_key(model)] = version_options
# Connect signals.
for sender, signal, signal_receiver in _get_senders_and_signals(model):
signal.connect(signal_receiver, sender=sender)
# All done!
return model
# Return a class decorator if model is not given
if model is None:
return register
# Register the model.
return register(model)
def _assert_registered(model):
if not is_registered(model):
raise RegistrationError("{model} has not been registered with django-reversion".format(
model=model,
))
def _get_options(model):
_assert_registered(model)
return _registered_models[_get_registration_key(model)]
def unregister(model):
_assert_registered(model)
del _registered_models[_get_registration_key(model)]
# Disconnect signals.
for sender, signal, signal_receiver in _get_senders_and_signals(model):
signal.disconnect(signal_receiver, sender=sender)
def _get_content_type(model, using):
from django.contrib.contenttypes.models import ContentType
version_options = _get_options(model)
return ContentType.objects.db_manager(using).get_for_model(
model,
for_concrete_model=version_options.for_concrete_model,
)
|