Beispiel #1
0
            def _change_state(instance, *args, **kwargs):
                meta = func._django_fsm
                if not (meta.has_transition(instance) and  meta.conditions_met(instance)):
                    raise TransitionNotAllowed("Can't switch from state '%s' using method '%s'" % (meta.current_state(instance), func.func_name))

                source_state = meta.current_state(instance)
                pre_transition.send(
                    sender = instance.__class__,
                    instance = instance,
                    name = func.func_name,
                    source = source_state,
                    target = meta.next_state(instance))
 	
                result = func(instance, *args, **kwargs)

                meta.to_next_state(instance)
                if save:
                    instance.save()

                post_transition.send(
                    sender = instance.__class__,
                    instance = instance,
                    name = func.func_name,
                    source = source_state,
                    target = meta.current_state(instance))
                return result
Beispiel #2
0
def default_status_check(sender, instance, **kwargs):
    if not isinstance(instance, OrderPayment):
        return

    # Send status change notification when record first created
    # This is to ensure any components listening for a status
    # on the Sender will also receive the initial status.

    # Get the default status for the status field on Sender
    default_status = StatusDefinition.CREATED

    from bluebottle.payments_logger.adapters import PaymentLogAdapter

    # adding a Log when the status changes
    payment_logger = PaymentLogAdapter()

    try:
        # if there is no Payment associated to the order_payment do not log
        # The log will be created in the adapter
        payment = Payment.objects.get(order_payment=instance)
        payment_logger.log(payment, "info", "a new payment status {0}".format(instance.status))

    except Payment.DoesNotExist:
        pass
    except Payment.MultipleObjectsReturned:
        payment = Payment.objects.order("-created").filter(order_payment=instance).all()[0]
        payment_logger.log(payment, "info", "a new payment status {0}".format(instance.status))
    finally:
        # Signal new status if current status is the default value
        if instance.status == default_status:
            signal_kwargs = {"sender": sender, "instance": instance, "target": instance.status}
            post_transition.send(**signal_kwargs)
Beispiel #3
0
            def _change_state(instance, *args, **kwargs):
                meta = func._django_fsm
                if not (meta.has_transition(instance)
                        and meta.conditions_met(instance)):
                    raise TransitionNotAllowed(
                        "Can't switch from state '%s' using method '%s'" %
                        (meta.current_state(instance), func.__name__))

                source_state = meta.current_state(instance)

                pre_transition.send(sender=instance.__class__,
                                    instance=instance,
                                    name=func.__name__,
                                    source=source_state,
                                    target=meta.next_state(instance))

                result = func(instance, *args, **kwargs)

                meta.to_next_state(instance)
                if save:
                    instance.save()

                post_transition.send(sender=instance.__class__,
                                     instance=instance,
                                     name=func.__name__,
                                     source=source_state,
                                     target=meta.current_state(instance))
                return result
Beispiel #4
0
    def change_state(self, instance, method, *args, **kwargs):
        meta = method._django_fsm
        method_name = method.__name__
        current_state = self.get_state(instance)

        if not (meta.has_transition(current_state) and meta.conditions_met(instance, current_state)):
            raise TransitionNotAllowed(
                "Can't switch from state '{}' using method '{}'".format(current_state, method_name))

        next_state = meta.next_state(current_state)

        pre_transition.send(
            sender=instance.__class__,
            instance=instance,
            name=method_name,
            source=current_state,
            target=next_state)

        result = method(instance, *args, **kwargs)

        if next_state:
            self.set_state(instance, next_state)

        post_transition.send(
            sender=instance.__class__,
            instance=instance,
            name=method_name,
            source=current_state,
            target=next_state)

        return result
Beispiel #5
0
            def _change_state(instance, *args, **kwargs):
                meta = func._django_fsm
                if not (meta.has_transition(instance) and  meta.conditions_met(instance)):
                    raise TransitionNotAllowed("Can't switch from state '%s' using method '%s'" % (meta.current_state(instance), func.func_name))
                fieldname = field or meta._get_state_field(instance).name

                source_state = meta.current_state(instance)
                pre_transition.send(
                    sender = instance.__class__,
                    instance = instance,
                    name = func.func_name,
                    source = source_state,
                    field = fieldname,
                    target = meta.next_state(instance))
 	
                result = func(instance, *args, **kwargs)

                meta.to_next_state(instance)
                if save:
                    instance.save()

                post_transition.send(
                    sender = instance.__class__,
                    instance = instance,
                    name = func.func_name,
                    source = source_state,
                    field = fieldname,
                    target = meta.current_state(instance))
                
                if save and hasattr(instance, '_django_fsm_audits'):
                    # The audit logs are created during `post_transition`
                    for audit in instance._django_fsm_audits:
                        audit.save()
                return result
Beispiel #6
0
    def change_state(self, instance, method, *args, **kwargs):
        meta = method._django_fsm
        method_name = method.__name__
        current_state = self.get_state(instance)

        if not (meta.has_transition(current_state)
                and meta.conditions_met(instance, current_state)):
            raise TransitionNotAllowed(
                "Can't switch from state '{}' using method '{}'".format(
                    current_state, method_name))

        next_state = meta.next_state(current_state)

        signal_kwargs = {
            'sender': instance.__class__,
            'instance': instance,
            'name': method_name,
            'source': current_state,
            'target': next_state
        }

        pre_transition.send(**signal_kwargs)

        result = method(instance, *args, **kwargs)
        if next_state:
            self.set_state(instance, next_state)

        post_transition.send(**signal_kwargs)

        return result
Beispiel #7
0
def default_status_check(sender, instance, **kwargs):
    if not (isinstance(instance, Payment) or isinstance(instance, OrderPayment)): return

    # Send status change notification when record first created
    # This is to ensure any components listening for a status 
    # on the Sender will also receive the initial status.

    # Get the default status for the status field on Sender
    default_status = sender._meta.get_field_by_name('status')[0].get_default()

    from bluebottle.payments_logger.adapters import PaymentLogAdapter

    # adding a Log when the status changes
    payment_logger = PaymentLogAdapter()

    try:
        # if there is no Payment associated to the order_payment do not log
        # The log will be created in the adapter
        payment = Payment.objects.get(order_payment=instance)
        payment_logger.log(payment, 'info', 'a new payment status {0}'.format(instance.status))

    except Payment.DoesNotExist:
        pass
    except Payment.MultipleObjectsReturned:
        payment = Payment.objects.order('-created').filter(order_payment=instance).all()[0]
        payment_logger.log(payment, 'info', 'a new payment status {0}'.format(instance.status))
    finally:
        # Signal new status if current status is the default value
        if (instance.status == default_status):
            signal_kwargs = {
                'sender': sender,
                'instance': instance,
                'target': instance.status
            }
            post_transition.send(**signal_kwargs)
Beispiel #8
0
    def change_state(self, instance, method, *args, **kwargs):
        meta = method._django_fsm
        method_name = method.__name__
        current_state = self.get_state(instance)

        if not (meta.has_transition(current_state) and meta.conditions_met(instance, current_state)):
            raise TransitionNotAllowed(
                "Can't switch from state '{}' using method '{}'".format(current_state, method_name))

        next_state = meta.next_state(current_state)

        signal_kwargs = {
            'sender': instance.__class__,
            'instance': instance,
            'name': method_name,
            'source': current_state,
            'target': next_state
        }

        pre_transition.send(**signal_kwargs)

        result = method(instance, *args, **kwargs)
        if next_state:
            self.set_proxy(instance, next_state)
            self.set_state(instance, next_state)

        post_transition.send(**signal_kwargs)

        return result
Beispiel #9
0
    def change_state(self, instance, method, *args, **kwargs):
        meta = method._django_fsm
        method_name = method.__name__
        current_state = self.get_state(instance)

        if not meta.has_transition(current_state):
            raise TransitionNotAllowed(
                "Can't switch from state '{0}' using method '{1}'".format(current_state, method_name),
                object=instance,
                method=method,
            )
        if not meta.conditions_met(instance, current_state):
            raise TransitionNotAllowed(
                "Transition conditions have not been met for method '{0}'".format(method_name),
                object=instance,
                method=method,
            )

        next_state = meta.next_state(current_state)

        signal_kwargs = {
            'sender': instance.__class__,
            'instance': instance,
            'name': method_name,
            'field': meta.field,
            'source': current_state,
            'target': next_state,
            'method_args': args,
            'method_kwargs': kwargs,
        }

        pre_transition.send(**signal_kwargs)

        # Add states to func kwargs
        kwargs['fsm_current_state'] = current_state
        kwargs['fsm_next_state'] = next_state

        try:
            result = method(instance, *args, **kwargs)
            if next_state is not None:
                if hasattr(next_state, 'get_state'):
                    next_state = next_state.get_state(instance, transition, result, args=args, kwargs=kwargs)
                    signal_kwargs['target'] = next_state
                self.set_proxy(instance, next_state)
                self.set_state(instance, next_state)
        except Exception as exc:
            exception_state = meta.exception_state(current_state)
            if exception_state:
                self.set_proxy(instance, exception_state)
                self.set_state(instance, exception_state)
                signal_kwargs['target'] = exception_state
                signal_kwargs['exception'] = exc
                post_transition.send(**signal_kwargs)
            raise
        else:
            post_transition.send(**signal_kwargs)

        return result
Beispiel #10
0
def order_payment_changed(sender, instance, **kwargs):
    # Send status change notification when record first created
    # This is to ensure any components listening for a status
    # on an OrderPayment will also receive the initial status.

    default_status = StatusDefinition.CREATED

    # Signal new status if current status is the default value
    if instance.status == default_status:
        signal_kwargs = {"sender": sender, "instance": instance, "target": instance.status}
        post_transition.send(**signal_kwargs)
Beispiel #11
0
    def change_state(self, instance, method, *args, **kwargs):
        meta = method._django_fsm
        method_name = method.__name__
        current_state = self.get_state(instance)

        if not meta.has_transition(current_state):
            raise TransitionNotAllowed(
                "Can't switch from state '{0}' using method '{1}'".format(current_state, method_name),
                object=instance, method=method)
        if not meta.conditions_met(instance, current_state):
            raise TransitionNotAllowed(
                "Transition conditions have not been met for method '{0}'".format(method_name),
                object=instance, method=method)

        next_state = meta.next_state(current_state)

        signal_kwargs = {
            'sender': instance.__class__,
            'instance': instance,
            'name': method_name,
            'field': meta.field,
            'source': current_state,
            'target': next_state,
            'method_args': args,
            'method_kwargs': kwargs
        }

        pre_transition.send(**signal_kwargs)

        try:
            result = method(instance, *args, **kwargs)
            if next_state is not None:
                if hasattr(next_state, 'get_state'):
                    next_state = next_state.get_state(
                        instance, transition, result,
                        args=args, kwargs=kwargs)
                    signal_kwargs['target'] = next_state
                self.set_proxy(instance, next_state)
                self.set_state(instance, next_state)
        except Exception as exc:
            exception_state = meta.exception_state(current_state)
            if exception_state:
                self.set_proxy(instance, exception_state)
                self.set_state(instance, exception_state)
                signal_kwargs['target'] = exception_state
                signal_kwargs['exception'] = exc
                post_transition.send(**signal_kwargs)
            raise
        else:
            post_transition.send(**signal_kwargs)

        return result
Beispiel #12
0
def order_payment_changed(sender, instance, **kwargs):
    # Send status change notification when record first created
    # This is to ensure any components listening for a status
    # on an OrderPayment will also receive the initial status.

    default_status = StatusDefinition.CREATED

    # Signal new status if current status is the default value
    if instance.status == default_status:
        signal_kwargs = {
            'sender': sender,
            'instance': instance,
            'target': instance.status
        }
        post_transition.send(**signal_kwargs)
Beispiel #13
0
    def change_state(self, instance, method, *args, **kwargs):
        meta = method._django_fsm
        method_name = method.__name__
        current_state = self.get_state(instance)

        if not meta.has_transition(current_state):
            raise TransitionNotAllowed(
                "Can't switch from state '{0}' using method '{1}'".format(current_state, method_name),
                object=instance,
                method=method,
            )
        if not meta.conditions_met(instance, current_state):
            raise TransitionNotAllowed(
                "Transition conditions have not been met for method '{0}'".format(method_name),
                object=instance,
                method=method,
            )

        next_state = meta.next_state(current_state)

        signal_kwargs = {
            "sender": instance.__class__,
            "instance": instance,
            "name": method_name,
            "source": current_state,
            "target": next_state,
        }

        pre_transition.send(**signal_kwargs)

        try:
            result = method(instance, *args, **kwargs)
            if next_state is not None:
                self.set_proxy(instance, next_state)
                self.set_state(instance, next_state)
        except Exception as exc:
            exception_state = meta.exception_state(current_state)
            if exception_state:
                self.set_proxy(instance, exception_state)
                self.set_state(instance, exception_state)
                signal_kwargs["target"] = exception_state
                signal_kwargs["exception"] = exc
                post_transition.send(**signal_kwargs)
            raise
        else:
            post_transition.send(**signal_kwargs)

        return result
Beispiel #14
0
def order_payment_changed(sender, instance, **kwargs):
    # Send status change notification when record first created
    # This is to ensure any components listening for a status 
    # on an OrderPayment will also receive the initial status.

    # Get the default status for the status field on OrderPayment
    default_status = OrderPayment._meta.get_field_by_name('status')[0].get_default()

    # Signal new status if current status is the default value
    if (instance.status == default_status):
        signal_kwargs = {
            'sender': sender,
            'instance': instance,
            'target': instance.status
        }
        post_transition.send(**signal_kwargs)
Beispiel #15
0
    def change_state(self, instance, method, *args, **kwargs):
        meta = method._django_fsm
        method_name = method.__name__
        current_state = self.get_state(instance)

        if not meta.has_transition(current_state):
            raise TransitionNotAllowed(
                "Can't switch from state '{0}' using method '{1}'".format(
                    current_state, method_name))
        if not meta.conditions_met(instance, current_state):
            raise TransitionNotAllowed(
                "Transition conditions have not been met for method '{0}'".
                format(method_name))

        next_state = meta.next_state(current_state)

        signal_kwargs = {
            'sender': instance.__class__,
            'instance': instance,
            'name': method_name,
            'source': current_state,
            'target': next_state
        }

        pre_transition.send(**signal_kwargs)

        try:
            result = method(instance, *args, **kwargs)
            if next_state is not None:
                self.set_proxy(instance, next_state)
                self.set_state(instance, next_state)
        except Exception as exc:
            exception_state = meta.exception_state(current_state)
            if exception_state:
                self.set_proxy(instance, exception_state)
                self.set_state(instance, exception_state)
                signal_kwargs['target'] = exception_state
                signal_kwargs['exception'] = exc
                post_transition.send(**signal_kwargs)
            raise
        else:
            post_transition.send(**signal_kwargs)

        return result
Beispiel #16
0
    def change_state(self, instance, method, *args, **kwargs):
        meta = method._django_fsm
        method_name = method.__name__
        current_state = self.get_state(instance)

        if not meta.has_transition(current_state):
            raise TransitionNotAllowed(
                "Can't switch from state '{0}' using method '{1}'".format(current_state, method_name))
        if not meta.conditions_met(instance, current_state):
            raise TransitionNotAllowed(
                "Transition conditions have not been met for method '{0}'".format(method_name))

        next_state = meta.next_state(current_state)

        signal_kwargs = {
            'sender': instance.__class__,
            'instance': instance,
            'name': method_name,
            'source': current_state,
            'target': next_state
        }
        signal_kwargs.update(**kwargs)

        pre_transition.send(**signal_kwargs)

        try:
            result = method(instance, *args, **kwargs)
            if next_state:
                self.set_proxy(instance, next_state)
                self.set_state(instance, next_state)
        except Exception as exc:
            exception_state = meta.exception_state(current_state)
            if exception_state:
                self.set_proxy(instance, exception_state)
                self.set_state(instance, exception_state)
                signal_kwargs['target'] = exception_state
                signal_kwargs['exception'] = exc
                post_transition.send(**signal_kwargs)
            raise
        else:
            post_transition.send(**signal_kwargs)

        return result
Beispiel #17
0
def default_status_check(sender, instance, **kwargs):
    if not isinstance(instance, OrderPayment):
        return

    # Send status change notification when record first created
    # This is to ensure any components listening for a status
    # on the Sender will also receive the initial status.

    # Get the default status for the status field on Sender
    default_status = StatusDefinition.CREATED

    from bluebottle.payments_logger.adapters import PaymentLogAdapter

    # adding a Log when the status changes
    payment_logger = PaymentLogAdapter()

    try:
        # if there is no Payment associated to the order_payment do not log
        # The log will be created in the adapter
        payment = Payment.objects.get(order_payment=instance)
        payment_logger.log(payment, 'info',
                           'a new payment status {0}'.format(instance.status))

    except Payment.DoesNotExist:
        pass
    except Payment.MultipleObjectsReturned:
        payment = Payment.objects.order('-created').filter(
            order_payment=instance).all()[0]
        payment_logger.log(payment, 'info',
                           'a new payment status {0}'.format(instance.status))
    finally:
        # Signal new status if current status is the default value
        if (instance.status == default_status):
            signal_kwargs = {
                'sender': sender,
                'instance': instance,
                'target': instance.status
            }
            post_transition.send(**signal_kwargs)
Beispiel #18
0
    def change_state(self, instance, method, *args, **kwargs):
        meta = method._django_fsm
        method_name = method.__name__
        current_state = self.get_state(instance)

        if not meta.has_transition(current_state):
            raise TransitionNotAllowed(
                "Can't switch from state '{0}' using method '{1}'".format(
                    current_state, method_name),
                object=instance,
                method=method)
        if not meta.conditions_met(instance, current_state):
            raise TransitionNotAllowed(
                "Transition conditions have not been met for method '{0}'".
                format(method_name),
                object=instance,
                method=method)

        next_state = meta.next_state(current_state)

        signal_kwargs = {
            'sender': instance.__class__,
            'instance': instance,
            'name': method_name,
            'field': meta.field,
            'source': current_state,
            'target': next_state,
            'method_args': args,
            'method_kwargs': kwargs
        }

        pre_transition.send(**signal_kwargs)

        try:
            result = method(instance, *args, **kwargs)
            if next_state is not None:
                if hasattr(next_state, 'get_state'):
                    next_state = next_state.get_state(instance,
                                                      transition,
                                                      result,
                                                      args=args,
                                                      kwargs=kwargs)
                    signal_kwargs['target'] = next_state
                self.set_proxy(instance, next_state)
                self.set_state(instance, next_state)
        except Exception as exc:
            exception_state = meta.exception_state(current_state)
            transition_error = FSMTransitionError(exc)
            if exception_state:
                result = None
                with transaction.atomic():

                    self.set_proxy(instance, exception_state)
                    self.set_state(instance, exception_state)
                    setattr(instance, '__django_fsm_log_attr_description',
                            str(transition_error))
                    instance.save(force_update=True)
                    signal_kwargs['target'] = exception_state
                    signal_kwargs['exception'] = exc

                    post_transition.send(**signal_kwargs)

                    def _raise():
                        raise transition_error

                    transaction.on_commit(_raise)

            else:
                raise transition_error
        else:
            post_transition.send(**signal_kwargs)

        return result