-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.py
84 lines (61 loc) · 1.84 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import cherrypy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column
from sqlalchemy.types import String, Integer
from cp_sqlalchemy import SQLAlchemyTool, SQLAlchemyPlugin
Base = declarative_base()
HERE = os.path.dirname(os.path.abspath(__file__))
class LogMessage(Base):
__tablename__ = 'logs'
id = Column(Integer, primary_key=True)
value = Column(String)
class Root(object):
@property
def db(self):
return cherrypy.request.db
def page(self):
return '''
<html>
<head><title>CherryPy-SQLAlchemy Example</title></head>
<body>
<form action='/' method='post'>
<input type='text' name='message' /><input type='submit' value='add' />
</form>
%s
</body>
<html>
'''
@cherrypy.expose
def index(self, message=None, submit=None):
if message:
self.db.add(LogMessage(value=message))
self.db.commit()
raise cherrypy.HTTPRedirect('/')
page = self.page()
ol = ['<ol>']
for msg in self.db.query(LogMessage).all():
ol.append('<li>%s</li>' % msg.value)
ol.append('</ol>')
return page % ('\n'.join(ol))
def run():
cherrypy.tools.db = SQLAlchemyTool()
app_config = {
'/': {
'tools.db.on': True,
}
}
cherrypy.tree.mount(Root(), '/', config=app_config)
dbfile = os.path.join(HERE, 'log.db')
if not os.path.exists(dbfile):
open(dbfile, 'w+').close()
sqlalchemy_plugin = SQLAlchemyPlugin(
cherrypy.engine, Base, 'sqlite:///%s' % (dbfile),
echo=True
)
sqlalchemy_plugin.subscribe()
sqlalchemy_plugin.create()
cherrypy.engine.start()
cherrypy.engine.block()
if __name__ == '__main__':
run()