PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/reversion/models.py

https://github.com/freyley/django-reversion
Python | 277 lines | 152 code | 46 blank | 79 comment | 22 complexity | 21689820cf7cad239616454b41e46cdf MD5 | raw file
  1. """Database models used by django-reversion."""
  2. import warnings
  3. from django.contrib.auth.models import User
  4. from django.contrib.contenttypes.models import ContentType
  5. from django.contrib.contenttypes import generic
  6. from django.core import serializers
  7. from django.conf import settings
  8. from django.db import models, IntegrityError
  9. from django.db.models import Count, Max
  10. from tools import recursive_revert
  11. def deprecated(original, replacement):
  12. """Decorator that defines a deprecated method."""
  13. def decorator(func):
  14. if not settings.DEBUG:
  15. return func
  16. def do_pending_deprication(*args, **kwargs):
  17. warnings.warn(
  18. "%s is deprecated, and will be removed in django-reversion 1.7. Use %s instead" % (original, replacement),
  19. PendingDeprecationWarning,
  20. )
  21. return func(*args, **kwargs)
  22. return do_pending_deprication
  23. return decorator
  24. class RevertError(Exception):
  25. """Exception thrown when something goes wrong with reverting a model."""
  26. class Revision(models.Model):
  27. """A group of related object versions."""
  28. manager_slug = models.CharField(
  29. max_length = 200,
  30. db_index = True,
  31. default = "default",
  32. )
  33. date_created = models.DateTimeField(auto_now_add=True,
  34. help_text="The date and time this revision was created.")
  35. user = models.ForeignKey(User,
  36. blank=True,
  37. null=True,
  38. help_text="The user who created this revision.")
  39. comment = models.TextField(blank=True,
  40. help_text="A text comment on this revision.")
  41. def revert(self, delete=False):
  42. """Reverts all objects in this revision.
  43. Result: all objects will be at the version created by this revision. If you are looking to undo this revision, you will need to write your own function.
  44. """
  45. version_set = self.version_set.all()
  46. # Optionally delete objects no longer in the current revision.
  47. if delete:
  48. # Get a dict of all objects in this revision.
  49. old_revision = {}
  50. for version in version_set:
  51. try:
  52. obj = version.object
  53. except ContentType.objects.get_for_id(version.content_type_id).model_class().DoesNotExist:
  54. pass
  55. else:
  56. old_revision[obj] = version
  57. # Calculate the set of all objects that are in the revision now.
  58. from reversion.revisions import RevisionManager
  59. current_revision = RevisionManager.get_manager(self.manager_slug)._follow_relationships(old_revision.keys())
  60. # Delete objects that are no longer in the current revision.
  61. for item in current_revision:
  62. if item in old_revision:
  63. if old_revision[item].type == VERSION_DELETE:
  64. item.delete()
  65. else:
  66. item.delete()
  67. # Attempt to revert all revisions.
  68. recursive_revert([version for version in version_set if version.type != VERSION_DELETE])
  69. def __unicode__(self):
  70. """Returns a unicode representation."""
  71. return u", ".join(unicode(version) for version in self.version_set.all())
  72. # Version types.
  73. VERSION_ADD = 0
  74. VERSION_CHANGE = 1
  75. VERSION_DELETE = 2
  76. VERSION_TYPE_CHOICES = (
  77. (VERSION_ADD, "Addition"),
  78. (VERSION_CHANGE, "Change"),
  79. (VERSION_DELETE, "Deletion"),
  80. )
  81. def has_int_pk(model):
  82. """Tests whether the given model has an integer primary key."""
  83. return (
  84. isinstance(model._meta.pk, (models.IntegerField, models.AutoField)) and
  85. not isinstance(model._meta.pk, models.BigIntegerField)
  86. )
  87. class VersionManager(models.Manager):
  88. """Manager for Version models."""
  89. @deprecated("Version.objects.get_for_object_reference()", "reversion.get_for_object_reference()")
  90. def get_for_object_reference(self, model, object_id):
  91. """
  92. Returns all versions for the given object reference.
  93. This method was deprecated in django-reversion 1.5, and will be removed in django-reversion 1.7.
  94. New applications should use reversion.get_for_object_reference(). The new version of this method
  95. returns results ordered with the most recent versions first. This legacy version of the method
  96. continues to return the results ordered with the oldest versions first.
  97. """
  98. from reversion.revisions import default_revision_manager
  99. return default_revision_manager.get_for_object_reference(model, object_id).order_by("pk")
  100. @deprecated("Version.objects.get_for_object()", "reversion.get_for_object()")
  101. def get_for_object(self, object):
  102. """
  103. Returns all the versions of the given object, ordered by date created.
  104. This method was deprecated in django-reversion 1.5, and will be removed in django-reversion 1.7.
  105. New applications should use reversion.get_for_object(). The new version of this method
  106. returns results ordered with the most recent versions first. This legacy version of the method
  107. continues to return the results ordered with the oldest versions first.
  108. """
  109. from reversion.revisions import default_revision_manager
  110. return default_revision_manager.get_for_object(object).order_by("pk")
  111. @deprecated("Version.objects.get_unique_for_object()", "reversion.get_unique_for_object()")
  112. def get_unique_for_object(self, obj):
  113. """
  114. Returns unique versions associated with the object.
  115. This method was deprecated in django-reversion 1.5, and will be removed in django-reversion 1.7.
  116. New applications should use reversion.get_unique_for_object(). The new version of this method
  117. returns results ordered with the most recent versions first. This legacy version of the method
  118. continues to return the results ordered with the oldest versions first.
  119. """
  120. from reversion.revisions import default_revision_manager
  121. versions = default_revision_manager.get_unique_for_object(obj)
  122. versions.reverse()
  123. return versions
  124. @deprecated("Version.objects.get_for_date()", "reversion.get_for_date()")
  125. def get_for_date(self, object, date):
  126. """
  127. Returns the latest version of an object for the given date.
  128. This method was deprecated in django-reversion 1.5, and will be removed in django-reversion 1.7.
  129. New applications should use reversion.get_for_date().
  130. """
  131. from reversion.revisions import default_revision_manager
  132. return default_revision_manager.get_for_date(object, date)
  133. @deprecated("Version.objects.get_deleted_object()", "reversion.get_for_object_reference()[0]")
  134. def get_deleted_object(self, model_class, object_id, select_related=None):
  135. """
  136. Returns the version corresponding to the deletion of the object with
  137. the given id.
  138. This method was deprecated in django-reversion 1.5, and will be removed in django-reversion 1.7.
  139. New applications should use reversion.get_for_date()[0].
  140. """
  141. from reversion.revisions import default_revision_manager
  142. return default_revision_manager.get_for_object_reference(model_class, object_id)[0]
  143. @deprecated("Version.objects.get_deleted()", "reversion.get_deleted()")
  144. def get_deleted(self, model_class, select_related=None):
  145. """
  146. Returns all the deleted versions for the given model class.
  147. This method was deprecated in django-reversion 1.5, and will be removed in django-reversion 1.7.
  148. New applications should use reversion.get_deleted(). The new version of this method
  149. returns results ordered with the most recent versions first. This legacy version of the method
  150. continues to return the results ordered with the oldest versions first.
  151. """
  152. from reversion.revisions import default_revision_manager
  153. return list(default_revision_manager.get_deleted(model_class).order_by("pk"))
  154. class Version(models.Model):
  155. """A saved version of a database model."""
  156. objects = VersionManager()
  157. revision = models.ForeignKey(Revision,
  158. help_text="The revision that contains this version.")
  159. object_id = models.TextField(help_text="Primary key of the model under version control.")
  160. object_id_int = models.IntegerField(
  161. blank = True,
  162. null = True,
  163. db_index = True,
  164. help_text = "An indexed, integer version of the stored model's primary key, used for faster lookups.",
  165. )
  166. content_type = models.ForeignKey(ContentType,
  167. help_text="Content type of the model under version control.")
  168. # A link to the current instance, not the version stored in this Version!
  169. object = generic.GenericForeignKey()
  170. format = models.CharField(max_length=255,
  171. help_text="The serialization format used by this model.")
  172. serialized_data = models.TextField(help_text="The serialized form of this version of the model.")
  173. object_repr = models.TextField(help_text="A string representation of the object.")
  174. @property
  175. def object_version(self):
  176. """The stored version of the model."""
  177. data = self.serialized_data
  178. if isinstance(data, unicode):
  179. data = data.encode("utf8")
  180. return list(serializers.deserialize(self.format, data))[0]
  181. type = models.PositiveSmallIntegerField(choices=VERSION_TYPE_CHOICES, db_index=True)
  182. @property
  183. def field_dict(self):
  184. """
  185. A dictionary mapping field names to field values in this version
  186. of the model.
  187. This method will follow parent links, if present.
  188. """
  189. if not hasattr(self, "_field_dict_cache"):
  190. object_version = self.object_version
  191. obj = object_version.object
  192. result = {}
  193. for field in obj._meta.fields:
  194. result[field.name] = field.value_from_object(obj)
  195. result.update(object_version.m2m_data)
  196. # Add parent data.
  197. for parent_class, field in obj._meta.parents.items():
  198. content_type = ContentType.objects.get_for_model(parent_class)
  199. if field:
  200. parent_id = unicode(getattr(obj, field.attname))
  201. else:
  202. parent_id = obj.pk
  203. try:
  204. parent_version = Version.objects.get(revision__id=self.revision_id,
  205. content_type=content_type,
  206. object_id=parent_id)
  207. except parent_class.DoesNotExist:
  208. pass
  209. else:
  210. result.update(parent_version.field_dict)
  211. setattr(self, "_field_dict_cache", result)
  212. return getattr(self, "_field_dict_cache")
  213. def revert(self):
  214. """Recovers the model in this version."""
  215. self.object_version.save()
  216. def __unicode__(self):
  217. """Returns a unicode representation."""
  218. return self.object_repr