def __new__(cls, name, bases, attrs): """ Instantiation of the State type. When this type is created, also create a logging model if required. """ if name != 'StateModel' and 'Machine' in attrs: attrs['state'] = StateField(max_length=100, default='0', verbose_name=_('state id'), machine=attrs['Machine']) # Wrap __unicode__ for state model if '__unicode__' in attrs: old_unicode = attrs['__unicode__'] def new_unicode(self): return '%s (%s)' % (old_unicode(self), self.Machine.get_state( self.state).description) attrs['__unicode__'] = new_unicode # Call class constructor of parent return ModelBase.__new__(cls, name, bases, attrs)
def __new__(mcs, name, bases, attrs): """ Instantiation of the State type. When this type is created, also create a logging model if required. """ if name != "StateModel" and "Machine" in attrs: attrs["state"] = StateField( max_length=100, default="0", verbose_name=_("state id"), machine=attrs["Machine"], ) # Wrap __unicode__ for state model if "__str__" in attrs: old_unicode = attrs["__str__"] def new_unicode(self): return "{} ({})".format( old_unicode(self), self.Machine.get_state(self.state).description) attrs["__str__"] = new_unicode # Call class constructor of parent return ModelBase.__new__(mcs, name, bases, attrs)
class Execution(StateModel): operation_state = StateField(machine=ExecutionStateMachine, default='initiated') author = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='executions', on_delete=models.CASCADE) operation_instance = models.ForeignKey(Instance, related_name='executions', on_delete=models.CASCADE) callback_url = models.CharField(max_length=2048, blank=True, default='') force_spawn_cluster = models.CharField(max_length=8, blank=True, default='') creation_date = models.DateTimeField(auto_now_add=True) status = models.CharField(max_length=2048, blank=True, default='NEW') status_info = models.CharField(max_length=2048, blank=True, default='') output_location = models.CharField(max_length=2048, blank=True, default='') hints = models.CharField(max_length=2048, blank=True, default='{}') cluster_id = models.IntegerField(default=-1) resource_manager_agent_credentials = models.CharField(max_length=2048, blank=True, default='{}') operation_manager_agent_credentials = models.CharField(max_length=2048, blank=True, default='{}') ongoing_transition = models.BooleanField(default=False)
def __new__(cls, name, bases, attrs): """ Instantiation of the State type. When this type is created, also create a logging model if required. """ if name != 'StateModel' and 'Machine' in attrs: attrs['state'] = StateField(max_length=100, default='0', verbose_name=_('state id'), machine=attrs['Machine']) # Call class constructor of parent return ModelBase.__new__(cls, name, bases, attrs)
class Issue(models.Model): objects = IssueManager() solved = models.BooleanField(default=False) title = models.CharField(max_length=50) submitter = models.ForeignKey(User, related_name='submitted_issues') solver = models.ForeignKey(User, related_name='solved_issues', null=True) text_description = models.TextField() state = StateField(machine=IssueStates) category = models.ForeignKey(IssueCategory) created_on = models.DateTimeField(auto_now_add=True) solved_on = models.DateTimeField(null=True) def __str__(self): return self.title
class DjangoState2Class(models.Model): """Django Test Model implementing a State Machine used since django-states2""" field1 = models.IntegerField() field2 = models.CharField(max_length=25) state = StateField(machine=TestMachine)
class _StateTransition(models.Model, metaclass=_StateTransitionMeta): """ The log entries for :class:`~django_states.machine.StateTransition`. """ state = StateField( max_length=100, default="0", verbose_name=_("state id"), machine=StateTransitionMachine, ) from_state = models.CharField(max_length=100, choices=get_state_choices()) to_state = models.CharField(max_length=100, choices=get_state_choices()) user = models.ForeignKey( getattr(settings, "AUTH_USER_MODEL", "auth.User"), blank=True, null=True, on_delete=models.CASCADE, ) serialized_kwargs = models.TextField(blank=True) start_time = models.DateTimeField( auto_now_add=True, db_index=True, verbose_name=_("transition started at") ) on = models.ForeignKey( state_model, related_name=("{}_history".format(field_name)), on_delete=models.CASCADE, ) class Meta: """Non-field Options""" verbose_name = "{} transition".format(state_model._meta.verbose_name) # When the state class has been given an app_label, use # use this app_label as well for this StateTransition model. if hasattr(state_model._meta, "app_label"): app_label = state_model._meta.app_label @property def kwargs(self): """ The ``kwargs`` that were used when calling the state transition. """ if not self.serialized_kwargs: return {} return json.loads(self.serialized_kwargs) @property def completed(self): """ Was the transition completed? """ return self.state == "transition_completed" @property def state_transition_definition(self): """ Gets the :class:`django_states.machine.StateTransition` that was used. """ return machine.get_transition_from_states(self.from_state, self.to_state) @property def from_state_definition(self): """ Gets the :class:`django_states.machine.StateDefinition` from which we originated. """ return machine.get_state(self.from_state) @property def from_state_description(self): """ Gets the description of the :class:`django_states.machine.StateDefinition` from which we were originated. """ return self.from_state_definition.description @property def to_state_definition(self): """ Gets the :class:`django_states.machine.StateDefinition` to which we transitioning. """ return machine.get_state(self.to_state) @property def to_state_description(self): """ Gets the description of the :class:`django_states.machine.StateDefinition` to which we were transitioning. """ return self.to_state_definition.description def make_transition(self, transition, user=None): """ Execute state transition. Provide ``user`` to do permission checking. :param transition: Name of the transition :param user: User object """ return self.get_state_info().make_transition(transition, user=user) @property def is_public(self): """ Returns ``True`` when this state transition is defined public in the machine. """ return self.state_transition_definition.public @property def transition_description(self): """ Returns the description for this transition as defined in the :class:`django_states.machine.StateTransition` declaration of the machine. """ return self.state_transition_definition.description def __str__(self): return '<State transition on {0} at {1} from "{2}" to "{3}">'.format( state_model.__name__, self.start_time, self.from_state, self.to_state )
class _StateTransition(models.Model): """ The log entries for :class:`~django_states.machine.StateTransition`. """ __metaclass__ = _StateTransitionMeta state = StateField(max_length=100, default='0', verbose_name=_('state id'), machine=StateTransitionMachine) from_state = models.CharField(max_length=100, choices=get_state_choices()) to_state = models.CharField(max_length=100, choices=get_state_choices()) user = models.ForeignKey(User, blank=True, null=True) serialized_kwargs = models.TextField(blank=True) start_time = models.DateTimeField( auto_now_add=True, db_index=True, verbose_name=_('transition started at') ) on = models.ForeignKey(state_model, related_name=('%s_history' % field_name)) class Meta: """Non-field Options""" # noticed ImportError in some cases! https://code.djangoproject.com/ticket/15084 verbose_name = _('%s transition') % state_model._meta.verbose_name # When the state class has been given an app_label, use # use this app_label as well for this StateTransition model. if hasattr(state_model._meta, 'app_label'): app_label = state_model._meta.app_label @property def kwargs(self): """ The ``kwargs`` that were used when calling the state transition. """ if not self.serialized_kwargs: return {} return json.loads(self.serialized_kwargs) @property def completed(self): """ Was the transition completed? """ return self.state == 'transition_completed' @property def state_transition_definition(self): """ Gets the :class:`django_states.machine.StateTransition` that was used. """ return machine.get_transition_from_states(self.from_state, self.to_state) @property def from_state_definition(self): """ Gets the :class:`django_states.machine.StateDefinition` from which we originated. """ return machine.get_state(self.from_state) @property def from_state_description(self): """ Gets the description of the :class:`django_states.machine.StateDefinition` from which we were originated. """ return unicode(self.from_state_definition.description) @property def to_state_definition(self): """ Gets the :class:`django_states.machine.StateDefinition` to which we transitioning. """ return machine.get_state(self.to_state) @property def to_state_description(self): """ Gets the description of the :class:`django_states.machine.StateDefinition` to which we were transitioning. """ return unicode(self.to_state_definition.description) def make_transition(self, transition, user=None): """ Execute state transition. Provide ``user`` to do permission checking. :param transition: Name of the transition :param user: User object """ return self.get_state_info().make_transition(transition, user=user) @property def is_public(self): """ Returns ``True`` when this state transition is defined public in the machine. """ return self.state_transition_definition.public @property def transition_description(self): """ Returns the description for this transition as defined in the :class:`django_states.machine.StateTransition` declaration of the machine. """ return unicode(self.state_transition_definition.description) def __unicode__(self): return '<State transition on {0} at {1} from "{2}" to "{3}">'.format( state_model.__name__, self.start_time, self.from_state, self.to_state)
class DjangoStateLogClass(models.Model): """Django Test Model implementing a Logging State Machine""" field1 = models.IntegerField() field2 = models.CharField(max_length=25) state = StateField(machine=TestLogMachine)
class DjangoStateHandlerClass(models.Model): pending_handler_called = models.BooleanField(default=False) approved_handler_called = models.BooleanField(default=False) approve_handler_called = models.BooleanField(default=False) state = StateField(machine=TestHandlerMachine)