/usr/share/pyshared/reversion/models.py is in python-django-reversion 1.8-2.
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 | """Database models used by django-reversion."""
from __future__ import unicode_literals
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.conf import settings
from django.core import serializers
from django.core.exceptions import ObjectDoesNotExist
from django.db import models, IntegrityError
from django.dispatch.dispatcher import Signal
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import force_text, python_2_unicode_compatible
def safe_revert(versions):
"""
Attempts to revert the given models contained in the give versions.
This method will attempt to resolve dependencies between the versions to revert
them in the correct order to avoid database integrity errors.
"""
unreverted_versions = []
for version in versions:
try:
version.revert()
except (IntegrityError, ObjectDoesNotExist):
unreverted_versions.append(version)
if len(unreverted_versions) == len(versions):
raise RevertError("Could not revert revision, due to database integrity errors.")
if unreverted_versions:
safe_revert(unreverted_versions)
class RevertError(Exception):
"""Exception thrown when something goes wrong with reverting a model."""
UserModel = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
@python_2_unicode_compatible
class Revision(models.Model):
"""A group of related object versions."""
manager_slug = models.CharField(
max_length = 200,
db_index = True,
default = "default",
)
date_created = models.DateTimeField(auto_now_add=True,
verbose_name=_("date created"),
help_text="The date and time this revision was created.")
user = models.ForeignKey(UserModel,
blank=True,
null=True,
verbose_name=_("user"),
help_text="The user who created this revision.")
comment = models.TextField(blank=True,
verbose_name=_("comment"),
help_text="A text comment on this revision.")
def revert(self, delete=False):
"""Reverts all objects in this revision."""
version_set = self.version_set.all()
# Optionally delete objects no longer in the current revision.
if delete:
# Get a dict of all objects in this revision.
old_revision = {}
for version in version_set:
try:
obj = version.object
except ContentType.objects.get_for_id(version.content_type_id).model_class().DoesNotExist:
pass
else:
old_revision[obj] = version
# Calculate the set of all objects that are in the revision now.
from reversion.revisions import RevisionManager
current_revision = RevisionManager.get_manager(self.manager_slug)._follow_relationships(obj for obj in old_revision.keys() if obj is not None)
# Delete objects that are no longer in the current revision.
for item in current_revision:
if item not in old_revision:
item.delete()
# Attempt to revert all revisions.
safe_revert(version_set)
def __str__(self):
"""Returns a unicode representation."""
return ", ".join(force_text(version) for version in self.version_set.all())
def has_int_pk(model):
"""Tests whether the given model has an integer primary key."""
pk = model._meta.pk
return (
(
isinstance(pk, (models.IntegerField, models.AutoField)) and
not isinstance(pk, models.BigIntegerField)
) or (
isinstance(pk, models.ForeignKey) and has_int_pk(pk.rel.to)
)
)
@python_2_unicode_compatible
class Version(models.Model):
"""A saved version of a database model."""
revision = models.ForeignKey(Revision,
help_text="The revision that contains this version.")
object_id = models.TextField(help_text="Primary key of the model under version control.")
object_id_int = models.IntegerField(
blank = True,
null = True,
db_index = True,
help_text = "An indexed, integer version of the stored model's primary key, used for faster lookups.",
)
content_type = models.ForeignKey(ContentType,
help_text="Content type of the model under version control.")
# A link to the current instance, not the version stored in this Version!
object = generic.GenericForeignKey()
format = models.CharField(max_length=255,
help_text="The serialization format used by this model.")
serialized_data = models.TextField(help_text="The serialized form of this version of the model.")
object_repr = models.TextField(help_text="A string representation of the object.")
@property
def object_version(self):
"""The stored version of the model."""
data = self.serialized_data
data = force_text(data.encode("utf8"))
return list(serializers.deserialize(self.format, data, ignorenonexistent=True))[0]
@property
def field_dict(self):
"""
A dictionary mapping field names to field values in this version
of the model.
This method will follow parent links, if present.
"""
if not hasattr(self, "_field_dict_cache"):
object_version = self.object_version
obj = object_version.object
result = {}
for field in obj._meta.fields:
result[field.name] = field.value_from_object(obj)
result.update(object_version.m2m_data)
# Add parent data.
for parent_class, field in obj._meta.parents.items():
content_type = ContentType.objects.get_for_model(parent_class)
if field:
parent_id = force_text(getattr(obj, field.attname))
else:
parent_id = obj.pk
try:
parent_version = Version.objects.get(revision__id=self.revision_id,
content_type=content_type,
object_id=parent_id)
except Version.DoesNotExist:
pass
else:
result.update(parent_version.field_dict)
setattr(self, "_field_dict_cache", result)
return getattr(self, "_field_dict_cache")
def revert(self):
"""Recovers the model in this version."""
self.object_version.save()
def __str__(self):
"""Returns a unicode representation."""
return self.object_repr
# Version management signals.
pre_revision_commit = Signal(providing_args=["instances", "revision", "versions"])
post_revision_commit = Signal(providing_args=["instances", "revision", "versions"])
|