Skip to content

Commit

Permalink
Admin integration (#58)
Browse files Browse the repository at this point in the history
Provide InlineForm to display transitions on admin
  • Loading branch information
ticosax authored Nov 29, 2017
1 parent 9b09da6 commit 6dc1c48
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,21 @@ article = Article.objects.create()
article.submit(by=some_user) # StateLog.by will be some_user
```

### 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:

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


@admin.register(FSMModel)
class FSMModel(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.
Expand Down
33 changes: 33 additions & 0 deletions django_fsm_log/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.contrib.contenttypes.admin import GenericTabularInline
from django.db.models import F

from .models import StateLog


__all__ = ('StateLogInline',)


class StateLogInline(GenericTabularInline):
model = StateLog
can_delete = False

def has_add_permission(self, request):
return False

def has_change_permission(self, request, obj=None):
return True

fields = (
'transition',
'state',
'by',
'timestamp',
)

def get_readonly_fields(self, request, obj=None):
return self.fields

def get_queryset(self, request):
return super(StateLogInline, self).get_queryset(
request
).order_by(F('timestamp').desc())
11 changes: 11 additions & 0 deletions tests/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.contrib import admin
from django_fsm_log.admin import StateLogInline

from .models import Article


class ArticleAdmin(admin.ModelAdmin):
inlines = [StateLogInline]


admin.site.register(Article, ArticleAdmin)
15 changes: 13 additions & 2 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@
'default': {'ENGINE': 'django.db.backends.sqlite3'}
}
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django_fsm_log',
'tests',
]
MIDDLEWARES = []
MIDDLEWARE_CLASSES = []
MIDDLEWARE = MIDDLEWARE_CLASSES = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
]
SECRET_KEY = 'abc123'
ROOT_URLCONF = 'tests.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
},
]
15 changes: 15 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
try:
from django.urls import reverse
except ImportError:
# django<=1.10
from django.core.urlresolvers import reverse


def test_state_log_inline_django2(article, admin_client, admin_user):
article.submit(by=admin_user)
article.publish(by=admin_user)
url = reverse('admin:tests_article_change', args=(article.pk,))
response = admin_client.get(url)
assert response.status_code == 200
assert '{} - submit'.format(article).encode() in response.content
assert '{} - publish'.format(article).encode() in response.content
11 changes: 11 additions & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
try:
from django.urls import path
from django.contrib import admin

urlpatterns = [path('admin', admin.site.urls)]
except ImportError:
# django < 2.0
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [url(r'^admin/', include(admin.site.urls))]

0 comments on commit 6dc1c48

Please sign in to comment.