├── .gitignore ├── LICENSE ├── README ├── app.py ├── database.py ├── forms.py ├── models.py ├── requirements.txt ├── static └── favicon.ico ├── templates ├── base.html ├── index.html └── uimodules │ └── form.html ├── uimodules.py └── util.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.so 3 | *~ 4 | build 5 | dist/ 6 | MANIFEST 7 | tornado.egg-info 8 | _auto2to3* 9 | .DS_Store 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2011 by Haldun Bayhantopcu 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | A tornado app template preconfigured for sqlalchemy and wtforms 2 | 3 | Moved to bitbucket: https://bitbucket.org/haldun/tornado-sqlalchemy-template 4 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | # Python imports 2 | import os 3 | 4 | # Tornado imports 5 | import tornado.auth 6 | import tornado.httpserver 7 | import tornado.ioloop 8 | import tornado.options 9 | import tornado.web 10 | 11 | from tornado.options import define, options 12 | from tornado.web import url 13 | 14 | # Sqlalchemy imports 15 | from sqlalchemy import create_engine 16 | from sqlalchemy.orm import scoped_session, sessionmaker 17 | 18 | # App imports 19 | import forms 20 | import models 21 | import uimodules 22 | 23 | # Options 24 | define("port", default=8888, help="run on the given port", type=int) 25 | define("debug", default=False, type=bool) 26 | define("db_path", default='sqlite:////tmp/test.db', type=str) 27 | 28 | class Application(tornado.web.Application): 29 | def __init__(self): 30 | handlers = [ 31 | url(r'/', IndexHandler, name='index'), 32 | ] 33 | settings = dict( 34 | debug=options.debug, 35 | static_path=os.path.join(os.path.dirname(__file__), "static"), 36 | template_path=os.path.join(os.path.dirname(__file__), 'templates'), 37 | xsrf_cookies=True, 38 | # TODO Change this to a random string 39 | cookie_secret="nzjxcjasduuqwheazmu293nsadhaslzkci9023nsadnua9sdads/Vo=", 40 | ui_modules=uimodules, 41 | ) 42 | tornado.web.Application.__init__(self, handlers, **settings) 43 | engine = create_engine(options.db_path, convert_unicode=True, echo=options.debug) 44 | models.init_db(engine) 45 | self.db = scoped_session(sessionmaker(bind=engine)) 46 | 47 | 48 | class BaseHandler(tornado.web.RequestHandler): 49 | @property 50 | def db(self): 51 | return self.application.db 52 | 53 | 54 | class IndexHandler(BaseHandler): 55 | def get(self): 56 | form = forms.HelloForm() 57 | self.render('index.html', form=form) 58 | 59 | def post(self): 60 | form = forms.HelloForm(self) 61 | if form.validate(): 62 | self.write('Hello %s' % form.planet.data) 63 | else: 64 | self.render('index.html', form=form) 65 | 66 | 67 | # Write your handlers here 68 | 69 | def main(): 70 | tornado.options.parse_command_line() 71 | http_server = tornado.httpserver.HTTPServer(Application()) 72 | http_server.listen(options.port) 73 | tornado.ioloop.IOLoop.instance().start() 74 | 75 | if __name__ == '__main__': 76 | main() 77 | -------------------------------------------------------------------------------- /database.py: -------------------------------------------------------------------------------- 1 | from sqlalchemy import create_engine 2 | from sqlalchemy import Column, Integer, String, DateTime, Boolean 3 | from sqlalchemy.orm import scoped_session, sessionmaker 4 | from sqlalchemy.ext.declarative import declarative_base 5 | 6 | engine = create_engine('sqlite:////tmp/test.db', convert_unicode=True, echo=True) 7 | db_session = scoped_session(sessionmaker(autocommit=False, 8 | autoflush=False, 9 | bind=engine)) 10 | Base = declarative_base() 11 | Base.query = db_session.query_property() 12 | 13 | def init_db(): 14 | Base.metadata.create_all(bind=engine) 15 | 16 | if __name__ == '__main__': 17 | init_db() 18 | -------------------------------------------------------------------------------- /forms.py: -------------------------------------------------------------------------------- 1 | from wtforms import * 2 | from wtforms.validators import * 3 | 4 | from util import MultiValueDict 5 | 6 | class BaseForm(Form): 7 | def __init__(self, handler=None, obj=None, prefix='', formdata=None, **kwargs): 8 | if handler: 9 | formdata = MultiValueDict() 10 | for name in handler.request.arguments.keys(): 11 | formdata.setlist(name, handler.get_arguments(name)) 12 | Form.__init__(self, formdata, obj=obj, prefix=prefix, **kwargs) 13 | 14 | 15 | # TODO Put your forms here 16 | 17 | class HelloForm(BaseForm): 18 | planet = TextField('name', validators=[Required()]) 19 | 20 | -------------------------------------------------------------------------------- /models.py: -------------------------------------------------------------------------------- 1 | from sqlalchemy import create_engine 2 | from sqlalchemy import Column, Integer, String, DateTime, Boolean 3 | from sqlalchemy.orm import scoped_session, sessionmaker 4 | from sqlalchemy.ext.declarative import declarative_base 5 | 6 | Base = declarative_base() 7 | 8 | def init_db(engine): 9 | Base.metadata.create_all(bind=engine) 10 | 11 | # Put your models here 12 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | tornado 2 | wtforms 3 | sqlalchemy -------------------------------------------------------------------------------- /static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haldun/tornado-sqlalchemy-template/c40177ee73eaba4f5fc928328a0aee7f179c9bc7/static/favicon.ico -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |