Chris McDonough
2010-11-21 d0e2f661e07d43188435b25aec0577dfbd50cfb0
- SQLAlchemy+URLDispatch tutorial updated to integrate changes to
``pyramid_routesalchemy`` template.
21 files modified
257 ■■■■ changed files
.gitignore 1 ●●●● patch | view | raw | blame | history
CHANGES.txt 3 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/basiclayout.rst 41 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/authorization/development.ini 37 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/authorization/tutorial/__init__.py 9 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/authorization/tutorial/models.py 4 ●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/basiclayout/development.ini 37 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/basiclayout/tutorial/__init__.py 9 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/basiclayout/tutorial/models.py 4 ●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/basiclayout/tutorial/tests.py 3 ●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/models/development.ini 37 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/models/tutorial/__init__.py 9 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/models/tutorial/models.py 4 ●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/views/development.ini 37 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/views/tutorial/__init__.py 9 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/views/tutorial/models.py 4 ●●● patch | view | raw | blame | history
pyramid/paster_templates/alchemy/+package+/__init__.py_tmpl 1 ●●●● patch | view | raw | blame | history
pyramid/paster_templates/pylons_sqla/+package+/__init__.py_tmpl 1 ●●●● patch | view | raw | blame | history
pyramid/paster_templates/pylons_sqla/+package+/tests.py_tmpl 3 ●●●● patch | view | raw | blame | history
pyramid/paster_templates/routesalchemy/+package+/__init__.py_tmpl 1 ●●●● patch | view | raw | blame | history
pyramid/paster_templates/routesalchemy/+package+/tests.py_tmpl 3 ●●●● patch | view | raw | blame | history
.gitignore
@@ -5,6 +5,7 @@
*.pt.py
*.txt.py
.coverage
tutorial.db
env26/
env24/
env27/
CHANGES.txt
@@ -23,6 +23,9 @@
  Shootout and Virginia sample applications, ported from their repoze.bfg
  origin packages.
- SQLAlchemy+URLDispatch tutorial updated to integrate changes to
  ``pyramid_routesalchemy`` template.
1.0a4 (2010-11-21)
==================
docs/tutorials/wiki2/basiclayout.rst
@@ -32,27 +32,21 @@
#. *Lines 1-4*. Imports to support later code.
#. *Lines 9-11*. Get the database configuration string from the
   ``development.ini`` file's ``[app:sqlalchemy]`` section.  This will be a
   URI (something like ``sqlite://``).
#. *Line 9*. Create a SQLAlchemy database engine from the ``sqlalchemy.``
   prefixed settings in the ``development.ini`` file's ``[app:tutorial]``
   section.  This will be a URI (something like ``sqlite://``).
#. *Line 12*. Get the database echo setting from ``development.ini``
   file's ``[app:sqlalchemy]`` section.  This will either be ``true``
   or ``false``.  If ``true``, the application will print SQL to the
   console as it is generated and run by SQLAlchemy.  By default, it
   is false.
#. *Line 10*. We initialize our SQL database using SQLAlchemy, passing
   it the engine
#. Line *13*. We initialize our SQL database using SQLAlchemy, passing
   it the db string and a variant of the db_echo value.
#. *Line 14*.  We construct a :term:`Configurator`.  ``settings`` is
#. *Line 11*.  We construct a :term:`Configurator`.  ``settings`` is
   passed as a keyword argument with the dictionary values passed by
   PasteDeploy as the ``settings`` argument.  This will be a
   dictionary of settings parsed by PasteDeploy, which contains
   deployment-related values such as ``reload_templates``,
   ``db_string``, etc.
#. *Line 15*.  We call
#. *Line 12*.  We call
   :meth:`pyramid.configuration.Configurator.add_static_view` with the
   arguments ``static`` (the name), and ``tutorial:static`` (the path).  This
   registers a static resource view which will match any URL that starts with
@@ -64,7 +58,7 @@
   ``/static/foo``) will be used to compose a path to a static file resource,
   such as a CSS file.
#. *Lines 16-17*.  Register a :term:`route configuration` via the
#. *Lines 13-14*.  Register a :term:`route configuration` via the
   :meth:`pyramid.configuration.Configurator.add_route` method that will be
   used when the URL is ``/``.  Since this route has an ``pattern`` equalling
   ``/`` it is the "default" route. The argument named ``view`` with the
@@ -78,7 +72,7 @@
   ``tutorial.views.my_view`` view returns a dictionary, a :term:`renderer`
   will use this template to create a response.
#. *Line 18*.  We use the
#. *Line 15*.  We use the
   :meth:`pyramid.configuration.Configurator.make_wsgi_app` method to return
   a :term:`WSGI` application.
@@ -97,29 +91,28 @@
      :linenos:
      :language: py
#. *Lines 1-14*.  Imports to support later code.
#. *Lines 1-13*.  Imports to support later code.
#. *Line 16*.  We set up a SQLAlchemy "DBSession" object here.  We
#. *Line 15*.  We set up a SQLAlchemy "DBSession" object here.  We
   specify that we'd like to use the "ZopeTransactionExtension".  This
   extension is an extension which allows us to use a *transaction
   manager* instead of controlling commits and aborts to database
   operations by hand.
#. *Line 17*.  We create a declarative ``Base`` object to use as a
#. *Line 16*.  We create a declarative ``Base`` object to use as a
   base class for our model.
#. *Lines 19-27*.  A model class named ``MyModel``.  It has an
#. *Lines 18-26*.  A model class named ``MyModel``.  It has an
   ``__init__`` that takes a two arguments (``name``, and ``value``).
   It stores these values as ``self.name`` and ``self.value`` within
   the ``__init__`` function itself.  The ``MyModel`` class also has a
   ``__tablename__`` attribute.  This informs SQLAlchemy which table
   to use to store the data representing instances of this class.
#. *Lines 29-34*.  A function named ``populate`` which adds a single
#. *Lines 28-33*.  A function named ``populate`` which adds a single
   model instance into our SQL storage and commits a transaction.
#. *Lines 36-44*.  A function named ``initialize_sql`` which sets up
   an actual SQL database and binds it to our SQLAlchemy DBSession
   object.  It also calls the ``populate`` function, to do initial
   database population.
#. *Lines 35-42*.  A function named ``initialize_sql`` which receives a SQL
   database engine and binds it to our SQLAlchemy DBSession object.  It also
   calls the ``populate`` function, to do initial database population.
docs/tutorials/wiki2/src/authorization/development.ini
@@ -5,8 +5,7 @@
debug_notfound = false
debug_templates = true
default_locale_name = en
db_string = sqlite:///%(here)s/tutorial.db
db_echo = false
sqlalchemy.url = sqlite:///%(here)s/tutorial.db
[pipeline:main]
pipeline =
@@ -18,3 +17,37 @@
use = egg:Paste#http
host = 0.0.0.0
port = 6543
# Begin logging configuration
[loggers]
keys = root, sqlalchemy
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = INFO
handlers = console
[logger_sqlalchemy]
level = INFO
handlers =
qualname = sqlalchemy.engine
# "level = INFO" logs SQL queries.
# "level = DEBUG" logs SQL queries and results.
# "level = WARN" logs neither.  (Recommended for production systems.)
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
# End logging configuration
docs/tutorials/wiki2/src/authorization/tutorial/__init__.py
@@ -2,7 +2,7 @@
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.settings import asbool
from sqlalchemy import engine_from_config
from tutorial.models import initialize_sql
from tutorial.security import groupfinder
@@ -10,11 +10,8 @@
def main(global_config, **settings):
    """ This function returns a WSGI application.
    """
    db_string = settings.get('db_string')
    if db_string is None:
        raise ValueError("No 'db_string' value in application configuration.")
    db_echo = settings.get('db_echo', 'false')
    initialize_sql(db_string, asbool(db_echo))
    engine = engine_from_config(settings, 'sqlalchemy.')
    initialize_sql(engine)
    authn_policy = AuthTktAuthenticationPolicy(
        'sosecret', callback=groupfinder)
    authz_policy = ACLAuthorizationPolicy()
docs/tutorials/wiki2/src/authorization/tutorial/models.py
@@ -3,7 +3,6 @@
from pyramid.security import Allow
from pyramid.security import Everyone
from sqlalchemy import create_engine
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import Text
@@ -30,8 +29,7 @@
       self.name = name
       self.data = data
def initialize_sql(db_string, echo=False):
    engine = create_engine(db_string, echo=echo)
def initialize_sql(engine):
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)
docs/tutorials/wiki2/src/basiclayout/development.ini
@@ -5,8 +5,7 @@
debug_notfound = false
debug_templates = true
default_locale_name = en
db_string = sqlite:///%(here)s/tutorial.db
db_echo = false
sqlalchemy.url = sqlite:///%(here)s/tutorial.db
[pipeline:main]
pipeline =
@@ -18,3 +17,37 @@
use = egg:Paste#http
host = 0.0.0.0
port = 6543
# Begin logging configuration
[loggers]
keys = root, sqlalchemy
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = INFO
handlers = console
[logger_sqlalchemy]
level = INFO
handlers =
qualname = sqlalchemy.engine
# "level = INFO" logs SQL queries.
# "level = DEBUG" logs SQL queries and results.
# "level = WARN" logs neither.  (Recommended for production systems.)
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
# End logging configuration
docs/tutorials/wiki2/src/basiclayout/tutorial/__init__.py
@@ -1,16 +1,13 @@
from pyramid.configuration import Configurator
from pyramid.settings import asbool
from sqlalchemy import engine_from_config
from tutorial.models import initialize_sql
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    db_string = settings.get('db_string')
    if db_string is None:
        raise ValueError("No 'db_string' value in application configuration.")
    db_echo = settings.get('db_echo', 'false')
    initialize_sql(db_string, asbool(db_echo))
    engine = engine_from_config(settings, 'sqlalchemy.')
    initialize_sql(engine)
    config = Configurator(settings=settings)
    config.add_static_view('static', 'tutorial:static')
    config.add_route('home', '/', view='tutorial.views.my_view',
docs/tutorials/wiki2/src/basiclayout/tutorial/models.py
@@ -1,6 +1,5 @@
import transaction
from sqlalchemy import create_engine
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import Unicode
@@ -33,8 +32,7 @@
    session.flush()
    transaction.commit()
    
def initialize_sql(db_string, db_echo=False):
    engine = create_engine(db_string, echo=db_echo)
def initialize_sql(engine):
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)
docs/tutorials/wiki2/src/basiclayout/tutorial/tests.py
@@ -3,8 +3,9 @@
from pyramid import testing
def _initTestingDB():
    from sqlalchemy import create_engine
    from tutorial.models import initialize_sql
    session = initialize_sql('sqlite://')
    session = initialize_sql(create_engine('sqlite://'))
    return session
class TestMyView(unittest.TestCase):
docs/tutorials/wiki2/src/models/development.ini
@@ -5,8 +5,7 @@
debug_notfound = false
debug_templates = true
default_locale_name = en
db_string = sqlite:///%(here)s/tutorial.db
db_echo = false
sqlalchemy.url = sqlite:///%(here)s/tutorial.db
[pipeline:main]
pipeline =
@@ -18,3 +17,37 @@
use = egg:Paste#http
host = 0.0.0.0
port = 6543
# Begin logging configuration
[loggers]
keys = root, sqlalchemy
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = INFO
handlers = console
[logger_sqlalchemy]
level = INFO
handlers =
qualname = sqlalchemy.engine
# "level = INFO" logs SQL queries.
# "level = DEBUG" logs SQL queries and results.
# "level = WARN" logs neither.  (Recommended for production systems.)
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
# End logging configuration
docs/tutorials/wiki2/src/models/tutorial/__init__.py
@@ -1,16 +1,13 @@
from pyramid.configuration import Configurator
from pyramid.settings import asbool
from sqlalchemy import engine_from_config
from tutorial.models import initialize_sql
def main(global_config, **settings):
    """ This function returns a WSGI application.
    """
    db_string = settings.get('db_string')
    if db_string is None:
        raise ValueError("No 'db_string' value in application configuration.")
    db_echo = settings.get('db_echo', 'false')
    initialize_sql(db_string, asbool(db_echo))
    engine = engine_from_config(settings, 'sqlalchemy.')
    initialize_sql(engine)
    config = Configurator(settings=settings)
    config.add_static_view('static', 'tutorial:static')
    config.add_route('home', '/', view='tutorial.views.my_view',
docs/tutorials/wiki2/src/models/tutorial/models.py
@@ -1,6 +1,5 @@
import transaction
from sqlalchemy import create_engine
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import Text
@@ -27,8 +26,7 @@
       self.name = name
       self.data = data
def initialize_sql(db_string, echo=False):
    engine = create_engine(db_string, echo=echo)
def initialize_sql(engine):
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)
docs/tutorials/wiki2/src/views/development.ini
@@ -5,8 +5,7 @@
debug_notfound = false
debug_templates = true
default_locale_name = en
db_string = sqlite:///%(here)s/tutorial.db
db_echo = false
sqlalchemy.url = sqlite:///%(here)s/tutorial.db
[pipeline:main]
pipeline =
@@ -18,3 +17,37 @@
use = egg:Paste#http
host = 0.0.0.0
port = 6543
# Begin logging configuration
[loggers]
keys = root, sqlalchemy
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = INFO
handlers = console
[logger_sqlalchemy]
level = INFO
handlers =
qualname = sqlalchemy.engine
# "level = INFO" logs SQL queries.
# "level = DEBUG" logs SQL queries and results.
# "level = WARN" logs neither.  (Recommended for production systems.)
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
# End logging configuration
docs/tutorials/wiki2/src/views/tutorial/__init__.py
@@ -1,16 +1,13 @@
from pyramid.configuration import Configurator
from pyramid.settings import asbool
from sqlalchemy import engine_from_config
from tutorial.models import initialize_sql
def main(global_config, **settings):
    """ This function returns a WSGI application.
    """
    db_string = settings.get('db_string')
    if db_string is None:
        raise ValueError("No 'db_string' value in application configuration.")
    db_echo = settings.get('db_echo', 'false')
    initialize_sql(db_string, asbool(db_echo))
    engine = engine_from_config(settings, 'sqlalchemy.')
    initialize_sql(engine)
    config = Configurator(settings=settings)
    config.add_static_view('static', 'tutorial:static')
    config.add_route('home', '/', view='tutorial.views.view_wiki')
docs/tutorials/wiki2/src/views/tutorial/models.py
@@ -1,6 +1,5 @@
import transaction
from sqlalchemy import create_engine
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import Text
@@ -27,8 +26,7 @@
       self.name = name
       self.data = data
def initialize_sql(db_string, echo=False):
    engine = create_engine(db_string, echo=echo)
def initialize_sql(engine):
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)
pyramid/paster_templates/alchemy/+package+/__init__.py_tmpl
@@ -1,5 +1,4 @@
from pyramid.configuration import Configurator
from pyramid.settings import asbool
from sqlalchemy import engine_from_config
from {{package}}.models import appmaker
pyramid/paster_templates/pylons_sqla/+package+/__init__.py_tmpl
@@ -1,5 +1,4 @@
from pyramid.configuration import Configurator
from pyramid.settings import asbool
from pyramid_beaker import session_factory_from_settings
pyramid/paster_templates/pylons_sqla/+package+/tests.py_tmpl
@@ -3,8 +3,9 @@
class MyHandlerTests(unittest.TestCase):
    def setUp(self):
        from pyramid.configuration import Configurator
        from sqlalchemy import create_engine
        from {{package}}.models import initialize_sql
        self.session = initialize_sql('sqlite://')
        self.session = initialize_sql(create_engine('sqlite://'))
        self.config = Configurator()
        self.config.begin()
pyramid/paster_templates/routesalchemy/+package+/__init__.py_tmpl
@@ -1,5 +1,4 @@
from pyramid.configuration import Configurator
from pyramid.settings import asbool
from sqlalchemy import engine_from_config
from {{package}}.models import initialize_sql
pyramid/paster_templates/routesalchemy/+package+/tests.py_tmpl
@@ -3,8 +3,9 @@
from pyramid import testing
def _initTestingDB():
    from sqlalchemy import create_engine
    from {{package}}.models import initialize_sql
    session = initialize_sql('sqlite://')
    session = initialize_sql(create_engine('sqlite://'))
    return session
class TestMyView(unittest.TestCase):