Permalink
Browse files

work towards https://github.com/Pylons/pyramid/issues#issue/44

  • Loading branch information...
1 parent 00fb8b0 commit ecee85c8a87e2440abd1aeda3d1043b01cf1f4ab @mcdonc mcdonc committed Nov 22, 2010
@@ -2,15 +2,12 @@ from pyramid.configuration import Configurator
from pyramid_beaker import session_factory_from_settings
-from sqlalchemy import engine_from_config
-
-from {{package}}.models import initialize_sql
+from {{package}}.models import init_model
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
- engine = engine_from_config(settings, 'sqlalchemy.')
- initialize_sql(engine)
+ init_model(settings)
config = Configurator(settings=settings)
session_factory = session_factory_from_settings(settings)
config.set_session_factory(session_factory)
@@ -1,6 +1,6 @@
from pyramid.view import action
-from {{package}}.models import MyModel
+from {{package}}.models.mymodel import MyModel
class MyHandler(object):
def __init__(self, request):
@@ -1,46 +0,0 @@
-import transaction
-
-from sqlalchemy import create_engine
-from sqlalchemy import Column
-from sqlalchemy import Integer
-from sqlalchemy import Unicode
-
-from sqlalchemy.exc import IntegrityError
-from sqlalchemy.ext.declarative import declarative_base
-
-from sqlalchemy.orm import scoped_session
-from sqlalchemy.orm import sessionmaker
-
-from zope.sqlalchemy import ZopeTransactionExtension
-
-DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
-Base = declarative_base()
-
-class MyModel(Base):
- __tablename__ = 'models'
- id = Column(Integer, primary_key=True)
- name = Column(Unicode(255), unique=True)
- value = Column(Integer)
-
- def __init__(self, name, value):
- self.name = name
- self.value = value
-
- @classmethod
- def by_name(cls, name=None):
- return DBSession.query(cls).filter(cls.name == name).first()
-
-def populate():
- model = MyModel(name=u'root', value=55)
- DBSession.add(model)
- DBSession.flush()
- transaction.commit()
-
-def initialize_sql(engine):
- DBSession.configure(bind=engine)
- Base.metadata.bind = engine
- Base.metadata.create_all(engine)
- try:
- populate()
- except IntegrityError:
- pass
@@ -0,0 +1,17 @@
+from sqlalchemy import engine_from_config
+
+from sqlalchemy.ext.declarative import declarative_base
+
+from sqlalchemy.orm import scoped_session
+from sqlalchemy.orm import sessionmaker
+
+from zope.sqlalchemy import ZopeTransactionExtension
+
+DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
+Base = declarative_base()
+
+def init_model(settings):
+ engine = engine_from_config(settings, 'sqlalchemy.')
+ DBSession.configure(bind=engine)
+ Base.metadata.bind = engine
+ return engine
@@ -0,0 +1,21 @@
+from sqlalchemy import Column
+from sqlalchemy import Integer
+from sqlalchemy import Unicode
+
+from {{package}}.models import Base
+from {{package}}.models import DBSession
+
+class MyModel(Base):
+ __tablename__ = 'models'
+ id = Column(Integer, primary_key=True)
+ name = Column(Unicode(255), unique=True)
+ value = Column(Integer)
+
+ def __init__(self, name, value):
+ self.name = name
+ self.value = value
+
+ @classmethod
+ def by_name(cls, name=None):
+ return DBSession.query(cls).filter(cls.name == name).first()
+
@@ -0,0 +1,25 @@
+import os
+import pkg_resources
+import sys
+import transaction
+
+from paste.deploy.loadwsgi import appconfig
+
+from {{package}}.models import init_model
+from {{package}}.models import Base
+from {{package}}.models import DBSession
+from {{package}}.models.mymodel import MyModel
+
+def setup(argv=sys.argv):
+ dist = pkg_resources.get_distribution('{{project}}')
+ root = dist.location
+ config = 'config:' + os.path.join(root, 'development.ini')
+ settings = appconfig(config, "{{project}}")
+ engine = init_model(settings)
+ DBSession.configure(bind=engine)
+ Base.metadata.bind = engine
+ Base.metadata.create_all(engine)
+ model = MyModel(name=u'root', value=55)
+ DBSession.add(model)
+ DBSession.flush()
+ transaction.commit()
@@ -3,20 +3,29 @@ import unittest
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(create_engine('sqlite://'))
self.config = Configurator()
self.config.begin()
def tearDown(self):
self.config.end()
+ def _initSQL(self):
+ from {{package}}.models import Base
+ from {{package}}.models import DBSession
+ from {{package}}.models import init_model
+ from {{package}}.models.mymodel import MyModel
+ engine = init_model({'sqlalchemy.url':'sqlite:///'})
+ Base.metadata.create_all(engine)
+ model = MyModel(name=u'root', value=55)
+ DBSession.add(model)
+ DBSession.flush()
+
def _makeOne(self, request):
from {{package}}.handlers import MyHandler
return MyHandler(request)
def test_index(self):
+ self._initSQL()
request = DummyRequest()
handler = self._makeOne(request)
info = handler.index()
@@ -43,6 +43,8 @@ setup(name='{{project}}',
entry_points = """\
[paste.app_factory]
main = {{package}}:main
+ [console_scripts]
+ setup-{{project}} = {{package}}.scripts:setup
""",
paster_plugins=['pyramid'],
)

0 comments on commit ecee85c

Please sign in to comment.