Skip to content

Commit cd703c2

Browse files
authored
Add 'ignore' to graph_transition command (#57)
1 parent 4bd42ac commit cd703c2

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

django_fsm/management/commands/graph_transitions.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ def node_name(field, state) -> str:
2121
return "{}.{}.{}.{}".format(opts.app_label, opts.verbose_name.replace(" ", "_"), field.name, state)
2222

2323

24-
def node_label(field, state) -> str:
25-
if isinstance(state, int):
26-
return str(state)
27-
if isinstance(state, bool) and hasattr(field, "choices"):
28-
return force_str(dict(field.choices).get(state))
29-
return state
24+
def node_label(field, state: str | None) -> str:
25+
if isinstance(state, (int, bool)) and hasattr(field, "choices") and field.choices:
26+
state = dict(field.choices).get(state)
27+
return force_str(state)
3028

3129

3230
def generate_dot(fields_data, ignore_transitions: list[str] | None = None): # noqa: C901, PLR0912

tests/testapp/models.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,23 @@ def on_error(self):
170170
pass
171171

172172

173+
class BlogPostState(models.IntegerChoices):
174+
NEW = 0, "New"
175+
PUBLISHED = 1, "Published"
176+
HIDDEN = 2, "Hidden"
177+
REMOVED = 3, "Removed"
178+
RESTORED = 4, "Restored"
179+
MODERATED = 5, "Moderated"
180+
STOLEN = 6, "Stolen"
181+
FAILED = 7, "Failed"
182+
183+
173184
class BlogPost(models.Model):
174185
"""
175186
Test workflow
176187
"""
177188

178-
state = FSMField(default="new", protected=True)
189+
state = FSMField(choices=BlogPostState.choices, default=BlogPostState.NEW, protected=True)
179190

180191
class Meta:
181192
permissions = [
@@ -186,41 +197,53 @@ class Meta:
186197
def can_restore(self, user):
187198
return user.is_superuser or user.is_staff
188199

189-
@transition(field=state, source="new", target="published", on_error="failed", permission="testapp.can_publish_post")
200+
@transition(
201+
field=state,
202+
source=BlogPostState.NEW,
203+
target=BlogPostState.PUBLISHED,
204+
on_error=BlogPostState.FAILED,
205+
permission="testapp.can_publish_post",
206+
)
190207
def publish(self):
191208
pass
192209

193-
@transition(field=state, source="published")
210+
@transition(field=state, source=BlogPostState.PUBLISHED)
194211
def notify_all(self):
195212
pass
196213

197214
@transition(
198215
field=state,
199-
source="published",
200-
target="hidden",
201-
on_error="failed",
216+
source=BlogPostState.PUBLISHED,
217+
target=BlogPostState.HIDDEN,
218+
on_error=BlogPostState.FAILED,
202219
)
203220
def hide(self):
204221
pass
205222

206223
@transition(
207224
field=state,
208-
source="new",
209-
target="removed",
210-
on_error="failed",
225+
source=BlogPostState.NEW,
226+
target=BlogPostState.REMOVED,
227+
on_error=BlogPostState.FAILED,
211228
permission=lambda _, u: u.has_perm("testapp.can_remove_post"),
212229
)
213230
def remove(self):
214231
raise Exception(f"No rights to delete {self}")
215232

216-
@transition(field=state, source="new", target="restored", on_error="failed", permission=can_restore)
233+
@transition(
234+
field=state,
235+
source=BlogPostState.NEW,
236+
target=BlogPostState.RESTORED,
237+
on_error=BlogPostState.FAILED,
238+
permission=can_restore,
239+
)
217240
def restore(self):
218241
pass
219242

220-
@transition(field=state, source=["published", "hidden"], target="stolen")
243+
@transition(field=state, source=[BlogPostState.PUBLISHED, BlogPostState.HIDDEN], target=BlogPostState.STOLEN)
221244
def steal(self):
222245
pass
223246

224-
@transition(field=state, source="*", target="moderated")
247+
@transition(field=state, source="*", target=BlogPostState.MODERATED)
225248
def moderate(self):
226249
pass

tests/testapp/tests/test_graph_transitions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from django.test import TestCase
55

66
from django_fsm.management.commands.graph_transitions import get_graphviz_layouts
7+
from django_fsm.management.commands.graph_transitions import node_label
8+
from tests.testapp.models import BlogPost
9+
from tests.testapp.models import BlogPostState
710

811

912
class GraphTransitionsCommandTest(TestCase):
@@ -12,6 +15,9 @@ class GraphTransitionsCommandTest(TestCase):
1215
"testapp.FKApplication",
1316
]
1417

18+
def test_node_label(self):
19+
assert node_label(BlogPost.state.field, BlogPostState.PUBLISHED.value) == BlogPostState.PUBLISHED.label
20+
1521
def test_app(self):
1622
call_command("graph_transitions", "testapp")
1723

0 commit comments

Comments
 (0)