Survey application with TurboGears and CatWalk
This tutorial will show you how to implement a simple application for creating and managing online surveys using the Web MegaFramework TurboGears and the model browser CatWalk.
I expect that you already have downloaded TurboGears and have completed the 20 minutes wiki tutorial.
This tutorial will go trough the following steps:
Quite some stuff, so let's get started!
Part one
Creating a new project and configuring your database
Create a new project using tg-admin quickstart, and call it survey
Create a database for your project, and configure your dev.cfg and prod.cfg to match your database settings. I'm using postgres and thinks look like this for me:
Implementing your model
Open your model file (model.py) and start defining your model classes.
First our Survey class:
A Survey have a name and description, it can be publish or not, it might have multiple questions and multiple answers.
Lets add our question object:
Questions have some text, a reference to the survey they belong to, eventually some options and they can be one of 5 possible question types.
Our Option object looks like this:
Option seams to be a protected keyword, that's why I specified another name for it. The class contains a text string and a reference to a question
To collect submitted values we need two classes, Answer and AnswerValue.
Be aware that by using DateTime.now as the default for my DateTimeCol I'm adding a dependency to mx.DateTime, so remember to add from mx import DateTime to your model.
I have some problems (at least with postgres) when running tg-admin sql create, due to the order in which tg-admin tries to create the tables. To circumvent it I added the create statements at the bottom of my model file.
You don't need to run tg-admin sql create now, because the tables will be created for you the first time you run the application
You can download a sample model file here
Installing CatWalk
Download the latest version of CatWalk
from http://www.checkandshare.com/catwalk/download.html
.
Save it in the same directory where your model.py file is.
Open your controller.py file and 'mount' CatWalk like this:
Start your engines:
Congratulations, CatWalk should be up an running on http://localhost:8080/catwalk
Are you using the svn version?
If you are, instead of editing your controller you can run the following command from within your project directory This will open CatWalk in a new browser window for you. Nice eh?
If you still want to mount CatWalk to your application the method is almost the same: Be aware that you need to import your model file and pass CatWalk a reference to it.
Security
One word about security. By using CatWalk you are providing an online tool with a great deal of access to your models and valuable data.
By default only localhost (127.0.01) has access to CatWalk. If you want access from another location, add your host name to the allowedHosts list when you mount CatWalk, like this:
Chances are that a lot of people share your host name, providing access to other hosts can compromite your data!!!
You can obscure your installation somehow by using another name for catwalk when mounting it. Like this: CatWalk will now gladly run under