Skip to content

Commit cedaeb2

Browse files
foarsitterkmmbvnr
authored andcommitted
Implement Transition.__hash__ and __eq__ for 'in' operator
Signed-off-by: Jelmer Draaijer <[email protected]>
1 parent 22bc86b commit cedaeb2

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

django_fsm/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ def has_perm(self, instance, user):
123123
else:
124124
return False
125125

126+
def __hash__(self):
127+
return hash(self.name)
128+
129+
def __eq__(self, other):
130+
if isinstance(other, str):
131+
return other == self.name
132+
if isinstance(other, Transition):
133+
return other.name == self.name
134+
135+
return False
136+
126137

127138
def get_available_FIELD_transitions(instance, field):
128139
"""

django_fsm/tests/test_basic_transitions.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.db import models
22
from django.test import TestCase
33

4-
from django_fsm import FSMField, TransitionNotAllowed, transition, can_proceed
4+
from django_fsm import FSMField, TransitionNotAllowed, transition, can_proceed, Transition
55
from django_fsm.signals import pre_transition, post_transition
66

77

@@ -143,6 +143,29 @@ class TestFieldTransitionsInspect(TestCase):
143143
def setUp(self):
144144
self.model = BlogPost()
145145

146+
def test_in_operator_for_available_transitions(self):
147+
# store the generator in a list, so we can reuse the generator and do multiple asserts
148+
transitions = list(self.model.get_available_state_transitions())
149+
150+
self.assertIn("publish", transitions)
151+
self.assertNotIn("xyz", transitions)
152+
153+
# inline method for faking the name of the transition
154+
def publish():
155+
pass
156+
157+
obj = Transition(
158+
method=publish,
159+
source="",
160+
target="",
161+
on_error="",
162+
conditions="",
163+
permission="",
164+
custom="",
165+
)
166+
167+
self.assertTrue(obj in transitions)
168+
146169
def test_available_conditions_from_new(self):
147170
transitions = self.model.get_available_state_transitions()
148171
actual = set((transition.source, transition.target) for transition in transitions)

0 commit comments

Comments
 (0)