Something that bothers me about the current setup is that the URLs are not as nice as they could be. Rather than "/?pagename=Foo", wouldn't "/Foo" be nicer? Luckily, this is really easy to do. We just add a default method that CherryPy will call whenever no other method matches:
And now, we can change the HTTPRedirect in the save method to turbogears.url("/%s" % pagename).
WikiWords have also been described as WordsSmashedTogether. A typical wiki will automatically create links for these words when it finds them. Sounds like a good idea, and this sounds like a job for a regular expression. Start by adding an import to our controller:
import re
A WikiWord is a word that starts with an uppercase letter, has a collection of lowercase letters and numbers followed by another uppercase letter and more letters and numbers. Here's a regular expression that meets that requirement:
wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)")
Put that line just above the Root controller class. Then, we need to use this regex. Just below the publish_parts line of the index method (that's the one that generates the HTML), add this:
\1' % root, content)]]>
Go ahead and edit your front page to include a WikiWord. When the page is displayed, you'll see that it's now a link. You probably won't be surprised to find that clicking that link produces an error.
It's time to add a check for pages that don't exist. I'll keep the approach here simple: if the page doesn't exist, you get an edit page to use to create it. In the index method, we'll check to see if the page exists. If it doesn't, we'll redirect to a new notfound method. Change the index method like this:
We'll need to import the exception as well. Add this to the top of the file:
And, we'll add the not found method. We'll just reuse the edit template.
Notice that the dictionary includes a variable called "new" for use in the template. Change the edit method to set new=False when it returns its dictionary:
return dict(pagename=page.pagename, data=page.data, new=False)
We need to be able to save a new item, so, we'll have to change the save method like this:
As we saw when we created the original FrontPage via the shell, instantiating an SQLObject is enough to INSERT into the database.
The one last thing we need to do is pass along the "new" variable to the save method via the form in edit.kid:
]]>
Give it a try! You should be able to create new pages now.