Skip to content

Commit 613432b

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

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
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: 17 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,13 @@ 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+
help="Ignore transitions with this name.",
150+
)
140151
parser.add_argument("args", nargs="*", help=("[appname[.model[.field]]]"))
141152

142153
def render_output(self, graph, **options):
@@ -153,9 +164,8 @@ def handle(self, *args, **options):
153164
field_spec = arg.split(".")
154165

155166
if len(field_spec) == 1:
156-
app = apps.get_app(field_spec[0])
157-
models = apps.get_models(app)
158-
for model in models:
167+
app = apps.get_app_config(field_spec[0])
168+
for model in apps.get_models(app):
159169
fields_data += all_fsm_fields_data(model)
160170
if len(field_spec) == 2: # noqa: PLR2004
161171
model = apps.get_model(field_spec[0], field_spec[1])
@@ -166,7 +176,8 @@ def handle(self, *args, **options):
166176
else:
167177
for model in apps.get_models():
168178
fields_data += all_fsm_fields_data(model)
169-
dotdata = generate_dot(fields_data)
179+
180+
dotdata = generate_dot(fields_data, ignore_transitions=options.get("exclude", "").split(","))
170181

171182
if options["outputfile"]:
172183
self.render_output(dotdata, **options)

0 commit comments

Comments
 (0)