Skip to main content

Django reusable app for revision-based undo functionality with django-simple-history

Project description

django-undo-revision

A Django reusable app for revision-based undo functionality, built on top of django-simple-history.

How it works

Every mutation is wrapped in a revision — a unit that can be rolled back atomically with a single undo_last_revision(project) call. A revision automatically groups all historical records (creates, updates, deletes) made within its context.

Installation

pip install django-undo-revision

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "django.contrib.contenttypes",
    "simple_history",
    "undo_revision",
]

Run migrations:

python manage.py migrate

Configuration

In settings.py, set the project model (the entity revisions are scoped to):

UNDO_REVISION_PROJECT_MODEL = "myapp.Project"  # required

# URL kwarg used by UndoRevisionMixin to extract the project id
UNDO_REVISION_PROJECT_URL_KWARG = "project_id"

# HTTP methods that should open a revision (default: all mutating methods)
UNDO_REVISION_HTTP_METHODS = ["post", "put", "patch", "delete"]

Usage

1. Inherit models from HistoricalModel

from undo_revision.models import HistoricalModel

class Document(HistoricalModel):
    title = models.CharField(max_length=255)
    body = models.TextField()

HistoricalModel wires up RevisionHistoricalRecords (an extension of HistoricalRecords) and RevisionQuerySet.

2. Open a revision around mutations

from undo_revision.revision.context import open_revision

with open_revision(project_id=project.id):
    doc.title = "New title"
    doc.save()
    other_obj.delete()
# All changes inside the block are grouped into one revision

If no changes were recorded inside the block, the revision is deleted automatically.

3. Undo the last revision

from undo_revision.revision.undo import undo_last_revision, RevisionNotFoundError

try:
    undo_last_revision(project)
except RevisionNotFoundError:
    print("Nothing to undo")

The function fetches the latest revision for the project and rolls back all its changes in reverse chronological order.

4. Auto-open revisions via mixin (DRF / CBV)

from undo_revision.revision.mixins import UndoRevisionMixin
from rest_framework.viewsets import ModelViewSet

class DocumentViewSet(UndoRevisionMixin, ModelViewSet):
    project_url_kwarg = "project_id"  # URL kwarg carrying the project id
    ...

The mixin wraps all mutating methods (post, put, patch, delete) with open_revision automatically.

5. Bulk operations

RevisionQuerySet exposes helpers for bulk mutations:

MyModel.objects.bulk_create_with_history(objs)
MyModel.objects.bulk_update_with_history(objs, fields=["title"])

# Skip history (not tracked in any revision)
MyModel.objects.bulk_create_without_history(objs)
MyModel.objects.bulk_update_without_history(objs, fields=["title"])
MyModel.objects.create_without_history(title="...")
MyModel.objects.filter(...).delete_without_history()

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_undo_revision-1.0.0.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_undo_revision-1.0.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file django_undo_revision-1.0.0.tar.gz.

File metadata

  • Download URL: django_undo_revision-1.0.0.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_undo_revision-1.0.0.tar.gz
Algorithm Hash digest
SHA256 31ca398f286d1ad51b93f369e0435d9a2b83c73c62641555ab8da6816feaf9bc
MD5 1487652fc309775c8747726fcc8c8cbc
BLAKE2b-256 6cf8e2cc6763490ccfc2c88478180bcb1b423b22a0e5fc26947740788fccf5f7

See more details on using hashes here.

File details

Details for the file django_undo_revision-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_undo_revision-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95692f022dfcca78db36652d0d4eead989d1bf5024438969f4916a0315f4d40f
MD5 e6129b9903fd2cabc52bedd116689a3d
BLAKE2b-256 c0a44538fe86b9039f83977913ec2c45f111e5824d7de2bffd748b76cdcd2c31

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page