Skip to main content

Django States

Build Status

Description

State engine for django models. Define a state graph for a model and remember the state of each object. State transitions can be logged for objects.

Installation

pip install django-states3

Usage example

To use a state machine, you should add a state field to the model

from django_states.fields import StateField
from django_states.machine import StateMachine, StateDefinition, StateTransition

class PurchaseStateMachine(StateMachine):
   log_transitions = True

   # possible states
   class initiated(StateDefinition):
       description = _('Purchase initiated')
       initial = True

   class paid(StateDefinition):
       description = _('Purchase paid')

       def handler(self, instance):
           code_to_execute_when_arriving_in_this_state()

   class shipped(StateDefinition):
       description = _('Purchase shipped')

   # state transitions
   class mark_paid(StateTransition):
       from_state = 'initiated'
       to_state = 'paid'
       description = 'Mark this purchase as paid'

   class ship(StateTransition):
       from_state = 'paid'
       to_state = 'shipped'
       description = 'Ship purchase'

       def handler(transition, instance, user):
           code_to_execute_during_this_transition()

       def has_permission(transition, instance, user):
           return true_when_user_can_make_this_transition()

class Purchase(StateModel):
    purchase_state = StateField(machine=PurchaseStateMachine, default='initiated')
    ... (other fields for a purchase)

If log_transitions is enabled, another model is created. Everything should be compatible with South_ for migrations.

Note: If you’re creating a DataMigration in South, remember to use obj.save(no_state_validation=True)

Usage example:

p = Purchase()

# Will automatically create state object for this purchase, in the
# initial state.
p.save()
p.get_purchase_state_info().make_transition('mark_paid', request.user) # User parameter is optional
p.state # Will return 'paid'
p.get_purchase_state_info().description # Will return 'Purchase paid'

# Returns an iterator of possible transitions for this purchase.
p.get_purchase_state_info().possible_transitions()

# Which can be used like this..
[x.get_name() for x in p.possible_transitions]

For better transition control, override:

  • has_permission(self, instance, user): Check whether this user is allowed to make this transition.

  • handler(self, instance, user): Code to run during this transition. When an exception has been raised in here, the transition will not be made.

Get all objects in a certain state:

Purchase.objects.filter(state='initiated')

Validation

You can add a test that needs to pass before a state transition can be executed. Well, you can add 2: one based on the current user (has_permission) and one generic (validate).

So on a StateTransition-object you need to specify an extra validate function (signature is validate(cls, instance)). This should yield TransitionValidationError, this way you can return multiple errors on that need to pass before the transition can happen.

The has_permission function (signature has_permission(transition, instance, user)) should check whether the given user is allowed to make the transition. E.g. a super user can moderate all comments while other users can only moderate comments on their blog-posts.

Groups

Sometimes you want to group several states together, since for a certain view (or other content) it doesn’t really matter which of the states it is. We support 2 different state groups, inclusive (only these) or exclusive (everything but these):

class is_paid(StateGroup):
    states = ['paid', 'shipped']

class is_paid(StateGroup):
    exclude_states = ['initiated']

State graph

You can get a graph of your states by running the graph_states management command.

python manage.py graph_states myapp.Purchase.state

This requires graphviz and python bindings for graphviz: pygraphviz and yapgvb.

Metadata

Release files for django-states3 3.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-states3 3.0.1
File Size Uploaded Provenance
django-states3-3.0.1.tar.gz 18.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-states3 3.0.1
File Interpreter ABI Platform
django_states3-3.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 39.5 kB

Release files / django-states3-3.0.1.tar.gz

Download URL django-states3-3.0.1.tar.gz
Size 18.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d54901076eb9b7c2b66bb546233edda3dd5dfa0e6004451d492551124729c4db
BLAKE2b-256 checksum
How to use checksums
18f84a410ff5ca2e019aa9b5f4e1e562d6aff9e00b637a826a76d842c6430ac6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.7.4 Darwin/18.7.0

Release files / django_states3-3.0.1-py3-none-any.whl

Download URL django_states3-3.0.1-py3-none-any.whl
Size 21.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca023e305accd280e604639dbaf123877e2d15e7d6e8fab9b486eee9a10ba10d
BLAKE2b-256 checksum
How to use checksums
d846ee8b6867be6b74f065e09b5d5f39effd1d4ba3c3639c1ce0a0f4c4852941
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.7.4 Darwin/18.7.0

Security for django-states3 3.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

3.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page