Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install khufu-sqlahelper

How to install Khufu-SQLAHelper

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install khufu-sqlahelper
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
0.4a3 Available View build log
0.4a2 Available View build log
0.4a1 Available View build log
0.3 Available View build log
0.2.1 Available View build log
0.2 Available View build log
Windows (64-bit)
0.4a3 Available View build log
0.4a2 Available View build log
0.4a1 Available View build log
0.3 Available View build log
0.2.1 Available View build log
0.2 Available View build log
Mac OS X (10.5+)
0.4a3 Available View build log
0.4a2 Available View build log
0.4a1 Available View build log
0.3 Available View build log
0.2.1 Available View build log
0.2 Available View build log
Linux (32-bit)
0.4a3 Available View build log
0.4a2 Available View build log
0.4a1 Available View build log
0.3 Available View build log
0.2.1 Available View build log
0.2 Available View build log
Linux (64-bit)
0.4a3 Available View build log
0.4a2 Available View build log
0.4a1 Available View build log
0.3 Available View build log
0.2.1 Available View build log
0.2 Available View build log
 
Author
License
BSD
Depended by
Lastest release
version 0.4a3 on Feb 17th, 2011

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:

  1. Hook up the configuration via configurator.include('khufu.sqlahelper')
  2. 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:

  1. Registers a SQLAlchemy based session factory and stores it in the middleware
  2. Uses zope.sqlalchemy to connect that session factory to the transaction handling
  3. Uses middleware to do the following:
  1. At the start of a request, a new session is created and stuffed into the environ
  2. Using a BeforeRequest event, the active session is retrieved from environ and added onto the request as the db attribute.
  3. 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

Subscribe to package updates

Last updated Feb 17th, 2011

Download Stats

Last month:2

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.