How to install Khufu-SQLAHelper
- Download and install ActivePython
- Open Command Prompt
- Type
pypm install khufu-sqlahelper
Lastest release
Khufu-SQLAHelper is meant to reduce the plumbing required to configure a SQLAlchemy based database connection with a Pyramid based web app.
Requirements
- Python >= 2.6 (not tested with Python 3.x series)
Requirements Satisfied by Installing
NOTE 1: The SQLAHelper middleware includes the repoze.tm2 middleware NOTE 2: The SQLAHelper middleware requires that the app being wrapped was generated by Pyramid
Usage
Database Configuration
To use SQLAHelper database configuration there are two steps:
- Hook up the configuration via configurator.include('khufu.sqlahelper')
- Wrap the web app in the SQLAHelper middleware with khufu.sqlahelper.with_db(pyramid_app)
Once inside a SQLAHelper-wrapped application, the database session is accessed via request.db.
An example which sets up a new wsgi app wrapped in the sqlahelper middlewares... the database connection is built based on the sqlalchemy.url value:
config = Configurator(root_factory=models.get_root, settings={'sqlalchemy.url': 'sqlite://:memory'}) config.include('khufu.sqlahelper') app = sqlahelper.with_db(config.make_wsgi_app())
Once inside view code, database queries can happen as follows:
@view_config(context=models.SomeContainerModel) def users_view(request): return {'models': request.db.query(models.SomeModel).all()}
NOTE: view code should never manually commit the ``request.db`` object, please use the transaction api directly if this is required
Database Traversal Utils
The following provides traversal as follows:
/ # Root /distros/ # Distribution container /distros/foobar # Distribution called foobar /distros/foobar/versions/ # Versions container for foobar /distros/foobar/versions/0.1 # Version 0.1 of foobar
The example code is as follows:
from pysoftcenter import models from pysoftcenter.traversalutils import (DataContainer, attrs_traversable, TraversalMixin) class DistroVersionContainer(DataContainer): model_class = models.DistroVersion unique_lookup = 'version' @attrs_traversable(versions=DistroVersionContainer) class DistroDataContainer(DataContainer): model_class = models.Distro class Root(TraversalMixin, dict): def __init__(self, db): TraversalMixin.__init__(self, db=db) self['distros'] = DistroDataContainer('distros', self) def get_root(request): root = Root(request.db) return root
Transaction Handling
If your application requires control over the transaction handling, please use the transaction api.
Under the Hood
There is nothing magical about Khufu-SQLAHelper. It does the following things:
- Registers a SQLAlchemy based session factory and stores it in the middleware
- Uses zope.sqlalchemy to connect that session factory to the transaction handling
- Uses middleware to do the following:
- At the start of a request, a new session is created and stuffed into the environ
- Using a BeforeRequest event, the active session is retrieved from environ and added onto the request as the db attribute.
- At the end of the request, either commit the session/transaction if no error occurred, else rollback (provided by repoze.tm2).
Credits
- Developed and maintained by Rocky Burt <rocky AT serverzen DOT com>
Changes
0.4a3 (Feb-16,2011)
- Updated project url's
0.4a2 (Feb-11-2011)
- Fixed small issue where decorator wasn't working
0.4a1 (Feb-10-2011)
- Added traversalutils for helping expose SQL-based models using traversal
0.3 (Feb-04-2011)
- Changed with_db api to make db param optional
- Updated docs
0.2.1 (Jan-17-2011)
- Added use_zope_tm option to get_session_factory
0.2 (Jan-9-2011)
- first release