TurboGears Upgrade Guide
| Status: | Draft |
|---|
Overview
The easiest way to upgrade TurboGears is to download the latest tgsetup.py and run it! This will update all of the parts of TurboGears that need updating.
Please also be aware of backwards compatibility issues. These are addressed in the remainder of the guide.
Updating from 1.0.3 to 1.0.4
Release 1.0.4 is only a minor release, so there are no incompatible changes.
There is however a change that affects only the start script of newly quickstarted projects, old projects will still work as they are with 1.0.4. If you still want to update an old project to the new behaviour, do the following.
- Quickstart a new project with the same name and settings as your old project somwhere in a temporary directory. Substitute <project> and <package> with your actual project resp. package name below.
- From the new project, copy the file <project>/<package>/commands.py to the corresponding location in your old project.
- From the new project, copy the file <project>/start-<project>.py to the corresponding location in your old project, overwriting the existing start script.
Open the setup.py file in your old project with an editor and change the following (again substituting <package> with the actual package name):
Delete the follwing line:
scripts = ["start-<package>.py"],
Add the following to the end of the parameters of the setup() call:
entry_points = { 'console_scripts': [ 'start-<package> = <package>.commands:start', ], },
Other things you should do when upgrading:
- Update your model.py file to align with the model.py boiler plate code in the new project. If you are using SQLAlchemy, you should now import and use metadata and mapper from turbogears.database instead of sqlalchemy.orm (you have to use this mapper at least for the TurboGears identity tables).
- Take over possibly missing logger configuration from the config files <project>/dev.cfg and <project>/test.cfg.
For more information, please see the Changelog.
Upgrading from SQLAlchemy 0.3 to 0.4
When your project uses SQLAlchemy and you want to upgrade from version 0.3.x to 0.4.x, you need to change a few import statements. There are no TurboGears specific changes.
Add the following line to model.py:
from sqlalchemy.orm import mapper, relation, create_session
(Leaving from sqlalchemy import * as is should also work.)
To controllers.py:
from sqlalchemy.exceptions import SQLError
Updating from 0.9a6+ to 1.0.x
These instructions cover updating existing TurboGears projects that were started with TurboGears versions 0.9a6-1.0b2 to work with TurboGears version 1.0.x, up to and including the latest release 1.0.4.
To be done...
Updating from 0.9a5 to 0.9a6
i18n.runTemplateFilter in the config file has been renamed i18n.run_template_filter (#796)
For people using FastData (experimental): get_add_url, get_edit_url and get_delete_url all are passed the row instead of the ID now, allowing you to use something other than the ID if desired.
In widgets, if you were using a dictionary as params be aware that now the dictionary is not updated at construction or display/render time but simply replaced with the new one. If you were using it to provide default attributes for your widget, take a look at how the TableForm does that.
QUICKSTART: quickstart projects that used identity would generate a table called user, which is invalid for some databases. The new quickstart model.py generates tables called tg_user. (#805
If you have an old project with a model.py generated by a previous version, change the table name of the User object to tg_user by adding the following in the class definition:
class sqlmeta: table="tg_user"for SQLObject models. The SQLAlchemy model structure has changed several times since then, so you should probably just generate a new project and copy the identity classes from there.
Updating from 0.9x to 0.9a5
Identity has changed to use PEP 8 (Python standard coding convention) style names. SQL migration scripts are available to help convert your database to the new names.
Additionally, your identity login screen must have a button that is named "login" or another name specified in the identity.form.submit configuration variable.
Updating from 0.9a1 to 0.9a2
TurboGears 0.9 introduced a new config file format that allowed you to do basically anything that can be done in Python. The modules looked mostly like INI files, but were actually Python modules being executed in a slightly altered namespace.
It turns out that this had issues with Python 2.3, and some other minor issues that arose because these files ended in .py, but were not truly importable Python modules.
For these reasons, TurboGears has switched back to the original CherryPy-style config file in 0.9a2. The configuration change that persists between TurboGears 0.8 and 0.9 is that configuration is now split between two files: a deployment config file (dev.cfg, prod.cfg) and a package config file: yourpackagename/app.cfg.
There are a few minor, manual steps required to shift to the old/new config format if you were using 0.9 prior to 0.9a2:
- Rename devcfg.py to dev.cfg.
- Rename prodcfg.py to prod.cfg.
- mkdir <yourpackage>/config.
- Create an empty file in <yourpackage>/config called __init__.py (to make yourpackage/config a proper Python package).
- Rename <yourpackage>/config.py to "<yourpackage>/config/app.cfg".
Edit your start-<yourproject>.py file:
- Change the references to devcfg.py to dev.cfg.
- Change the references to prodcfg.py to prod.cfg.
- Change the references to <yourpackage>.config to <yourpackage>.config.app
- Edit dev.cfg and prod.cfg to add [global] at the top of the file.
Edit <yourpackage>/config/app.cfg:
- Add [global] at the top of the file
- Change any path("something") to [something] (note the removal of quotes)
Change calls to absfile to use %(top_level_dir)s. This diff will give you an idea of the necessary change:
-static_filter.dir = absfile("${package}", "static") +static_filter.dir = "%(top_level_dir)s/static"
Updating from 0.8 to 0.9
There are a number of changes that will need to be made to your project in order to upgrade to TurboGears 0.9.
Upgrade Project Files and Configuration Files
One of the big changes in 0.9 is how configuration files work. Configuration files are now python modules, which provide a great deal of flexibility. The old configuration files will still work, but all ongoing development is going to be in the new format, and migration is relatively painless. The easiest way to add the new configuration files is to run the following command (in the base directory of your project):
tg-admin update
Running this command is going to try and upgrade (and overwrite!) existing files, so be careful! tg-admin will prompt you with 'Y/n/d/b' before overwriting anything. Y is yes (overwrite), n is no (don't overwrite), d will show you a diff of the new and old file, and b will make a backup of your existing file before overwriting. Briefly, files that are going to be updated are:
- controllers.py Some methods have been added for the new Identity feature, and turbogears.controllers.RootController is used instead of the deprecated version. It is probably easiest to say 'n' (don't overwrite). Later, if you like, you can create a new project (using 'tg-admin quickstart') to get a pure version of controllers.py and merge in these changes.
- model.py You can probably leave this alone (answer 'n'). The only change is to import some files for Identity, and you can copy this import from a newly quickstarted project later if you plan on using Identity.
- master.kid Changes to this file aren't critical, so 'n' is probably a safe choice. Again, there are Identity specific changes that you may want to merge in later.
- setup.py This one you should probably answer 'b' (backup). If you have made changes (which isn't likely), you can merge them in later.
- dev.cfg and prod.cfg The config files have been separated out into deployment config (held in these two files which you originally had) and an application config that would be the same regardless of where the application is deployed. You don't need to make this transition if you don't want to. You can safely press "n" to leave these files alone.
A new config file will be placed in <yourpackage>/config/app.cfg. Configuration items that are applicable to both development and production now go into app.cfg. The reason for this is that it allows your project to be conveniently packaged as an egg if you choose to do so, with the project specific configuration intact.
tg-admin update does not overwrite your old dev.cfg or prod.cfg. You can call <yourprojectname>-start.py to start up your project using the old config files, or you can use start-<yourprojectname>.py to start using the new configuration files. Migration to the new files should be straightforward, and is probably as simple as copying the database configurations from the old files to the new ones. Once you have fully migrated to 0.9, you should delete dev.cfg, prod.cfg, and yourprojectname-start.py.
Make Static File Paths Absolute
CherryPy 2.2 requires that paths to static files be absolute. While you might think this would prevent projects from being deployed on different machines, TurboGears provides a function 'absfile' to help maintain portability. If your package name is 'bigboy', in 0.8 you would say:
# <=0.8 usage only [/static] staticFilter.on = True staticFilter.dir = "static"
Now, you must now specify:
# 0.9 usage [/static] staticFilter.on = True staticFilter.dir = "%(top_level_dir)s/static"
top_level_dir is the directory where the top level of your project is located. You can also use package_dir which is the same directory the config file is located in.
tg-admin update should take care of the /static and /favicon.ico cases (in the new config files), but if you have any custom static files or folders, you will need to make a change to make sure that the file paths are absolute.
Methods Must Explicitly Allow JSON
In order to prevent accidentally exposing information that perhaps everyone shouldn't see, exposed methods no longer automatically provide JSON output when the url contains 'tg_format=json'. Instead, you must explicitly state when a method is going to provide JSON output. This can be done two different ways:
Enable JSON in app.cfg If you would like to have the same behavior as existed in v0.8 (i.e. JSON output is always exposed), you can globally enable it in your app.cfg with:
tg.allow_json = True
Enable JSON only for specific methods If you would like to only let specific methods return JSON (which is probably smart from a security perspective), you can do so using the allow_json parameter in the @turbogears.expose decorator, like this:
# This method can return html or json, depending on the url that is called @turbogears.expose(template=".templates.my_pretty_view", allow_json=True) def pretty(self): return {"msg":"I'm so pretty."}Or, if you really only care to have JSON output for a method, you can use the format parameter, like this:
# This method only returns json; #'tg_format=json' isn't necessary in the url. @turbogears.expose(format="json") def modestly_hot(self): return {"msg":"I'm only sorta hot. I don't mess around."}
Changing from std. to tg. in your templates
The std object that appears in your template namespace that holds useful values and functions has been renamed tg. You should be able to do a search and replace in your template files to swap std. with tg..
Other Incompatibilities
There are a couple other changes that probably won't impact most projects, but will cause things to not work if you are using them.
- The server.webpath configuration variable will not only properly set outgoing URLs, but will also "fix" incoming URLs if TurboGears is running at some path underneath another webserver. If you were previously running a CherryPy filter to handle this, you no longer need to.
- Previously, if you were using a FormEncode Schema for validation for an exposed method, validation would fail if a value was missing but the method had a default value for that parameter. Now, if that value is missing, the method will get called with the default just as it is normally called in Python.
Deprecated Items
The following items will work in 0.9, but will be changing in the future. You should migrate away from any usage of these items as you are able to.
- For clarity's sake, turbogears.controllers.Root is now turbogears.controllers.RootController.
- Error handling has been greatly improved. Use of the validation_error method has been deprecated. It will still be called if it exists, but a DeprecationWarning will be displayed.
- turbogears.tests.util has been moved to turbogears.testutil.
- CherryPy has a list of names that are deprecated in order to comply with PEP-8.
- expose(html=...) is now deprecated in favor of expose(template=...).
Updating from 0.5 to 0.8
0.8 has a number of minor backwards incompatible changes. There are only two such changes that you'll likely need to take action on.
The primary one is the renaming of the turbogears* variables. You'll want to do project wide search/replace for these values.
| Search for | Replace with |
|---|---|
| turbogearshtml | tg_template |
| turbogearsfmt | tg_format |
| turbogearsflash | tg_flash |
| turbogearsjs | tg_js |
The other noticeable change that you'll come across is that turbogears-admin.py has been renamed to tg-admin. Note that in addition to the base name change, you no longer need to add the .py on the end.
Though they are less likely to pose problems for you, you may wish to check out the complete changelog for additional changes.