dynamic construction of forms

0 views
Skip to first unread message

david

unread,
Jun 3, 2007, 5:31:16 PM6/3/07
to ToscaWidgets-discuss
Hi,

I am just getting started with TW, using Pylons. I have created a form
class - as an example, I have:

class AddCommentForm(TableForm):
class fields(WidgetsList):
id = HiddenField(default="I'm hidden!")
author = TextField(
validator = UnicodeString(not_empty=True),
)
... etc

This works great (using the process as described on the pylons site -
http://docs.pythonweb.org/display/pylonscookbook/An+Alternative+ToscaWidgets+Setup+with+Mako)

However, if I don't know which fields I need beforehand ("id",
"author", etc.), how would I create this class dynamically at run
time? None of the obvious ways seem to work...

Thanks!

David

Alberto Valverde

unread,
Jun 10, 2007, 9:20:34 AM6/10/07
to toscawidge...@googlegroups.com

There are various ways you could do this. One would be to dynamically
create a widgets subclass and instantiate it or a better alternative
would be to create a list with all the widgets and pass it to the
constructor. Something like:

fields = generate_fields(model.Something)
form = TableForm("a_form", fields=fields)

There's some work in this direction at TGFastData but it only handles
TG widgets and SQLObject. It should be fairly easy to adapt to TW
and it will probably be a useful addition to twForms since the
needed dependencies (RuleDispatch) are already required by TW. Here's
the source [1} in case you want some ideas (take a look at the
fields_for function)

Alberto

[1] http://tinyurl.com/29zmlq

Timothy Sweetser

unread,
Jun 11, 2007, 12:37:43 PM6/11/07
to ToscaWidgets-discuss
I've been working on something along these lines for a couple of days.
Should be in a usable state shortly.

david

unread,
Jun 13, 2007, 9:51:42 PM6/13/07
to ToscaWidgets-discuss
I'd love to see it when you have something. Thanks!
David

On Jun 11, 12:37 pm, Timothy Sweetser <transitauthor...@gmail.com>
wrote:

Timothy Sweetser

unread,
Jun 14, 2007, 11:17:56 AM6/14/07
to ToscaWidgets-discuss
Yup, definately. It's working right now. I'm just moving things around
a bit and adding some different functionality. I'll probably post a
link on here later today when I'm happy with it.

Alberto Valverde

unread,
Jun 14, 2007, 12:04:56 PM6/14/07
to toscawidge...@googlegroups.com

On Jun 14, 2007, at 5:17 PM, Timothy Sweetser wrote:

>
> Yup, definately. It's working right now. I'm just moving things around
> a bit and adding some different functionality. I'll probably post a
> link on here later today when I'm happy with it.

Cool. Looking forward to see it :)

Alberto

david

unread,
Jul 4, 2007, 2:57:13 PM7/4/07
to ToscaWidgets-discuss
Any more news on this? Anxiously waiting :)

On Jun 14, 11:17 am, Timothy Sweetser <transitauthor...@gmail.com>
wrote:

HiTekElvis

unread,
Jul 5, 2007, 7:15:46 PM7/5/07
to ToscaWidgets-discuss
Yeah, it looks like we all are. :-P

-Josh

Timothy Sweetser

unread,
Jul 7, 2007, 2:07:19 PM7/7/07
to ToscaWidgets-discuss
Hah, sorry, guys... Got caught up in work projects and didn't have any
time to finish it. I've gotten back to it the past couple of days and
am (desperately) hoping to throw it up here tonight.

Timothy Sweetser

unread,
Jul 8, 2007, 10:23:06 PM7/8/07
to ToscaWidgets-discuss
Ok, here we go! Here's the beta code for generating form fields using
SQL Alchemy Mappers:

http://wistful.googlecode.com/files/fieldfactory.py

To use:

1. Toss the module somewhere in your project (lib/ with Pylons apps).
2. Import the generate_form_fields function and call it with your sqla
Mapper/Elixir Entity as a parameter. If you don't want it to guess
which fields are passwords, zip codes, email, etc, set the optional
apply_rules param to False.
3. It'll return a list of fields, which you can then drop into your
form, a la Alberto's example in the second message of the thread.

Let me know if it works, fails, or is messed up somewhere... Comments
and feedback are much appreciated. Hopefully with a little work and
some decent docstrings this can get integrated into TW.

It was designed with some code generation functionality, so I'm
planning on writing up some TG2/Pylons-compatible paster commands
that'll use it to do basic form scaffolding. Assuming the current code
isn't horrendously ill-conceived, those'll be next on my list.

On Jun 3, 5:31 pm, david <dgel...@gmail.com> wrote:
> Hi,
>
> I am just getting started with TW, using Pylons. I have created a form
> class - as an example, I have:
>
> class AddCommentForm(TableForm):
> class fields(WidgetsList):
> id = HiddenField(default="I'm hidden!")
> author = TextField(
> validator = UnicodeString(not_empty=True),
> )
> ... etc
>

> This works great (using the process as described on the pylons site -http://docs.pythonweb.org/display/pylonscookbook/An+Alternative+Tosca...)

Timothy Sweetser

unread,
Jul 9, 2007, 12:26:06 PM7/9/07
to ToscaWidgets-discuss
A couple more thoughts:
1. Some of the validator/widget args may be unnecessary or redundant.
I'll try to weed those out. If you notice any, let me know.
2. There are a couple of cases where compound or multiple validators
could get used. I may add that functionality.
3. Would it be nice to be able to define Fieldsets? (Ditto for child/
repeated fieldsets for managing relationships, assuming it's
workable.)
4. I'm adding code for manually overriding properties for specific
fields at generation time.
5. In general, I'm working on making it easier to customize the
factory and handle a wider variety of use cases. If you see anywhere
the code needs more flexibility, shout it out.

On Jul 8, 10:23 pm, Timothy Sweetser <transitauthor...@gmail.com>
wrote:

Timothy Sweetser

unread,
Jul 10, 2007, 9:33:32 PM7/10/07
to ToscaWidgets-discuss
Actually, in the interests of #5 on that list, the code that
interfaces with the sqla column is getting refactored to make it more
generic. Any comments so far?

On Jul 9, 12:26 pm, Timothy Sweetser <transitauthor...@gmail.com>

Alberto Valverde

unread,
Jul 17, 2007, 7:11:50 AM7/17/07
to toscawidge...@googlegroups.com

On Jul 11, 2007, at 3:33 AM, Timothy Sweetser wrote:

>
> Actually, in the interests of #5 on that list, the code that
> interfaces with the sqla column is getting refactored to make it more
> generic. Any comments so far?

Although I haven't actually run the code yet it looks very
interesting! Something like this could be a very useful addition to
twForms...

I'll let you know my thoughts as soon as I get a chance to give it a
try. Has anyone tried it out that can comment?

Thanks for sharing it!

Alberto

BruceC

unread,
Jul 19, 2007, 1:17:35 AM7/19/07
to ToscaWidgets-discuss
I gave it a quick test & it looked fantastic...I plugged it in &
produced a form straight away direct from a test model data source.

I've got a limited amount of time before our project has to go live, &
so I'm not confident enough to use this straight away, though I *was*
interested in how one might go about making any changes to the display
of form fields before they get displayed. At least when you explicitly
design a form, you can customise the appearance of individual fields...
(eg, to turn a foreign key integer data source into a select list)...

I am keeping my eye on this though - looks fantastic :)

Timothy Sweetser

unread,
Jul 19, 2007, 5:11:00 PM7/19/07
to ToscaWidgets-discuss
You can also use it to generate the code instead of the objects, if
that might be more useful in the short term. In a shell that has
access to your models and the fieldfactory module, just import
render_form_fields from the fieldfactory module and pass that function
a model. It'll return a tuple of the list of imports, a list of any
standalone code (say, for the lambdas used to update single/multi-
select fields), and the list of field definitions. From there, it's
just a matter of printing out each of the lists and copying the code
to the file where you define your form.

I should be posting some improvements in the next few days.

Reply all
Reply to author
Forward
0 new messages