New submission from Marcin <erg...@gmail.com>:

fixes naming and mixed declarative/imperative style for models
also fixes problem with db_echo param not working  by using asbool()

----------
assignedto: chrism
files: normalized_alchemy_templates.diff
messages: 450
nosy: chrism, ergo
priority: bug
status: unread
title: normalized sql templates for repoze.bfg + fixed db_echo bug

__________________________________
Repoze Bugs <b...@bugs.repoze.org>
<http://bugs.repoze.org/issue167>
__________________________________
Index: repoze/bfg/paster_templates/alchemy/+package+/run.py_tmpl
===================================================================
--- repoze/bfg/paster_templates/alchemy/+package+/run.py_tmpl	(revision 10318)
+++ repoze/bfg/paster_templates/alchemy/+package+/run.py_tmpl	(working copy)
@@ -1,4 +1,5 @@
 from repoze.bfg.configuration import Configurator
+from paste.deploy.converters import asbool
 
 from {{package}}.models import appmaker
 
@@ -11,11 +12,11 @@
     zcml_file = settings.get('configure_zcml', 'configure.zcml')
     db_string = settings.get('db_string')
     if db_string is None:
-        raise ValueError("No 'db_string' in application configuration.")
+        raise ValueError("No 'db_string' value in application configuration.")
     db_echo = settings.get('db_echo')
     if db_echo is None:
         db_echo = True
-    get_root = appmaker(db_string, db_echo)
+    get_root = appmaker(db_string, asbool(db_echo))
     config = Configurator(settings=settings, root_factory=get_root)
     config.begin()
     config.load_zcml(zcml_file)
Index: repoze/bfg/paster_templates/alchemy/+package+/models.py
===================================================================
--- repoze/bfg/paster_templates/alchemy/+package+/models.py	(revision 10318)
+++ repoze/bfg/paster_templates/alchemy/+package+/models.py	(working copy)
@@ -19,7 +19,7 @@
 Base = declarative_base()
 
 class MyModel(Base):
-    __tablename__ = 'mymodel'
+    __tablename__ = 'models'
     id = Column(Integer, primary_key=True)
     name = Column(Unicode(255), unique=True)
     value = Column(Integer)
@@ -73,7 +73,7 @@
     session.flush()
     transaction.commit()
 
-def initialize_sql(db_string, db_echo):
+def initialize_sql(db_string, db_echo=False):
     engine = create_engine(db_string, echo=db_echo)
     DBSession.configure(bind=engine)
     Base.metadata.bind = engine
@@ -83,6 +83,6 @@
     except IntegrityError:
         pass
 
-def appmaker(db_string, db_echo):
+def appmaker(db_string, db_echo=False):
     initialize_sql(db_string, db_echo)
     return default_get_root
Index: repoze/bfg/paster_templates/alchemy/+project+.ini_tmpl
===================================================================
--- repoze/bfg/paster_templates/alchemy/+project+.ini_tmpl	(revision 10318)
+++ repoze/bfg/paster_templates/alchemy/+project+.ini_tmpl	(working copy)
@@ -1,20 +1,20 @@
 [DEFAULT]
 debug = true
 
-[app:alchemy]
-use = egg:{{project}}#app
+[app:sqlalchemy]
+use = egg:{{package}}#app
 reload_templates = true
 debug_authorization = false
 debug_notfound = false
 debug_templates = true
 default_locale_name = en
 db_string = sqlite:///%(here)s/{{package}}.db
-db_echo = false
+db_echo = true
 
 [pipeline:main]
 pipeline =
     egg:repoze.tm2#tm
-    alchemy
+    sqlalchemy
 
 [server:main]
 use = egg:Paste#http
Index: repoze/bfg/paster_templates/routesalchemy/+package+/views.py_tmpl
===================================================================
--- repoze/bfg/paster_templates/routesalchemy/+package+/views.py_tmpl	(revision 10318)
+++ repoze/bfg/paster_templates/routesalchemy/+package+/views.py_tmpl	(working copy)
@@ -1,7 +1,7 @@
 from {{package}}.models import DBSession
-from {{package}}.models import Model
+from {{package}}.models import MyModel
 
 def my_view(request):
     dbsession = DBSession()
-    root = dbsession.query(Model).filter(Model.name==u'root').first()
+    root = dbsession.query(MyModel).filter(MyModel.name==u'root').first()
     return {'root':root, 'project':'{{project}}'}
Index: repoze/bfg/paster_templates/routesalchemy/+package+/run.py_tmpl
===================================================================
--- repoze/bfg/paster_templates/routesalchemy/+package+/run.py_tmpl	(revision 10318)
+++ repoze/bfg/paster_templates/routesalchemy/+package+/run.py_tmpl	(working copy)
@@ -1,4 +1,5 @@
 from repoze.bfg.configuration import Configurator
+from paste.deploy.converters import asbool
 
 from {{package}}.models import initialize_sql
 
@@ -12,7 +13,10 @@
     db_string = settings.get('db_string')
     if db_string is None:
         raise ValueError("No 'db_string' value in application configuration.")
-    initialize_sql(db_string)
+    db_echo = settings.get('db_echo')
+    if db_echo is None:
+        db_echo = True
+    initialize_sql(db_string, asbool(db_echo))
     config = Configurator(settings=settings)
     config.begin()
     config.load_zcml(zcml_file)
Index: repoze/bfg/paster_templates/routesalchemy/+package+/models.py
===================================================================
--- repoze/bfg/paster_templates/routesalchemy/+package+/models.py	(revision 10318)
+++ repoze/bfg/paster_templates/routesalchemy/+package+/models.py	(working copy)
@@ -3,11 +3,11 @@
 from sqlalchemy import create_engine
 from sqlalchemy import Column
 from sqlalchemy import Integer
-from sqlalchemy import MetaData
 from sqlalchemy import Table
 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
@@ -16,34 +16,30 @@
 from zope.sqlalchemy import ZopeTransactionExtension
 
 DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
+Base = declarative_base()
 
-metadata = MetaData()
+class MyModel(Base):
+    __tablename__ = 'models'
+    id = Column(Integer, primary_key=True)
+    name = Column(Unicode(255), unique=True)
+    value = Column(Integer)
 
-class Model(object):
-    def __init__(self, name=''):
+    def __init__(self, name, value):
         self.name = name
+        self.value = value
 
-models_table = Table(
-    'models',
-    metadata,
-    Column('id', Integer, primary_key=True),
-    Column('name', Unicode(255), unique=True),
-    )
-
-models_mapper = mapper(Model, models_table)
-
 def populate():
     session = DBSession()
-    model = Model(name=u'root')
+    model = MyModel(name=u'root',value=55)
     session.add(model)
     session.flush()
     transaction.commit()
     
-def initialize_sql(db_string, echo=False):
-    engine = create_engine(db_string, echo=echo)
+def initialize_sql(db_string, db_echo=False):
+    engine = create_engine(db_string, echo=db_echo)
     DBSession.configure(bind=engine)
-    metadata.bind = engine
-    metadata.create_all(engine)
+    Base.metadata.bind = engine
+    Base.metadata.create_all(engine)
     try:
         populate()
     except IntegrityError:
Index: repoze/bfg/paster_templates/routesalchemy/+project+.ini_tmpl
===================================================================
--- repoze/bfg/paster_templates/routesalchemy/+project+.ini_tmpl	(revision 10318)
+++ repoze/bfg/paster_templates/routesalchemy/+project+.ini_tmpl	(working copy)
@@ -1,7 +1,7 @@
 [DEFAULT]
 debug = true
 
-[app:sql]
+[app:sqlalchemy]
 use = egg:{{package}}#app
 reload_templates = true
 debug_authorization = false
@@ -9,11 +9,12 @@
 debug_templates = true
 default_locale_name = en
 db_string = sqlite:///%(here)s/{{package}}.db
+db_echo = true
 
 [pipeline:main]
 pipeline =
     egg:repoze.tm2#tm
-    sql
+    sqlalchemy
 
 [server:main]
 use = egg:Paste#http
_______________________________________________
Repoze-dev mailing list
Repoze-dev@lists.repoze.org
http://lists.repoze.org/listinfo/repoze-dev

Reply via email to