Skip to content

Commit 86316c0

Browse files
committed
Make it compatible with fsm-log
1 parent 382838e commit 86316c0

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

django_fsm/admin.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4+
from functools import partial
45
from typing import Any
56

67
from django.conf import settings
@@ -15,6 +16,13 @@
1516

1617
import django_fsm as fsm
1718

19+
try:
20+
import django_fsm_log # noqa: F401
21+
except ModuleNotFoundError:
22+
FSM_LOG_ENABLED = False
23+
else:
24+
FSM_LOG_ENABLED = True
25+
1826

1927
@dataclass
2028
class FSMObjectTransition:
@@ -127,7 +135,20 @@ def response_change(self, request: HttpRequest, obj: Any) -> HttpResponse:
127135
)
128136

129137
try:
130-
transition_func()
138+
if FSM_LOG_ENABLED:
139+
for fn in [
140+
partial(transition_func, request=request, by=request.user),
141+
partial(transition_func, by=request.user),
142+
transition_func,
143+
]:
144+
try:
145+
fn()
146+
except TypeError: # noqa: PERF203
147+
pass
148+
else:
149+
break
150+
else:
151+
transition_func()
131152
except fsm.TransitionNotAllowed:
132153
self.message_user(
133154
request=request,

tests/settings.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"django.contrib.sessions",
4444
"django.contrib.messages",
4545
"django.contrib.staticfiles",
46+
"django_fsm_log",
4647
"guardian",
4748
*PROJECT_APPS,
4849
]
@@ -135,3 +136,34 @@
135136
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
136137

137138
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
139+
140+
141+
# Django FSM-log settings
142+
DJANGO_FSM_LOG_IGNORED_MODELS = (
143+
"tests.testapp.models.AdminBlogPost",
144+
"tests.testapp.models.Application",
145+
"tests.testapp.models.BlogPost",
146+
"tests.testapp.models.DbState",
147+
"tests.testapp.models.FKApplication",
148+
"tests.testapp.tests.SimpleBlogPost",
149+
"tests.testapp.tests.test_abstract_inheritance.BaseAbstractModel",
150+
"tests.testapp.tests.test_abstract_inheritance.InheritedFromAbstractModel",
151+
"tests.testapp.tests.test_access_deferred_fsm_field.DeferrableModel",
152+
"tests.testapp.tests.test_basic_transitions.SimpleBlogPost",
153+
"tests.testapp.tests.test_conditions.BlogPostWithConditions",
154+
"tests.testapp.tests.test_custom_data.BlogPostWithCustomData",
155+
"tests.testapp.tests.test_exception_transitions.ExceptionalBlogPost",
156+
"tests.testapp.tests.test_graph_transitions.VisualBlogPost",
157+
"tests.testapp.tests.test_integer_field.BlogPostWithIntegerField",
158+
"tests.testapp.tests.test_lock_mixin.ExtendedBlogPost",
159+
"tests.testapp.tests.test_lock_mixin.LockedBlogPost",
160+
"tests.testapp.tests.test_mixin_support.MixinSupportTestModel",
161+
"tests.testapp.tests.test_multi_resultstate.MultiResultTest",
162+
"tests.testapp.tests.test_multidecorators.TestModel",
163+
"tests.testapp.tests.test_protected_field.ProtectedAccessModel",
164+
"tests.testapp.tests.test_protected_fields.RefreshableProtectedAccessModel",
165+
"tests.testapp.tests.test_proxy_inheritance.InheritedModel",
166+
"tests.testapp.tests.test_state_transitions.Caterpillar",
167+
"tests.testapp.tests.test_string_field_parameter.BlogPostWithStringField",
168+
"tests.testapp.tests.test_transition_all_except_target.TestExceptTargetTransitionShortcut",
169+
)

tests/testapp/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from django.contrib import admin
4+
from django_fsm_log.admin import StateLogInline
45

56
from django_fsm.admin import FSMAdminMixin
67

@@ -20,3 +21,5 @@ class AdminBlogPostAdmin(FSMAdminMixin, admin.ModelAdmin):
2021
"state",
2122
"step",
2223
]
24+
25+
inlines = [StateLogInline]

tests/testapp/tests/test_admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def test_transition_applied(self, mock_message_user):
139139
path="/",
140140
data={"_fsm_transition_to": "moderate"},
141141
)
142+
request.user = MockSuperUser()
142143

143144
blog_post = AdminBlogPost.objects.create(title="Article name")
144145
assert blog_post.state == AdminBlogPostState.CREATED
@@ -162,6 +163,7 @@ def test_transition_not_allowed_exception(self, mock_message_user):
162163
path="/",
163164
data={"_fsm_transition_to": "publish"},
164165
)
166+
request.user = MockSuperUser()
165167

166168
blog_post = AdminBlogPost.objects.create(title="Article name")
167169
assert blog_post.state == AdminBlogPostState.CREATED
@@ -185,6 +187,7 @@ def test_concurrent_transition_exception(self, mock_message_user):
185187
path="/",
186188
data={"_fsm_transition_to": "moderate"},
187189
)
190+
request.user = MockSuperUser()
188191

189192
blog_post = AdminBlogPost.objects.create(title="Article name")
190193
assert blog_post.state == AdminBlogPostState.CREATED

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ deps =
1313
dj51: Django==5.1
1414
dj52: Django==5.2
1515

16+
django-fsm-log
1617
django-guardian
1718
graphviz
1819
pep8

0 commit comments

Comments
 (0)