Exemple #1
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
    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
Exemple #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.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
Exemple #4
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
Exemple #5
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
Exemple #6
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
Exemple #7
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
Exemple #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):
            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
Exemple #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,
            "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
Exemple #10
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
Exemple #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))
        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
    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