Esempio n. 1
0
    def contribute_to_class(self, cls, name):
        """
        Adds methods to the :class:`~django.db.models.Model`.

        The extra methods will be added for each :class:`StateField` in a
        model:

        - :meth:`~django_states.model_methods.get_STATE_transitions`
        - :meth:`~django_states.model_methods.get_public_STATE_transitions`
        - :meth:`~django_states.model_methods.get_STATE_info`
        - :meth:`~django_states.model_methods.get_STATE_machine`
        """
        super(StateField, self).contribute_to_class(cls, name)

        if cls._meta.abstract:
            # Do not contribute to abstract models
            return
        # Set choice options (for combo box)
        self._choices = self._machine.get_state_choices()
        self.default = self._machine.initial_state

        # Do we need logging?
        # For Django 1.7: the migrations framework creates copies for all
        #                 the models, placing them all in a module name
        #                 "__fake__". Of course, for Django, for each module,
        #                 the names should be unique, so that wouldn't work.
        #                 We decide just to not have a logging model for the
        #                 migrations.
        # https://github.com/django/django/blob/f2ddc439b1938acb6cae693bda9d8cf83a4583be/django/db/migrations/state.py#L316
        if self._machine.log_transitions and cls.__module__ != '__fake__':
            from django_states.log import _create_state_log_model
            log_model = _create_state_log_model(cls, name, self._machine)
        else:
            log_model = None

        setattr(cls, '_%s_log_model' % name, log_model)

        # adding extra methods
        setattr(cls, 'get_%s_display' % name,
            curry(get_STATE_display, field=name, machine=self._machine))
        setattr(cls, 'get_%s_transitions' % name,
            curry(get_STATE_transitions, field=name))
        setattr(cls, 'get_public_%s_transitions' % name,
            curry(get_public_STATE_transitions, field=name))
        setattr(cls, 'get_%s_info' % name,
            curry(get_STATE_info, field=name, machine=self._machine))
        setattr(cls, 'get_%s_machine' % name,
            curry(get_STATE_machine, field=name, machine=self._machine))

        models.signals.class_prepared.connect(self.finalize, sender=cls)
Esempio n. 2
0
    def contribute_to_class(self, cls, name):
        """
        Adds methods to the :class:`~django.db.models.Model`.

        The extra methods will be added for each :class:`StateField` in a
        model:

        - :meth:`~django_states.model_methods.get_STATE_transitions`
        - :meth:`~django_states.model_methods.get_public_STATE_transitions`
        - :meth:`~django_states.model_methods.get_STATE_info`
        - :meth:`~django_states.model_methods.get_STATE_machine`
        """
        super(StateField, self).contribute_to_class(cls, name)

        if cls._meta.abstract:
            # Do not contribute to abstract models
            return
        # Set choice options (for combo box)
        self._choices = self._machine.get_state_choices()
        self.default = self._machine.initial_state

        # Do we need logging?
        # For Django 1.7: the migrations framework creates copies for all
        #                 the models, placing them all in a module name
        #                 "__fake__". Of course, for Django, for each module,
        #                 the names should be unique, so that wouldn't work.
        #                 We decide just to not have a logging model for the
        #                 migrations.
        # https://github.com/django/django/blob/f2ddc439b1938acb6cae693bda9d8cf83a4583be/django/db/migrations/state.py#L316
        if self._machine.log_transitions and cls.__module__ != '__fake__':
            from django_states.log import _create_state_log_model
            log_model = _create_state_log_model(cls, name, self._machine)
        else:
            log_model = None

        setattr(cls, '_%s_log_model' % name, log_model)

        # adding extra methods
        setattr(cls, 'get_%s_display' % name,
                curry(get_STATE_display, field=name, machine=self._machine))
        setattr(cls, 'get_%s_transitions' % name,
                curry(get_STATE_transitions, field=name))
        setattr(cls, 'get_public_%s_transitions' % name,
                curry(get_public_STATE_transitions, field=name))
        setattr(cls, 'get_%s_info' % name,
                curry(get_STATE_info, field=name, machine=self._machine))
        setattr(cls, 'get_%s_machine' % name,
                curry(get_STATE_machine, field=name, machine=self._machine))

        models.signals.class_prepared.connect(self.finalize, sender=cls)
Esempio n. 3
0
    def contribute_to_class(self, cls, name):
        """
        Adds methods to the :class:`~django.db.models.Model`.

        The extra methods will be added for each :class:`StateField` in a
        model:

        - :meth:`~django_states.model_methods.get_STATE_transitions`
        - :meth:`~django_states.model_methods.get_public_STATE_transitions`
        - :meth:`~django_states.model_methods.get_STATE_info`
        - :meth:`~django_states.model_methods.get_STATE_machine`
        """
        super(StateField, self).contribute_to_class(cls, name)

        # Set choice options (for combo box)
        self._choices = self._machine.get_state_choices()
        self.default = self._machine.initial_state

        # do we need logging?
        if self._machine.log_transitions:
            from django_states.log import _create_state_log_model
            log_model = _create_state_log_model(cls, name, self._machine)
        else:
            log_model = None

        setattr(cls, '_%s_log_model' % name, log_model)

        # adding extra methods
        setattr(cls, 'get_%s_display' % name,
            curry(get_STATE_display, field=name, machine=self._machine))
        setattr(cls, 'get_%s_transitions' % name,
            curry(get_STATE_transitions, field=name))
        setattr(cls, 'get_public_%s_transitions' % name,
            curry(get_public_STATE_transitions, field=name))
        setattr(cls, 'get_%s_info' % name,
            curry(get_STATE_info, field=name, machine=self._machine))
        setattr(cls, 'get_%s_machine' % name,
            curry(get_STATE_machine, field=name, machine=self._machine))

        models.signals.class_prepared.connect(self.finalize, sender=cls)