Columns

Common SQLAlchemy column types.

class keg_elements.db.columns.DBEnum(value)

Base class for all database enum types.

To create a new enum, subclass this, add the enum values, and implement db_name():

class MyEnum(DBEnum):
    option1 = 'Option 1'
    option2 = 'Option 2'

    @classmethod
    def db_name(cls):
        return 'my_enum_db_name'

To declare a DB column of this type:

class MyEntity(db.Model):
    option = sa.Column(MyEnum.db_type())

To set the choices on a form field:

class MyEntityForm(wtforms.Form):
    option = wtforms.SelectField(
        'Option',
        choices=MyEnum.form_options()
        coerce=MyEnum.coerce
    )

If using ModelForm the field will be configured with the above options automatically:

class MyEntityForm(ModelForm):
    class Meta:
        model = MyEntity
class keg_elements.db.columns.EncryptedUnicode(*args, **kwargs)

Unicode column type that encrypts value with the given key for persistance to storage.

Parameters:
  • key – A bytes object containing the encryption key or a callable that returns the key

  • encrypt – A callable that takes a unicode string and the encryption key as arguments and returns the encrypted data as a bytes object.

  • decrypt – A callable that takes a bytes object and the encryption key as arguments and returns the decrypted data as a unicode string.

impl

alias of UnicodeText

process_bind_param(value, dialect)

Receive a bound parameter value to be converted.

Custom subclasses of _types.TypeDecorator should override this method to provide custom behaviors for incoming data values. This method is called at statement execution time and is passed the literal Python data value which is to be associated with a bound parameter in the statement.

The operation could be anything desired to perform custom behavior, such as transforming or serializing data. This could also be used as a hook for validating logic.

Parameters:
  • value – Data to operate upon, of any type expected by this method in the subclass. Can be None.

  • dialect – the Dialect in use.

See also

types_typedecorator

_types.TypeDecorator.process_result_value()

process_result_value(value, dialect)

Receive a result-row column value to be converted.

Custom subclasses of _types.TypeDecorator should override this method to provide custom behaviors for data values being received in result rows coming from the database. This method is called at result fetching time and is passed the literal Python data value that’s extracted from a database result row.

The operation could be anything desired to perform custom behavior, such as transforming or deserializing data.

Parameters:
  • value – Data to operate upon, of any type expected by this method in the subclass. Can be None.

  • dialect – the Dialect in use.

See also

types_typedecorator

_types.TypeDecorator.process_bind_param()

class keg_elements.db.columns.TimeZoneType(length=None, **kwargs)

A column type for time zones, stored as a unicode string.