Now that we have all the requisite components in place (that is, tasklistdb.py, taskapp.py
, and tasklistajax.js)
, it is straightforward to put them together. If you run the code below (available as tasklist.py)
and point your browser at http://localhost:8080/, you will get a familiar looking login screen and after entering some credentials (username admin and password admin are configured by default) the resulting screen will look almost the same as the application we developed in the previous chapter, as illustrated in the following screenshot:
For the CherryPy application, we need a root class that can act as the root of the tree of pages we serve the user. Again, we call this class simply Root
and assign an instance of our TaskApp
application to the task variable and an instance of the LogonDB
application to the logon variable (highlighted in the code below). Together with the index()
method, this will create a tree of pages looking like this:
/ /logon /task
If the user starts on the top-level page or on the logon page, he/she will be redirected to the /task
page after successful authentication. Below the /task
page are, of course, the other pages that implement the server side of the AJAX communications like, for example, /task/add
.
Chapter4/tasklist.py
import cherrypy from taskapp import TaskApp from logondb import LogonDB import os.path current_dir = os.path.dirname(os.path.abspath(__file__)) theme = "smoothness" class Root(object): logon = LogonDB() task = TaskApp(dbpath='/tmp/taskdb.db', logon=logon, logoffpath="/ logon/logoff") @cherrypy.expose def index(self): return Root.logon.index(returnpage='/task') if __name__ == "__main__": Root.logon.initdb() def connect(thread_index): Root.task.connect() Root.logon.connect() # Tell CherryPy to call "connect" for each thread, when it starts up cherrypy.engine.subscribe('start_thread', connect) cherrypy.quickstart(Root(),config={ '/': { 'log.access_file' : os.path.join(current_dir,"access.log"), 'log.screen': False, 'tools.sessions.on': True }, '/static': { 'tools.staticdir.on':True, 'tools.staticdir.dir':os.path.join(current_dir,"static") }, '/jquery.js': { 'tools.staticfile.on':True, 'tools.staticfile.filename':os.path.join(current_ dir,"static","jquery","jquery-1.4.2.js") }, '/jquery-ui.js': { 'tools.staticfile.on':True, 'tools.staticfile.filename':os.path.join(current_ dir,"static","jquery","jquery-ui-1.8.1.custom.min.js") }, '/jquerytheme.css': { 'tools.staticfile.on':True, 'tools.staticfile.filename':os.path.join(current_dir,"static", "jquery","css",theme,"jquery-ui-1.8.4.custom.css") }, '/images': { 'tools.staticdir.on':True, 'tools.staticdir.dir':os.path.join(current_dir,"static","jquery", "css",theme,"images") } })
Before the CherryPy application is started in the usual way by calling the quickstart()
function, we first initialize the authentication database and create a function connect()
(highlighted). This is the function we will register with CherryPy to execute each time CherryPy starts a new thread. The function will create a connection to the SQLite databases containing the authentication and tasklist data.
3.147.84.157