PageRenderTime 68ms CodeModel.GetById 45ms RepoModel.GetById 0ms app.codeStats 0ms

/tboneold/appbase.py

https://github.com/peter-the-tea-drinker/tornado-base
Python | 57 lines | 31 code | 9 blank | 17 comment | 0 complexity | ce0fd0119bdd3dbbc5b37f57729fdc2a MD5 | raw file
  1. import config
  2. import sqlalchemy
  3. import sqlalchemy.orm
  4. from sqlalchemy.ext.declarative import declarative_base
  5. # to upgrade live
  6. # - create an empty database (with new schema)
  7. # - upgrade all the web apps
  8. # - set some kind of "in_upgrade" flag
  9. # - for all new transactions
  10. # - look in the new DB first.
  11. # - fallback to reading from the old db, and upgrade the record.
  12. # - post to the new DB (try "new", fallback "edit")
  13. # - gradually move records into the new DB (upgrading).
  14. class ORM(object):
  15. def __init__(self):
  16. self.engine = None
  17. self.Session = sqlalchemy.orm.sessionmaker()
  18. self.Base = declarative_base()
  19. def initialize(self,engine):
  20. self.engine = engine
  21. self.Session.configure(bind=engine)
  22. orm = ORM()
  23. orm.initialize(sqlalchemy.create_engine(config.DB))#, encoding='utf-8'))
  24. sql = sqlalchemy
  25. def wsgi_app(urls):
  26. import tornado.wsgi
  27. return tornado.wsgi.WSGIApplication(urls)
  28. def tornado_serve(urls,debug=False,cookie_secret=None):
  29. import tornado.ioloop
  30. import tornado.web
  31. application = tornado.web.Application(
  32. urls,
  33. debug=debug,
  34. cookie_secret=cookie_secret,
  35. xsrf_cookies=True,
  36. static_path = config.STATIC,
  37. template_path = config.TEMPLATE
  38. )
  39. application.listen(8888)
  40. tornado.ioloop.IOLoop.instance().start()
  41. # engine = sqlalchemy.create_engine('sqlite:///project.db')
  42. # metadata = sqlalchemy.MetaData()
  43. # metadata.create_all(engine)
  44. # python migrations/manage.py version_control sqlite:///project.db migrations
  45. # python migrations/manage.py db_version sqlite:///project.db migrations
  46. # python migrations/manage.py script "Add user tables" migrations
  47. # how to migrate data:
  48. #