Sat, 19 May 2007

passing options to toscawidgets

The syntax for passing options to a widget with toscawidgets is slightly different than it was with the turbogears widgets. This actually had me google searching for a while to find this so I thought I'd post it here as a full example:

You define your widget in the controllers.py file:
from toscawidgets.core import WidgetsList

from toscawidgets.widgets.forms import SingleSelectField, TableForm, TextField
from formencode.schema import Schema
...
class FilteringSchema(Schema):
filter_extra_fields = True
allow_extra_fields = True

class Fields(WidgetsList):
testselect = SingleSelectField(label="Test:", validator=validators.Int())
name = TextField(validator=validators.NotEmpty(), label="Name:", attrs={'maxlength':128})

testform = TableForm("TestForm", validator=FilteringSchema, fields=Fields(), action="save", submit_text="Submit")

class Root(controllers.RootController):

@expose(html="test.templates.formpage")
def formpage(self):
selectlist = [(1, 'option 1'), (2, 'option 2')]
data = dict(child_args=dict(testselect=dict(options=selectlist)), value=dict(name='Test Guy'))
return dict(form=testform, data=data)


This produces your form and the data to populate your form passed as form and data. In the formpage template you simply display the form with the following line:
${form.display(**data)}


This should produce a form with a select field showing option 1 and option 2 in a dropdown, and a name text field filled in with the value Test Guy.

posted at: 01:45 | path: /turbogears | permanent link to this entry


Tue, 15 May 2007

turbogears kid to genshi

I've started working on an existing turbogears application recently and we're about to do a pretty major update. From what I've read genshi is taking over from where kid left off and so before I go any further with kid I thought it best to migrate the project to genshi before we get into styling the site and so on. I ran into a few little problems with widgets and some syntax changes between the two which I thought I'd outline after piecing together the following from a few different google searches. I made the following change using turbogears 1.0.2.2:

Set turbogears to use genshi in your dev.cfg (or app.cfg):
tg.defaultview = "genshi"


Rename your kid templates from .kid to .html

Modify your template headers should look similar to this:
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude">

<xi :include href="master.html" />

notice the use of Xinclude instead of py:extends

Modify your master.html file if your using one close to the stock from tutorial:
old: <head py:match="item.tag=='{http://www.w3.org/1999/xhtml}head'" py:attrs="item.items()">

new: <head py:match="head" py:attrs="select('@*')">

old: <meta py:replace="item[:]"/>
new: <meta py:replace="select('*')" />

old: <body py:match="item.tag=='{http://www.w3.org/1999/xhtml}body'" py:attrs="item.items()">
new: <body py:match="body" py:attrs="select('@*')">

old: <div py:replace="[item.text]+item[:]"/>
new: <div py:replace="select('*|text()')" />


If you are using turbogears widgets they will not work with genshi and it seems the best solution is to use ToscaWidgets (check the site for installation instructions). I was able to get them working by simply importing the widgets from the toscawidgets module and using the FilteringSchema class. Other than that widgets work just the same as they did before. Here are the changes in the controller.py file:
from toscawidgets.core import WidgetsList

from toscawidgets.widgets.forms import TextField, HiddenField, SingleSelectField, TableForm, TextArea, RadioButtonList
from formencode.schema import Schema
...
class FilteringSchema(Schema):
filter_extra_fields = True
allow_extra_fields = True

class ProjectorFields(WidgetsList):
test1 = TextField(validator=validators.NotEmpty(), label="Test 1:")
test2 = TextField(validator=validators.NotEmpty(), label="Test 2:")

form = TableForm("testform", validator=FilteringSchema, fields=TestFormFields(), action="testform", submit_text="Submit")



The one last thing I ran into was the use of py:attrs has changed to using a dictionary of values:
old: py:attrs="selected=tg.selector(id == testid)"

new: py:attrs="{'selected': tg.selector(id == testid)}"


posted at: 01:56 | path: /turbogears | permanent link to this entry


Sat, 23 Sep 2006

turbogears apache deployment

After a number of frustrating hours trying to get turbogears to run through mod_python in apache and not getting very far, I ended up at the cherrypy website where they recommend deploying it using mod_proxy. This works quite well and was nice any easy to setup. It works by simply running your turbogears application on some port and then having apache direct requests (proxy) to the port that the turbogears application is running. The following documention covers how to set it up:


http://docs.cherrypy.org/apache-proxy


http://docs.turbogears.org/1.0/BehindApache


posted at: 19:35 | path: /turbogears | permanent link to this entry


2019-Sep
2019-Jul
2019-Jun
2019-May
2018-Dec
2018-Jan
2017-Aug
2017-Jun
2017-May
2016-Nov
2015-Dec
2015-Nov
2015-Oct
2015-Jul
2015-Jun
2014-Dec
2012-Oct
2012-Sep
2012-Jun
2012-Feb
2012-Jan
2011-Dec
2011-Sep
2011-Aug
2011-May
2011-Feb
2010-Jun
2010-Apr
2010-Jan
2009-Sep
2009-Jul
2009-May
2009-Jan
2008-Oct
2008-Sep
2008-Jun
2008-May
2008-Jan
2007-Nov
2007-Oct
2007-Aug
2007-Jun
2007-May
2007-Mar
2007-Feb
2007-Jan
2006-Nov
2006-Oct
2006-Sep
2006-Aug
2006-Jun
2006-Apr
2006-Jan
2005-Dec
2005-Nov
2005-Oct
2005-Sep
2005-Aug
2005-Jul
2005-Jun
2005-May
2005-Apr
2005-Mar
2005-Feb
2005-Jan
2004-Dec
2004-Nov
2004-Oct
2004-Sep
2004-Aug
2004-Jul
2004-Jun
2004-May

Powered by PyBlosxom | RSS 2.0