def validate_available_choice(enum, to_value): """ Validate that to_value is defined as a value in enum. """ if to_value is not None and to_value not in list(dict(enum.choices()).keys()): message = _(six.text_type('Select a valid choice. {value} is not one of the available choices.')) raise InvalidStatusOperationError(message.format(value=to_value))
def set_enum(self, new_value): if new_value is models.NOT_PROVIDED: new_value = None if hasattr(self, private_att_name): # Fetch previous value from private enum attribute. old_value = getattr(self, private_att_name) else: # First setattr no previous value on instance. old_value = new_value # Update private enum attribute with new value if new_value is not None and not isinstance(new_value, enum): if isinstance(new_value, Enum): raise TypeError( "Invalid Enum class passed. Passed {}, expected {}". format(new_value.__class__.__name__, enum.__name__)) try: new_value = enum(new_value) except ValueError: raise InvalidStatusOperationError( ugettext( six.text_type( "{value!r} is not one of the available choices " "for enum {enum}.")).format(value=new_value, enum=enum)) setattr(self, private_att_name, new_value) self.__dict__[att_name] = new_value # Run validation for new value. validators.validate_valid_transition(enum, old_value, new_value)
def validate_valid_transition(enum, from_value, to_value): """ Validate that to_value is a valid choice and that to_value is a valid transition from from_value. """ validate_available_choice(enum, to_value) if hasattr(enum, '_transitions') and not enum.is_valid_transition(from_value, to_value): message = _('%(enum)s can not go from "%(from)s" to "%(to)s"' % {'enum': enum.__name__, 'from': enum.name(from_value), 'to': enum.name(to_value) or to_value}) raise InvalidStatusOperationError(message)
def validate_available_choice(enum, to_value): """ Validate that to_value is defined as a value in enum. """ if to_value is None: return if type(to_value) is not int: try: to_value = int(to_value) except ValueError: message_str = "'{value}' cannot be converted to int" message = _(six.text_type(message_str)) raise InvalidStatusOperationError(message.format(value=to_value)) if to_value not in list(dict(enum.choices()).keys()): message = _( six.text_type( 'Select a valid choice. {value} is not one of the available choices.' )) raise InvalidStatusOperationError(message.format(value=to_value))
def validate_valid_transition(enum, from_value, to_value): """ Validate that to_value is a valid choice and that to_value is a valid transition from from_value. """ validate_available_choice(enum, to_value) if hasattr(enum, '_transitions') and not enum.is_valid_transition(from_value, to_value): message = _(six.text_type('{enum} can not go from "{from_value}" to "{to_value}"')) raise InvalidStatusOperationError(message.format( enum=enum.__name__, from_value=enum.name(from_value), to_value=enum.name(to_value) or to_value ))
def validate_available_choice(enum, to_value): """ Validate that to_value is defined as a value in enum. Pass by name is not supported. """ if to_value is None: return try: enum(to_value) except ValueError: raise InvalidStatusOperationError( ugettext( six.text_type("{value!r} is not one of the available choices " "for enum {enum}.")).format(value=to_value, enum=enum))
def validate_available_choice(enum, to_value): """ Validate that to_value is defined as a value in enum. """ if to_value is not None and not to_value in dict(enum.choices()).keys(): raise InvalidStatusOperationError(_(u'Select a valid choice. %(value)s is not one of the available choices.') % {'value': to_value})