Skip to content

Commit d553b35

Browse files
committed
Add 'ignore' to graph_transition command
1 parent 04be14b commit d553b35

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ $ ./manage.py graph_transitions > transitions.dot
429429

430430
# Create a PNG image file only for specific model
431431
$ ./manage.py graph_transitions -o blog_transitions.png myapp.Blog
432+
433+
# Exclude some transitions
434+
$ ./manage.py graph_transitions -e transition_1,transition_2 myapp.Blog
432435
```
433436

434437
## Extensions

django_fsm/management/commands/graph_transitions.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ def node_label(field, state):
2727
return state
2828

2929

30-
def generate_dot(fields_data): # noqa: C901, PLR0912
30+
def generate_dot(fields_data, ignore_transitions: list[str] | None = None): # noqa: C901, PLR0912
31+
ignore_transitions = ignore_transitions or []
3132
result = graphviz.Digraph()
3233

3334
for field, model in fields_data:
3435
sources, targets, edges, any_targets, any_except_targets = set(), set(), set(), set(), set()
3536

3637
# dump nodes and edges
3738
for transition in field.get_all_transitions(model):
39+
if transition.name in ignore_transitions:
40+
continue
41+
3842
_targets = list(
3943
(state for state in transition.target.allowed_states)
4044
if isinstance(transition.target, (GET_STATE, RETURN_VALUE))
@@ -127,7 +131,7 @@ def add_arguments(self, parser):
127131
"-o",
128132
action="store",
129133
dest="outputfile",
130-
help=("Render output file. Type of output dependent on file extensions. Use png or jpg to render graph to image."),
134+
help="Render output file. Type of output dependent on file extensions. Use png or jpg to render graph to image.",
131135
)
132136
parser.add_argument(
133137
"--layout",
@@ -137,6 +141,14 @@ def add_arguments(self, parser):
137141
default="dot",
138142
help=f"Layout to be used by GraphViz for visualization. Layouts: {get_graphviz_layouts()}.",
139143
)
144+
parser.add_argument(
145+
"--exclude",
146+
"-e",
147+
action="store",
148+
dest="exclude",
149+
default="",
150+
help="Ignore transitions with this name.",
151+
)
140152
parser.add_argument("args", nargs="*", help=("[appname[.model[.field]]]"))
141153

142154
def render_output(self, graph, **options):
@@ -153,9 +165,8 @@ def handle(self, *args, **options):
153165
field_spec = arg.split(".")
154166

155167
if len(field_spec) == 1:
156-
app = apps.get_app(field_spec[0])
157-
models = apps.get_models(app)
158-
for model in models:
168+
app = apps.get_app_config(field_spec[0])
169+
for model in apps.get_models(app):
159170
fields_data += all_fsm_fields_data(model)
160171
if len(field_spec) == 2: # noqa: PLR2004
161172
model = apps.get_model(field_spec[0], field_spec[1])
@@ -166,7 +177,8 @@ def handle(self, *args, **options):
166177
else:
167178
for model in apps.get_models():
168179
fields_data += all_fsm_fields_data(model)
169-
dotdata = generate_dot(fields_data)
180+
181+
dotdata = generate_dot(fields_data, ignore_transitions=options["exclude"].split(","))
170182

171183
if options["outputfile"]:
172184
self.render_output(dotdata, **options)

tests/testapp/tests/test_graph_transitions.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77

88

99
class GraphTransitionsCommandTest(TestCase):
10+
MODELS_TO_TEST = [
11+
"testapp.Application",
12+
"testapp.FKApplication",
13+
]
14+
1015
def test_dummy(self):
11-
call_command("graph_transitions", "testapp.Application")
16+
for model in self.MODELS_TO_TEST:
17+
call_command("graph_transitions", model)
1218

1319
def test_layouts(self):
14-
for layout in get_graphviz_layouts():
15-
call_command("graph_transitions", "-l", layout, "testapp.Application")
16-
17-
def test_fk_dummy(self):
18-
call_command("graph_transitions", "testapp.FKApplication")
20+
for model in self.MODELS_TO_TEST:
21+
for layout in get_graphviz_layouts():
22+
call_command("graph_transitions", "-l", layout, model)
1923

20-
def test_fk_layouts(self):
21-
for layout in get_graphviz_layouts():
22-
call_command("graph_transitions", "-l", layout, "testapp.FKApplication")
24+
def test_exclude(self):
25+
for model in self.MODELS_TO_TEST:
26+
call_command("graph_transitions", "-e", "standard,no_target", model)

0 commit comments

Comments
 (0)