Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Re-add save parameter to @transition decorator as a feature #79

Closed
zenefitsbob opened this issue May 1, 2015 · 8 comments
Closed

Re-add save parameter to @transition decorator as a feature #79

zenefitsbob opened this issue May 1, 2015 · 8 comments

Comments

@zenefitsbob
Copy link

I looked through the history and it looks like this used to be a feature. Why was it removed? I would like to use something like this for my own project.

@transition(... save=True)

Thanks for providing a nice library for me to use! :)

@zenefitsbob zenefitsbob changed the title Add save parameter to @transition decorator back as a feature Re-add save parameter to @transition decorator as a feature May 1, 2015
@kmmbvnr
Copy link
Collaborator

kmmbvnr commented May 1, 2015

There are no advantages of having that inside transition decorator.

Just call .save() explicitly.

@kmmbvnr kmmbvnr closed this as completed May 1, 2015
@zenefitsbob
Copy link
Author

The advantage is more concise code:

post.publish()

Instead of:

post.publish()
post.save()

Am I missing something?

@kmmbvnr
Copy link
Collaborator

kmmbvnr commented May 1, 2015

Concise is not an advantage.

Without explicit save you have no information is model saved or not, till take a look to models.py file.
This also makes hard to implement generic actions (it would save things twice)

@tachang
Copy link

tachang commented Oct 9, 2015

I found it pretty useful though because sometimes I am in the shell and need to transition an element. Also I sometimes forget to save but I know I really want to transition a model.

@variable
Copy link

variable commented Sep 18, 2017

class Cart(TimestampModel):
    class STATE(object):
        NEW = 'new'
        LOCKED = 'locked'

    state = FSMField(default=STATE.NEW)

    @property
    def total(self):
        return sum(item.total for item in self.items.all())

    def create_order(self, data={}):
        assert self.items.count()
        # locks the cart itself and save state
        # https://github.com/kmmbvnr/django-fsm/issues/79
        self.lock()
        self.save()
        order = Order.objects.create(cart=self, total=self.total, data=data)
        return order

    @transition(field=state, source=STATE.NEW, target=STATE.LOCKED)
    def lock(self):
        # https://github.com/kmmbvnr/django-fsm/issues/79
        # would have been nice if this feature is accepted
        # so we don't have to have a dummy function just to change state...
        pass

if we have the save param, I can simply do

@transition(field=state, source=STATE.NEW, target=STATE.LOCKED)
def create_order(self, data={}):
        assert self.items.count()
        order = Order.objects.create(cart=self, total=self.total, data=data)
        return order

@kmmbvnr
Copy link
Collaborator

kmmbvnr commented Sep 19, 2017

If I'll have a time to few more BSD licensed code, the next version of django-fsm would set state before function entry, like I did in the viewflow - https://github.com/viewflow/viewflow/blob/master/viewflow/fsm.py#L165

That would allow writing just

@transition(field=state, source=STATE.NEW, target=STATE.LOCKED)
def lock(self):
    this.save()

@oxalorg
Copy link

oxalorg commented Apr 30, 2020

the next version of django-fsm would set state before function entry

@kmmbvnr Thank you for everything, but was this implemented? Can we use self.save() inside the transition function?

@kmmbvnr
Copy link
Collaborator

kmmbvnr commented May 1, 2020

@oxalorg i'm working on new version of viewflow that would contain new version of state management. The main difference that fsm became decoupled from django models and composition used to make it plays together

no ETA on release though (

Wrapping Django Models — Viewflow  low code development platform

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants