Skip to content

Commit dabf45e

Browse files
committed
integrating sqlalchemy
1 parent f0da4f3 commit dabf45e

File tree

10 files changed

+101
-37
lines changed

10 files changed

+101
-37
lines changed

speleo.service/handlers/blockboard_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import tornado.web
1+
import common
22

3-
class BlockboardHandler(tornado.web.RequestHandler):
3+
class BlockboardHandler(common.BaseHandler):
44

55
RoutePath = r'/blockboard'
66

speleo.service/handlers/common.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44
class BaseHandler(tornado.web.RequestHandler):
55

6+
@property
7+
def db(self):
8+
return self.application.database
9+
10+
@property
11+
def cache(self):
12+
return self.application.cache
13+
614
def get_current_user(self):
715
user_json = self.get_secure_cooke("user")
816
if not user_json: return None

speleo.service/handlers/contact_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import tornado.web
1+
import common
22

3-
class ContactHandler(tornado.web.RequestHandler):
3+
class ContactHandler(common.BaseHandler):
44

55
RoutePath = r'/contact'
66

speleo.service/handlers/dashboard_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import tornado.web
1+
import common
22

3-
class DashboardHandler(tornado.web.RequestHandler):
3+
class DashboardHandler(common.BaseHandler):
44

55
RoutePath = r'/dashboard'
66

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import tornado.web
1+
import common
22

3-
class RootHandler(tornado.web.RequestHandler):
3+
class RootHandler(common.BaseHandler):
44

55
RoutePath = r'/'
66

77
def get(self):
8-
self.render('index.html')
8+
self.redirect('/about')

speleo.service/handlers/search_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import tornado.web
1+
import common
22

3-
class SearchHandler(tornado.web.RequestHandler):
3+
class SearchHandler(common.BaseHandler):
44

55
RoutePath = r'/search'
66

speleo.service/handlers/settings_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import tornado.web
1+
import common
22

3-
class SettingsHandler(tornado.web.RequestHandler):
3+
class SettingsHandler(common.BaseHandler):
44

55
RoutePath = r'/settings'
66

speleo.service/handlers/simple_handler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import tornado.web
1+
import common
22

3-
class AboutHandler(tornado.web.RequestHandler):
3+
class AboutHandler(common.BaseHandler):
44

55
RoutePath = r'/about'
66

77
def get(self):
88
self.render('about.html')
99

10-
class AnalyzeHandler(tornado.web.RequestHandler):
10+
class AnalyzeHandler(common.BaseHandler):
1111

1212
RoutePath = r'/analyze'
1313

1414
def get(self):
1515
self.render('analyze.html')
1616

17-
class QueryHandler(tornado.web.RequestHandler):
17+
class QueryHandler(common.BaseHandler):
1818

1919
RoutePath = r'/query'
2020

2121
def get(self):
2222
self.render('query.html')
2323

24-
class HealthHandler(tornado.web.RequestHandler):
24+
class HealthHandler(common.BaseHandler):
2525

2626
RoutePath = r'/health'
2727

speleo.service/main.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import inspect
44
import logging
55
import handlers
6+
import models
67
import tornado.ioloop
78
import tornado.web
89
from tornado.options import define, options
@@ -13,38 +14,56 @@
1314
define("debug", default=False, help="Set to true to enable debugging", type=bool)
1415
define("port", default=8888, help="The port to run the service on", type=int)
1516
define('security', default='none', help='none|ldap', type=str)
17+
define('database', default='sqlite:////tmp/example.db', help="The database connection string to use", type=str)
1618
define('ldap_host', default='ldap://127.0.0.1', help='The ldap service to authenticate against', type=str)
1719
define('ldap_domain', default='', help='The domain to authenticate users under', type=str)
1820
define('ldap_basedn', default='', help='The base dn to search for users under', type=str)
1921
define('ldap_bind_dn', default=None, help='A priviledged user to perform ldap binds', type=str)
2022
define('ldap_bind_password', default=None, help='The priviledged user password', type=str)
2123

2224
# ------------------------------------------------------------
23-
# settings
25+
# application
2426
# ------------------------------------------------------------
25-
settings = {
26-
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
27-
'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
28-
'login_url':'/auth/login',
29-
'cookie_secret':'something random should go here',
30-
'xsrf_cookies':True,
31-
'debug': options.debug,
32-
}
27+
class SpeleoApplication(tornado.web.Application):
3328

34-
# ------------------------------------------------------------
35-
# handlers
36-
# ------------------------------------------------------------
37-
search = inspect.getmembers(handlers)
38-
routes = [(h.RoutePath, h) for n,h in search if 'Handler' in n]
29+
def __init__(self):
30+
# ----------------------------------------------------
31+
# settings
32+
# ----------------------------------------------------
33+
root_path = os.path.dirname(__file__)
34+
settings = {
35+
'static_path': os.path.join(root_path, 'static'),
36+
'template_path': os.path.join(root_path, 'templates'),
37+
'login_url':'/auth/login',
38+
'cookie_secret':'something random should go here',
39+
'xsrf_cookies':True,
40+
'debug': options.debug,
41+
}
42+
43+
# ----------------------------------------------------
44+
# handlers
45+
# ----------------------------------------------------
46+
search = inspect.getmembers(handlers)
47+
routes = [(h.RoutePath, h) for n, h in search if 'Handler' in n]
48+
49+
# ----------------------------------------------------
50+
# shared
51+
# ----------------------------------------------------
52+
self.database = models.get_database(options.database, options.debug)
53+
#self.cache = memcache.Client([options.cache], debug=options.debug)
54+
55+
# ----------------------------------------------------
56+
# initialize
57+
# ----------------------------------------------------
58+
tornado.web.Application.__init__(self, routes, **settings)
59+
logging.debug('Installed Routes')
60+
for route in routes: logging.debug(route)
3961

4062
# ------------------------------------------------------------
41-
# main service
63+
# service
4264
# ------------------------------------------------------------
4365
if __name__ == "__main__":
4466
tornado.options.parse_config_file('settings.py')
4567
tornado.options.parse_command_line()
46-
application = tornado.web.Application(routes, **settings)
47-
logging.debug('Installed Handlers')
48-
for route in routes: logging.debug(route)
49-
application.listen(options.port)
68+
SpeleoApplication().listen(options.port)
5069
tornado.ioloop.IOLoop.instance().start()

speleo.service/models.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from sqlalchemy import create_engine
2+
from sqlalchemy import Column, Integer, String, DateTime, Boolean
3+
from sqlalchemy.ext.declarative import declarative_base
4+
5+
# ------------------------------------------------------------
6+
# utility methods
7+
# ------------------------------------------------------------
8+
Base = declarative_base()
9+
10+
def get_database(connection, debug=False):
11+
''' Retrieve a database instance from the given config
12+
13+
:param connection: The connection string to the database
14+
:param debug: Set to true to debug the operations
15+
'''
16+
engine = create_engine(connection, convert_unicode=True, echo=debug)
17+
Base.metadata.create_all(engine)
18+
models.init_db(engine)
19+
return scoped_session(sessionmaker(bind=engine))
20+
21+
22+
# ------------------------------------------------------------
23+
# models
24+
# ------------------------------------------------------------
25+
class User(Base):
26+
27+
__tablename__ = 'users'
28+
29+
id = Column(Integer, primary_key=True)
30+
username = Column(String(30), nullable=False)
31+
first_name = Column(String(30), nullable=False)
32+
last_name = Column(String(30), nullable=False)
33+
email = Column(String(75), nullable=False)
34+
password = Column(String(128), nullable=False)
35+
36+
def __repr__(self):
37+
return "<User(%s)>" % (self.username)

0 commit comments

Comments
 (0)