Skip to content

Commit e5d0ab7

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

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

django_fsm/admin.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515

1616
import django_fsm as fsm
1717

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

1926
@dataclass
2027
class FSMObjectTransition:
@@ -127,7 +134,20 @@ def response_change(self, request: HttpRequest, obj: Any) -> HttpResponse:
127134
)
128135

129136
try:
130-
transition_func()
137+
if FSM_LOG_ENABLED:
138+
# Run the transition
139+
try:
140+
# Attempt to pass in the request and by argument if using django-fsm-log
141+
transition_func(request=request, by=request.user)
142+
except TypeError:
143+
try:
144+
# Attempt to pass in the by argument if using django-fsm-log
145+
transition_func(by=request.user)
146+
except TypeError:
147+
# If the function does not have a by attribute, just call with no arguments
148+
transition_func()
149+
else:
150+
transition_func()
131151
except fsm.TransitionNotAllowed:
132152
self.message_user(
133153
request=request,

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)