Skip to main content

A Django module to manage approval workflows for CRUD operations.

Project description

djangoapprove

djangoapprove is a Django module that manages approval workflows for CRUD operations on Django models. It allows interception of model operations (create, update, delete) and introduces a customizable approval flow before the changes are applied.

Features

  • Approval Workflow: Intercepts CRUD operations and creates approval requests.
  • Approval Types: Supports creation, updates, and deletions.
  • Direct Operations: Allows bypassing approval for direct operations.

Installation

  1. Install using pip:
    pip install djangoapprove
    
  2. Add djangoapprove to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    ...,
    'djangoapprove',
]
  1. Run migrations to create the Approval model table:
python manage.py makemigrations djangoapprove
python manage.py migrate

Usage

Basic Integration

To integrate djangoapprove with a model, use the ApprovalMixin and the ApprovalManager:

from django.db import models
from djangoapprove.mixins import ApprovalMixin, ApprovalManager

class MyModel(ApprovalMixin, models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

    objects = ApprovalManager() # or approval_objects = ApprovalManager for MyModel.approval_objects.create

    def get_unique_key(self):
        return f"mymodel-{self.pk or some_unique_property}"

Approving Requests

Once an operation is intercepted, it creates an Approval record. Approvals can be reviewed and transitioned to the next state (e.g., APPROVED).

from djangoapprove.models import Approval

# Approve a pending request
approval = Approval.objects.get(pk=1)
approval.transition("APPROVE")

Settings

You can customize states, transitions, and initial/completed statuses in your settings.py:

APPROVAL_ACTIONS = {
    "APPROVE": "Approve",
    "REJECT": "Reject",
}

APPROVAL_STATUSES = {
    "PENDING": "Pending",
    "APPROVED": "Approved",
    "REJECTED": "Rejected",
    "FAILED": "Failed",
}

APPROVAL_TRANSITIONS = {
    "APPROVE": {"PENDING": "APPROVED"}, # Applying APPROVE to PENDING results in APPROVED
    "REJECT": {"PENDING": "REJECTED"}, # Applying REJECT to PENDING results in REJECTED
}

APPROVAL_INITIAL_STATUS = "PENDING"
APPROVAL_COMPLETED_STATUS = "APPROVED"

Example: Approval Workflow

  1. Intercept Save Operation:
instance = MyModel(name="Example", description="This is a test.")
approval = instance.save()  # Does not save immediately
  1. Review The Approval:
# Fetch the pending approval
approval = Approval.objects.filter(unique_key='Example').first()
print(approval.data)  # View the data payload
  1. Approve The Request:
approval.transition("APPROVE")
  1. Verify Execution:
# The instance should now be created
instance = MyModel.objects.get(name="Example")

Direct Operations (Bypassing Approval)

If you need to bypass the approval process (e.g., for admin operations), you can use the direct_save and direct_delete methods:

instance = MyModel(name="Direct Save Example", value=123)
instance.direct_save()
instance.direct_delete()

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

djangoapprove-0.2.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

djangoapprove-0.2.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file djangoapprove-0.2.0.tar.gz.

File metadata

  • Download URL: djangoapprove-0.2.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for djangoapprove-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f0edabeae8b327d80758709e35928eaea2a8af47d66c5e6ccc6c33c8e9cc1bf5
MD5 a73b3dda4143d395d3c0a5f7ba894778
BLAKE2b-256 3d4150b9b6311c21e4072d0d3464ec060061890e9e3d4c519f9b256161a06481

See more details on using hashes here.

File details

Details for the file djangoapprove-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: djangoapprove-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for djangoapprove-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2777e4433b17d9597b1e308474933ca1e274214292d9da77962cb583b718c559
MD5 48309e2edce7ad29d46f3b7efeaea43d
BLAKE2b-256 353d525fad6dc00fadf02031047cf561b53a01ef7704b6e57dafdde827706e7e

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