Git Product home page Git Product logo

django-fsm's Introduction

Django friendly finite state machine support

django-fsm adds declarative states managment for django models. Instead of adding some state field to a django model, and manage it values by hand, you could use FSMState field and mark model methods with the transition decorator. Your method will contain the side-effects of the state change.

The decorator also takes a list of conditions, all of which must be met before a transition is allowed.

Installation

$ python setup.py install

Or, for the latest git version

$ pip install -e git://github.com/kmmbvnr/django-fsm.git#egg=django-fsm

Usage

Add FSMState field to you model

from fsm.fields import FSMField

class BlogPost(models.Model):
    state = FSMField(default='new')

Use the transition decorator to annotate model methods

from fsm import transition

@transition(source='new', target='published')
def publish(self):
    """
    This function may contain side-effects, 
    like updating caches, notifying users, etc.
    The return value will be discarded.
    """

source parameter accepts a list of states, or an individual state. You can use * for source, to allow switching to target from any state.

If calling publish() succeeds without raising an exception, the state field will be changed, but not written to the database.

from fsm import can_proceed

def publish_view(request, post_id):
    post = get_object__or_404(BlogPost, pk=post_id)
    if not can_proceed(post.publish):
         raise Http404;

    post.publish()
    post.save()
    return redirect('/')

If you use the transition decorator with the save argument set to True, the new state will be written to the database

@transition(source='new', target='published', save=True)
def publish(self):
    """
    Side effects other than changing state goes here
    """

If you require some conditions to be met before changing state, use the conditions argument to transition. conditions must be a list of functions that take one argument, the model instance. The function must return either True or False or a value that evaluates to True or False. If all functions return True, all conditions are considered to be met and transition is allowed to happen. If one of the functions return False, the transition will not happen. These functions should not have any side effects.

You can use ordinary functions

def can_publish(instance):
    # No publishing after 17 hours
    if datetime.datetime.now().hour > 17:
       return False
    return True

Or model methods

def can_destroy(self):
    return self.is_under_investigation()

Use the conditions like this:

@transition(source='new', target='published', conditions=[can_publish])
def publish(self):
    """
    Side effects galore
    """

@transition(source='*', target='destroyed', conditions=[can_destroy])
def destroy(self):
    """
    Side effects galore
    """

If you store the states in the db table you could use FSMKeyField to ensure Foreign Key database integrity.

django-fsm's People

Contributors

kmmbvnr avatar cyberdelia avatar

Stargazers

Miglu avatar Gregory TAPPERO avatar

Watchers

James Cloos avatar  avatar  avatar Evgeny Sizikov avatar Shota avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.