File: README.rst

package info (click to toggle)
django-fsm 2.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 260 kB
  • sloc: python: 1,484; makefile: 9
file content (421 lines) | stat: -rw-r--r-- 11,777 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
Django friendly finite state machine support
============================================

|Build Status|

django-fsm adds simple declarative state management for django models.

If you need parallel task execution, view and background task code reuse
over different flows - check my new project django-viewflow:

https://github.com/viewflow/viewflow


Instead of adding a state field to a django model and managing its
values by hand, you use ``FSMField`` and mark model methods with
the ``transition`` decorator. These methods could contain side-effects
of the state change.

Nice introduction is available here:
https://gist.github.com/Nagyman/9502133

You may also take a look at django-fsm-admin project containing a mixin
and template tags to integrate django-fsm state transitions into the
django admin.

https://github.com/gadventures/django-fsm-admin

Transition logging support could be achived with help of django-fsm-log
package

https://github.com/gizmag/django-fsm-log

FSM really helps to structure the code, especially when a new developer
comes to the project. FSM is most effective when you use it for some
sequential steps.


Installation
------------

.. code:: bash

    $ pip install django-fsm

Or, for the latest git version

.. code:: bash

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

The library has full Python 3 support

Usage
-----

Add FSMState field to your model

.. code:: python

    from django_fsm import FSMField, transition

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

Use the ``transition`` decorator to annotate model methods

.. code:: python

    @transition(field=state, 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. The ``field`` parameter accepts both a string attribute name or an
actual field instance.

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

.. code:: python

    from django_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 PermissionDenied

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

If some conditions are required to be met before changing the state, use
the ``conditions`` argument to ``transition``. ``conditions`` must be a
list of functions taking 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 the transition is allowed to happen. If one
of the functions returns ``False``, the transition will not happen.
These functions should not have any side effects.

You can use ordinary functions

.. code:: python

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

Or model methods

.. code:: python

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

Use the conditions like this:

.. code:: python

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

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

You can instantiate a field with ``protected=True`` option to prevent
direct state field modification.

.. code:: python

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

    model = BlogPost()
    model.state = 'invalid' # Raises AttributeError

Note that calling
`refresh_from_db <https://docs.djangoproject.com/en/1.8/ref/models/instances/#django.db.models.Model.refresh_from_db>`_
on a model instance with a protected FSMField will cause an exception.

`target`
~~~~~~~~

`target` state parameter could point to a specific state or `django_fsm.State` implementation

.. code:: python
          
    from django_fsm import FSMField, transition, RETURN_VALUE, GET_STATE
    @transition(field=state,
                source='*',
                target=RETURN_VALUE('for_moderators', 'published'))
    def publish(self, is_public=False):
        return 'for_moderators' if is_public else 'published'

    @transition(
        field=state,
        source='for_moderators',
        target=GET_STATE(
            lambda self, allowed: 'published' if allowed else 'rejected',
            states=['published', 'rejected']))
    def moderate(self, allowed):
        self.allowed=allowed


``custom`` properties
~~~~~~~~~~~~~~~~~~~~~

Custom properties can be added by providing a dictionary to the
``custom`` keyword on the ``transition`` decorator.

.. code:: python

    @transition(field=state,
                source='*',
                target='onhold',
                custom=dict(verbose='Hold for legal reasons'))
    def legal_hold(self):
        """
        Side effects galore
        """

``on_error`` state
~~~~~~~~~~~~~~~~~~

If the transition method raises an exception, you can provide a
specific target state

.. code:: python

    @transition(field=state, source='new', target='published', on_error='failed')
    def publish(self):
       """
       Some exception could happen here
       """

``state_choices``
~~~~~~~~~~~~~~~~~

Instead of passing a two-item iterable ``choices`` you can instead use the
three-element ``state_choices``, the last element being a string reference
to a model proxy class.

The base class instance would be dynamically changed to the corresponding Proxy
class instance, depending on the state. Even for queryset results, you
will get Proxy class instances, even if the QuerySet is executed on the base class.

Check the `test
case <https://github.com/kmmbvnr/django-fsm/blob/master/tests/testapp/tests/test_state_transitions.py>`__
for example usage. Or read about `implementation
internals <http://schinckel.net/2013/06/13/django-proxy-model-state-machine/>`__

Permissions
~~~~~~~~~~~

It is common to have permissions attached to each model transition.
``django-fsm`` handles this with ``permission`` keyword on the
``transition`` decorator. ``permission`` accepts a permission string, or
callable that expects ``instance`` and ``user`` arguments and returns
True if the user can perform the transition.

.. code:: python

    @transition(field=state, source='*', target='publish',
                permission=lambda instance, user: not user.has_perm('myapp.can_make_mistakes'))
    def publish(self):
        pass

    @transition(field=state, source='*', target='publish',
                permission='myapp.can_remove_post')
    def remove(self):
        pass

You can check permission with ``has_transition_permission`` method

.. code:: python

    from django_fsm import has_transition_perm
    def publish_view(request, post_id):
        post = get_object_or_404(BlogPost, pk=post_id)
        if not has_transition_perm(post.publish, request.user):
            raise PermissionDenied

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

Model methods
~~~~~~~~~~~~~

``get_all_FIELD_transitions`` Enumerates all declared transitions

``get_available_FIELD_transitions`` Returns all transitions data
available in current state

``get_available_user_FIELD_transitions`` Enumerates all transitions data
available in current state for provided user

Foreign Key constraints support
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

In your model :

.. code:: python

    class DbState(models.Model):
        id = models.CharField(primary_key=True, max_length=50)
        label = models.CharField(max_length=255)

        def __unicode__(self):
            return self.label


    class BlogPost(models.Model):
        state = FSMKeyField(DbState, default='new')

        @transition(field=state, source='new', target='published')
        def publish(self):
            pass

In your fixtures/initial\_data.json :

.. code:: json

    [
        {
            "pk": "new",
            "model": "myapp.dbstate",
            "fields": {
                "label": "_NEW_"
            }
        },
        {
            "pk": "published",
            "model": "myapp.dbstate",
            "fields": {
                "label": "_PUBLISHED_"
            }
        }
    ]

Note : source and target parameters in @transition decorator use pk
values of DBState model as names, even if field "real" name is used,
without \_id postfix, as field parameter.

Integer Field support
~~~~~~~~~~~~~~~~~~~~~

You can also use ``FSMIntegerField``. This is handy when you want to use
enum style constants.

.. code:: python

    class BlogPostStateEnum(object):
        NEW = 10
        PUBLISHED = 20
        HIDDEN = 30

    class BlogPostWithIntegerField(models.Model):
        state = FSMIntegerField(default=BlogPostStateEnum.NEW)

        @transition(field=state, source=BlogPostStateEnum.NEW, target=BlogPostStateEnum.PUBLISHED)
        def publish(self):
            pass

Signals
~~~~~~~

``django_fsm.signals.pre_transition`` and
``django_fsm.signals.post_transition`` are called before and after
allowed transition. No signals on invalid transition are called.

Arguments sent with these signals:

**sender** The model class.

**instance** The actual instance being processed

**name** Transition name

**source** Source model state

**target** Target model state

Optimistic locking
------------------

``django-fsm`` provides optimistic locking mixin, to avoid concurrent
model state changes. If model state was changed in database
``django_fsm.ConcurrentTransition`` exception would be raised on
model.save()

.. code:: python

    from django_fsm import FSMField, ConcurrentTransitionMixin

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

For guaranteed protection against race conditions caused by concurrently
executed transitions, make sure:

- Your transitions do not have any side effects except for changes in the database,
- You always run the save() method on the object within ``django.db.transaction.atomic()`` block.

Following these recommendations, you can rely on
ConcurrentTransitionMixin to cause a rollback of all the changes that
have been executed in an inconsistent (out of sync) state, thus
practically negating their effect.

Drawing transitions
-------------------

Renders a graphical overview of your models states transitions

You need ``pip install graphviz>=0.4`` library and add ``django_fsm`` to
your ``INSTALLED_APPS``:

.. code:: python

    INSTALLED_APPS = (
        ...
        'django_fsm',
        ...
    )

.. code:: bash

    # Create a dot file
    $ ./manage.py graph_transitions > transitions.dot

    # Create a PNG image file only for specific model
    $ ./manage.py graph_transitions -o blog_transitions.png myapp.Blog

Changelog
---------

django-fsm 2.6.0 2017-06-08
~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Fix django 1.11 compatibility
- Fix TypeError in `graph_transitions` command when using django's lazy translations

.. |Build Status| image:: https://travis-ci.org/viewflow/django-fsm.svg?branch=master
   :target: https://travis-ci.org/viewflow/django-fsm