pywebtools.sqlalchemy – Utilities for use with SQLAlchemy

This module provide the generic DBSession scoped_session for use with Pyramid & SQLAlchemy. It also provides the Base declarative_base() that acts as the base class for any declarative model. If you are starting from a SQLAlchemy + Pyramid starter template in your models module, remove the DBSession and Base and replace them imports from this module. Then do the same for the main function in the main package.

exception pywebtools.sqlalchemy.DBUpgradeException(current, required)

The DBUpgradeException is used to indicate that the database requires an upgrade before the Web Teaching Environment system can be used.

class pywebtools.sqlalchemy.JSONUnicodeText(*args, **kwargs)

The class:~pywebtools.sqlalchemy.JSONUnicodeText is an extension to the UnicodeText column type that does automatic conversion from the JSON string representation stored in the DB to a dict/list representation for use in python.


alias of UnicodeText

process_bind_param(value, dialect)

Convert the dict/list to JSON for storing.

process_result_value(value, dialect)

Convert the JSON to dict/list for use.

class pywebtools.sqlalchemy.MutableDict(*args, **kwargs)

The MutableDict is a dict extension for use with the JSONUnicodeText column. It monitors any change to its values and marks the column as dirty, if a change has occurred.

It is smart about its internal structure and will convert any nested dict into MutableDict as well, to ensure that all changes are tracked.

classmethod coerce(key, value)

Automatically coerce any dict to a MutableDict. Used by SQLAlchemy.

pywebtools.sqlalchemy.check_database_version(db_version, dbsession=None)

Checks that the current version of the database matches the version specified by db_version. This requires the use of the Alembic database migration library.

  • db_version (str) – The version identifier to check.
  • dbsession (scoped_session()) – The database session to use for database access. If None will create a new session.