• Stars
    star
    235
  • Rank 165,284 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created over 10 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatic logging for Django FSM

Django Finite State Machine Log

test suite codecov Jazzband pre-commit.ci status Documentation Status

Provides persistence of the transitions of your fsm's models. Backed by the excellent Django FSM package.

Logs can be accessed before a transition occurs and before they are persisted to the database by enabling a cached backend. See Advanced Usage

Changelog

4.0.0 (not released)

  • remove support for django 2.2 & 4.0

3.1.0 (2023-03-23)

  • fsm_log_description now accepts a default description parameter
  • Document fsm_log_description decorator
  • Add support for Django 4.1
  • Add compatibility for python 3.11

3.0.0 (2022-01-14)

  • Switch to github actions (from travis-ci)
  • Test against django 3.2 and 4.0, then python 3.9 and 3.10
  • Drop support for django 1.11, 2.0, 2.1, 3.0, 3.1
  • Drop support for python 3.4, 3.5, 3.6
  • allow using StateLogManager in migrations #95

2.0.1 (2020-03-26)

  • Add support for django3.0
  • Drop support for python2

1.6.2 (2019-01-06)

  • Address Migration history breakage added in 1.6.1

1.6.1 (2018-12-02)

  • Make StateLog.description field nullable

1.6.0 (2018-11-14)

  • Add source state on transitions
  • Fixed get_state_display with FSMIntegerField (#63)
  • Fixed handling of transitions if target is None (#71)
  • Added fsm_log_description decorator (#1, #67)
  • Dropped support for Django 1.10 (#64)

1.5.0 (2017-11-29)

  • cleanup deprecated code.
  • add codecov support.
  • switch to pytest.
  • add Admin integration to visualize past transitions.

1.4.0 (2017-11-09)

  • Bring compatibility with Django 2.0 and drop support of unsupported versions of Django: 1.6, 1.7, 1.9.

Compatibility

  • Python 2.7 and 3.4+
  • Django 1.8+
  • Django-FSM 2+

Installation

First, install the package with pip. This will automatically install any dependencies you may be missing

pip install django-fsm-log

Register django_fsm_log in your list of Django applications:

INSTALLED_APPS = (
    ...,
    'django_fsm_log',
    ...,
)

Then migrate the app to create the database table

python manage.py migrate django_fsm_log

Usage

The app listens for the django_fsm.signals.post_transition signal and creates a new record for each transition.

To query the log:

from django_fsm_log.models import StateLog
StateLog.objects.all()
# ...all recorded logs...

Disabling logging for specific models

By default transitions get recorded for all models. Logging can be disabled for specific models by adding their fully qualified name to DJANGO_FSM_LOG_IGNORED_MODELS.

DJANGO_FSM_LOG_IGNORED_MODELS = ('poll.models.Vote',)

for_ Manager Method

For convenience there is a custom for_ manager method to easily filter on the generic foreign key:

from my_app.models import Article
from django_fsm_log.models import StateLog

article = Article.objects.all()[0]

StateLog.objects.for_(article)
# ...logs for article...

by Decorator

We found that our transitions are commonly called by a user, so we've added a decorator to make logging this easy:

from django.db import models
from django_fsm import FSMField, transition
from django_fsm_log.decorators import fsm_log_by

class Article(models.Model):

    state = FSMField(default='draft', protected=True)

    @fsm_log_by
    @transition(field=state, source='draft', target='submitted')
    def submit(self, by=None):
        pass

With this the transition gets logged when the by kwarg is present.

article = Article.objects.create()
article.submit(by=some_user) # StateLog.by will be some_user

description Decorator

Decorator that allows to set a custom description (saved on database) to a transitions.

from django.db import models
from django_fsm import FSMField, transition
from django_fsm_log.decorators import fsm_log_description

class Article(models.Model):

    state = FSMField(default='draft', protected=True)

    @fsm_log_description(description='Article submitted')  # description param is NOT required
    @transition(field=state, source='draft', target='submitted')
    def submit(self, description=None):
        pass

article = Article.objects.create()
article.submit()  # logged with "Article submitted" description
article.submit(description="Article reviewed and submitted")  # logged with "Article reviewed and submitted" description

.. TIP:: The "description" argument passed when calling ".submit" has precedence over the default description set in the decorator

The decorator also accepts a allow_inline boolean argument that allows to set the description inside the transition method.

from django.db import models
from django_fsm import FSMField, transition
from django_fsm_log.decorators import fsm_log_description

class Article(models.Model):

    state = FSMField(default='draft', protected=True)

    @fsm_log_description(allow_inline=True)
    @transition(field=state, source='draft', target='submitted')
    def submit(self, description=None):
        description.set("Article submitted")

article = Article.objects.create()
article.submit()  # logged with "Article submitted" description

Admin integration

There is an InlineForm available that can be used to display the history of changes.

To use it expand your own AdminModel by adding StateLogInline to its inlines:

from django.contrib import admin
from django_fsm_log.admin import StateLogInline


@admin.register(FSMModel)
class FSMModelAdmin(admin.ModelAdmin):
    inlines = [StateLogInline]

Advanced Usage

You can change the behaviour of this app by turning on caching for StateLog records. Simply add DJANGO_FSM_LOG_STORAGE_METHOD = 'django_fsm_log.backends.CachedBackend' to your project's settings file. It will use your project's default cache backend by default. If you wish to use a specific cache backend, you can add to your project's settings:

DJANGO_FSM_LOG_CACHE_BACKEND = 'some_other_cache_backend'

The StateLog object is now available after the django_fsm.signals.pre_transition signal is fired, but is deleted from the cache and persisted to the database after django_fsm.signals.post_transition is fired.

This is useful if:

  • you need immediate access to StateLog details, and cannot wait until django_fsm.signals.post_transition has been fired
  • at any stage, you need to verify whether or not the StateLog has been written to the database

Access to the pending StateLog record is available via the pending_objects manager

from django_fsm_log.models import StateLog
article = Article.objects.get(...)
pending_state_log = StateLog.pending_objects.get_for_object(article)

Contributing

Running tests

pip install tox
tox

Linting with pre-commit

We use ruff, black and more, all configured and check via pre-commit. Before committing, run the following:

pip install pre-commit
pre-commit install

More Repositories

1

django-debug-toolbar

A configurable set of panels that display various debug information about the current request/response.
Python
7,858
star
2

pip-tools

A set of tools to keep your pinned Python dependencies fresh.
Python
7,398
star
3

tablib

Python Module for Tabular Datasets in XLS, CSV, JSON, YAML, &c.
Python
4,500
star
4

django-silk

Silky smooth profiling for Django
Python
4,210
star
5

djangorestframework-simplejwt

A JSON Web Token authentication plugin for the Django REST Framework.
Python
3,765
star
6

django-taggit

Simple tagging for django
Python
3,205
star
7

django-oauth-toolkit

OAuth2 goodies for the Djangonauts!
Python
3,021
star
8

django-redis

Full featured redis cache backend for Django.
Python
2,773
star
9

django-model-utils

Django model mixins and utilities.
Python
2,577
star
10

django-push-notifications

Send push notifications to mobile devices through GCM or APNS in Django.
Python
2,210
star
11

django-simple-history

Store model history and view/revert changes from admin site.
Python
2,083
star
12

django-widget-tweaks

Tweak the form field rendering in templates, not in python-level form definitions. CSS classes and HTML attributes can be altered.
Python
2,019
star
13

sorl-thumbnail

Thumbnails for Django
Python
1,717
star
14

django-constance

Dynamic Django settings.
Python
1,643
star
15

django-two-factor-auth

Complete Two-Factor Authentication for Django providing the easiest integration into most Django projects.
Python
1,590
star
16

django-polymorphic

Improved Django model inheritance with automatic downcasting
Python
1,577
star
17

django-pipeline

Pipeline is an asset packaging library for Django.
Python
1,489
star
18

dj-database-url

Use Database URLs in your Django Application.
Python
1,439
star
19

django-axes

Keep track of failed login attempts in Django-powered sites.
Python
1,356
star
20

django-tinymce

TinyMCE integration for Django
JavaScript
1,231
star
21

prettytable

Display tabular data in a visually appealing ASCII table format
Python
1,223
star
22

django-admin2

Extendable, adaptable rewrite of django.contrib.admin
Python
1,182
star
23

django-analytical

Analytics services for Django projects
Python
1,174
star
24

django-smart-selects

chained and grouped selects for django forms
Python
1,094
star
25

django-waffle

A feature flipper for Django
Python
1,075
star
26

django-configurations

A helper for organizing Django project settings by relying on well established programming patterns.
Python
1,067
star
27

django-rest-knox

Authentication Module for django rest auth
Python
1,057
star
28

django-defender

A simple super fast django reusable app that blocks people from brute forcing login attempts
Python
997
star
29

django-auditlog

A Django app that keeps a log of changes made to an object.
Python
990
star
30

django-payments

Universal payment handling for Django.
Python
964
star
31

django-hosts

Dynamic and static host resolving for Django. Maps hostnames to URLconfs.
Python
942
star
32

django-nose

Django test runner using nose
Python
882
star
33

django-dbbackup

Management commands to help backup and restore your project database and media files
Python
879
star
34

geojson

Python bindings and utilities for GeoJSON
Python
876
star
35

django-floppyforms

Full control of form rendering in the templates.
Python
836
star
36

django-newsletter

An email newsletter application for the Django web application framework, including an extended admin interface, web (un)subscription, dynamic e-mail templates, an archive and HTML email support.
Python
825
star
37

django-avatar

A Django app for handling user avatars.
Python
797
star
38

django-formtools

A set of high-level abstractions for Django forms
Python
735
star
39

django-user-sessions

Extend Django sessions with a foreign key back to the user, allowing enumerating all user's sessions.
Python
586
star
40

django-admin-sortable

Generic drag-and-drop ordering for objects and tabular inlines in Django Admin
Python
557
star
41

django-invitations

Generic invitations app for Django
Python
530
star
42

django-sortedm2m

A transparent sorted ManyToMany field for django.
Python
508
star
43

django-recurrence

Utility for working with recurring dates in Django.
Python
460
star
44

django-categories

This app attempts to provide a generic category system that multiple apps could use. It uses MPTT for the tree storage and provides a custom admin for better visualization (copied and modified from feinCMS).
Python
455
star
45

django-robots

A Django app for managing robots.txt files following the robots exclusion protocol
Python
451
star
46

django-embed-video

Django app for easy embedding YouTube and Vimeo videos and music from SoundCloud.
Python
383
star
47

wagtailmenus

An app to help you manage and render menus in your Wagtail projects more effectively
Python
380
star
48

django-downloadview

Serve files with Django.
Python
357
star
49

jsonmodels

jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
Python
328
star
50

django-queued-storage

Provides a proxy for Django storage backends that allows you to upload files locally and eventually serve them remotely
Python
314
star
51

django-permission

[Not maintained] An enhanced permission system which support object permission in Django
Python
302
star
52

django-eav2

Django EAV 2 - EAV storage for modern Django
Python
297
star
53

django-revproxy

Reverse Proxy view that supports all HTTP methods, Diazo transformations and Single Sign-On.
Python
290
star
54

django-authority

A Django app that provides generic per-object-permissions for Django's auth app and helpers to create custom permission checks.
Python
286
star
55

django-simple-menu

Simple, yet powerful, code-based menus for Django applications
Python
258
star
56

django-dbtemplates

Django template loader for database stored templates with extensible cache backend
JavaScript
250
star
57

django-mongonaut

Built from scratch to replicate some of the Django admin functionality and add some more, to serve as an introspective interface for Django and Mongo.
Python
240
star
58

django-cookie-consent

Reusable application for managing various cookies and visitors consent for their use in Django project.
Python
210
star
59

django-celery-monitor

Celery Monitoring for Django
Python
191
star
60

django-ddp

Django/PostgreSQL implementation of the Meteor server.
Python
167
star
61

icalevents

Python module for iCal URL/file parsing and querying.
Python
153
star
62

docopt-ng

Humane command line arguments parser. Now with maintenance, typehints, and complete test coverage.
Python
149
star
63

django-voting

A generic voting application for Django
Python
93
star
64

django-ical

iCal feeds for Django based on Django's syndication feed framework.
Python
89
star
65

django-flatblocks

django-chunks + headerfield + variable chunknames + "inclusion tag" == django-flatblocks
Python
82
star
66

django-redshift-backend

Redshift database backend for Django
Python
80
star
67

pathlib2

Backport of pathlib aiming to support the full stdlib Python API.
Python
80
star
68

website

Code for the Jazzband website
Python
63
star
69

django-sorter

A helper app for sorting objects in Django templates.
Python
53
star
70

django-discover-jenkins

A streamlined fork of django-jenkins designed to work with the default test command and the discover runner
Python
49
star
71

contextlib2

contextlib2 is a backport of the standard library's contextlib module to earlier Python versions.
Python
37
star
72

django-fernet-encrypted-fields

Python
35
star
73

imaplib2

Fork of Piers Lauder's imaplib2 library for Python.
Python
31
star
74

help

Use this repo to get help from the roadies
27
star
75

.github

Community health and config files for Jazzband
7
star
76

django-postgres-utils

Django app providing additional lookups and functions for PostgreSQL
Python
7
star
77

admin

Some admin files for Jazzband
3
star
78

actions

Various GitHub actions for Jazzband projects
1
star