durusmail: quixote-users: Form widgets usable outside context of a Publisher?
Form widgets usable outside context of a Publisher?
2005-08-15
Re: Form widgets usable outside context of a Publisher?
2005-08-15
Form widgets usable outside context of a Publisher?
2005-08-15
Form widgets usable outside context of a Publisher?
2005-08-15
Form widgets usable outside context of a Publisher?
mso@oz.net
2005-08-15
Al Pacifico wrote:
> The problem is that widget.py imports get_request which is from the
> Publisher class. In this context, there is no Publisher instance. The
> funny
> thing, though, is that once you set an attribute of the widget, the error
> goes away. For example:

It's not setting an attribute that makes the error going away, it's
calling .render() a second time.  The first time through, .render() calls
Widget.parse() (widget.py line 121) to get the current value.  ._parse()
sets the value from the GET/POST variables and sets ._parsed to True.
Subsequent times, ._parsed is true so it returns the cached value.  The
value defaults to None (or whatever you've set in the constructor), so
there are a couple ways around this:

    # Tell the widget it's been parsed.
    x = StringWidget('name', size=20)
    x._parsed = True
    print x.render()

    # Supply a request so it doesn't ask the Publisher for one.
    from quixote.http_request import HTTPRequest
    dummy = HTTPRequest(stdin=None, environ={})
    x = StringWidget('name', size=20)
    x.parse(dummy)
    print x.render()

Granted, the second one is ugly, and you may get into trouble with .stdin
being None.

+1 for making the widgets independent of the Publisher.  Perhaps
Widget.parse() should check whether there's a Publisher and return the
existing value if not.

--
-- Mike Orr 

reply