Very very cool and useful stuff Matthew!!
What about putting these things into a CookBook page? ;-)
Ciao
Michele
Matthew Bevan wrote:
> Working as much as I have on widgets (use of, not creation of) I've come up
> with a few useful tidbits I'd like to share:
>
> I define a SingleSelectField as:
>
> > W.SingleSelectField('author', label='Author',
> > options=make_list(model.Account, 'No Author'),
> > help_text='Select an author to display in byline information.'),
>
> The make_list function and build_list method are the trick, here. The
> make_list function prepends an optional empty value to an existing callable
> (which should return a list of tuples). It also allows you to pass arguments
> to the callable; the primary reason for the function.
>
> > def make_list(c, none_label=None, *args, **kwargs):
> > def _call():
> > return [ [], [(None, none_label)] ][none_label is not None] + \
> > c.build_list(*args, **kwargs)
> > return _call
>
> The build_list method of my SQLObject classes return a list of tuples suitable
> for use in a select-type widget (single, multiple, radio, or checkbox) by
> performing a simple .select() on itself, passing any arguments it receives to
> the select method.
>
> > @classmethod # of Account
> > def build_list(self, *args, **kwargs):
> > return [(a.id, a.full_name) for a in self.select(*args, **kwargs)]
>
> An alternative and generic build_list function is: (and can be used alongside
> classes using the above method)
>
> > @classmethod # of anything
> > def build_list(self, field, *args, **kwargs):
> > return [(a.id, getattr(a, field)) for a in self.select(*args, **kwargs)]
>
> Any additional arguments to make_list are automatically passed, thus, to the
> select method, allowing you to use SQLBuilder in your arguments to the
> widget. Take the following examples:
>
> > from model import *
> > from datetime import datetime
> >
> > ..., options=make_list(Account, None,
> > Account.q.enabled, orderBy=Account.q.full_name), ...
> >
> > ..., options=make_list(Language, "Unspecified", Language.q.strings > 0), ...
> >
> > # Alternate method for accounts:
> > ..., options=make_list(Account, None, 'user_name'), ...
>
> Note that you can't use both methods within a single class.
>
> Enjoy!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---