Commits

Dan Connolly committed 0fedc21

integrate mock data to run per development.ini
- never mind pyramid_debugtoolbar; the server produces JSON
- bootstrap_db config option
- update views.py w.r.t. session re-factoring

Comments (0)

Files changed (4)

finjax/development.ini

 pyramid.debug_routematch = false
 pyramid.default_locale_name = en
 pyramid.includes =
-    pyramid_debugtoolbar
     pyramid_tm
 
 sqlalchemy.url = sqlite:///%(here)s/finjax.db
+bootstrap_db = true
 
 [server:main]
 use = egg:waitress#main

finjax/finjax/__init__.py

 from pyramid.config import Configurator
 import sqlalchemy
 
-from .models import DBConfig
-from .views import FinjaxAPI
+import models
+import views
 
 
 def main(global_config, **settings):
     @param settings: settings for this application
     @return: a WSGI application.
     """
-    finjax, config = RunTime.make(settings, [FinjaxAPI, Configurator])
+    finjax, config = RunTime.make(settings, [views.FinjaxAPI, Configurator])
     finjax.add_rest_api()
     return config.make_wsgi_app()
 
     @singleton
     @provides(sqlalchemy.engine.Engine)
     def db(self, section='sqlalchemy.'):
-        return sqlalchemy.engine_from_config(self._settings, section)
+        e = sqlalchemy.engine_from_config(self._settings, section)
+        if 'bootstrap_db' in self._settings:
+            models.Mock().bootstrap(e)
+        return e
 
     @classmethod
     def make(cls, settings, what):
         @param what: list of classes to instantiate;
                      use None as list item to get the whole Injector depgraph.
         '''
-        mods = [cls(settings), DBConfig()]
+        mods = [cls(settings), models.DBConfig()]
         depgraph = injector.Injector(mods)
         return [depgraph.get(it) if it else depgraph
                 for it in what]

finjax/finjax/models.py

         return [depgraph.get(it) if it else depgraph
                 for it in what]
 
+    @singleton
     @provides(engine.Engine)
     def engine_with_mock_data(self, url='sqlite:///'):
         engine = create_engine(url)
+        self.bootstrap(engine)
+        return engine
+
+    def bootstrap(self, engine):
         Base.metadata.create_all(engine)
         engine.execute(Account.__table__.insert(),
                        self.mock_accounts())
         engine.execute(Transaction.__table__.insert(), self.transactions)
         engine.execute(Split.__table__.insert(), self.splits)
 
-        return engine
-
     def mock_accounts(self):
         return [dict(name=acct.name,
                      guid=_n2g(acct.name),

finjax/finjax/views.py

 from dotdict import dotdict
 from models import (
     Account, Transaction,
-    KSessionMaker
+    KSession
     )
 
 
 class JSONDBView(object):
     '''View to access DB and render to JSON.
     '''
-    @inject(session=KSessionMaker)
+    @inject(session=KSession)
     def __init__(self, session):
         self._session = session