rsinger86/django-lifecycle

Possible for was_not and is_now to take in multiple values?

Closed this issue · 2 comments

Hi I have a case where the same implementation, but I hope not to repeat myself.

Instead of having two sets of

@hook(
        AFTER_UPDATE,
        when="status",
        was_not="rejected_by_vendor",
        is_now="rejected_by_vendor"
    )
    def change_lineitems_to_quotation_rejected(self):
        # blah

@hook(
        AFTER_UPDATE,
        when="status",
        was_not="rejected_by_customer",
        is_now="rejected_by_customer"
    )
    def same_change_lineitems_to_quotation_rejected(self):
        # same blah

I hope to have

@hook(
        AFTER_UPDATE,
        when="status",
        was_not_any=["rejected_by_vendor", "rejected_by_customer"],
        is_now_any=["rejected_by_vendor", "rejected_by_customer"]
    )
    def same_change_lineitems_to_quotation_rejected(self):
        # same blah

Can help?

Currently I chose

def change_lineitems_to_quotation_rejected_because_customer(self):
   # the blah is here

@hook(
        AFTER_UPDATE,
        when="status",
        was_not="rejected_by_vendor",
        is_now="rejected_by_vendor"
    )
    def change_lineitems_to_quotation_rejected_because_vendor(self):
          self.change_lineitems_to_quotation_rejected()

@hook(
        AFTER_UPDATE,
        when="status",
        was_not="rejected_by_customer",
        is_now="rejected_by_customer"
    )
    def change_lineitems_to_quotation_rejected_because_customer(self):
        self.change_lineitems_to_quotation_rejected()

Hm, I'll have to think more about this, but I can see this possibly being a nice addition.

You can also stack decorators to avoid duplicate function defs:

@hook(
        AFTER_UPDATE,
        when="status",
        was_not="rejected_by_vendor",
        is_now="rejected_by_vendor"
    )
@hook(
        AFTER_UPDATE,
        when="status",
        was_not="rejected_by_customer",
        is_now="rejected_by_customer"
    )
    def change_lineitems_to_quotation_rejected_because_vendor(self):
          self.change_lineitems_to_quotation_rejected()

Oh in that case

I'm happy just to stack decorators