django-fsm icon indicating copy to clipboard operation
django-fsm copied to clipboard

Implement Transition.__hash__ and __eq__ for 'in' operator

Open foarsitter opened this issue 2 years ago • 3 comments

Hello Folks,

For managing te state of your models in a Django project this library is a perfect fit. So thanks for providing this excellent library.

I find myself looping a lot over the results of get_available_FIELD_transitions to check if an transition is available.

Therefore I decided to make the Transition object hashable by implementing the __hash__ and __eq__ methods.

This adds support for a clean way of checking if a transition is available in a certain context

if 'publish' in blog.get_available_state_transitions():
    do_logic()

Looking forward to receive your feedback.

foarsitter avatar May 25 '22 18:05 foarsitter

each transition belong to a FSMMeta instance. I would be great to make sure that we don't conflate different transitions that share the name but are attached to different state machines.

ticosax avatar Jun 02 '22 15:06 ticosax

Hi @ticosax, thanks for your feedback. We can extend the __eq__ ~~method to incorporate the FSMMeta~~, although i'm not familiar with the situations wherein transitions have the same name (a class method is unique right?).

How can I create an use case that matches what you are describing so I can add a test case for it?

foarsitter avatar Jun 03 '22 06:06 foarsitter

You are right, name is not overridable (unlike I assumed it was a parameter). then, there couldn't be name collision My bad, thanks.

    @property
    def name(self):
        return self.method.__name__

ticosax avatar Jun 03 '22 07:06 ticosax

@kmmbvnr thanks!

foarsitter avatar Dec 21 '23 09:12 foarsitter