[Grok-dev] AddForm Mystery Number

Steve Schmechel steveschmechel at yahoo.com
Mon Jun 1 23:54:51 EDT 2009


Last bit of noise on this - I promise.

After further discovery, it is possible to do something like:

@grok.action('Big name for button', name='ValidNameForAction')

Could the Grok Reference include some of these options or is there a fear creating dependencies on the underlying Zope libraries?

Digging around in all the Zope code to figure out what is meant by:

action(label, **options)
    Decorator that defines an action factory based on a form method. The method receives the form data as keyword parameters.

is not much fun and I am not sure the current description is entirely accurate.  It's not just "form data"; it's also a limited set of valid option parameters defined in Zope.FormLib.


Reference:
http://grok.zope.org/doc/current/reference/decorators.html#grok-action-declare-a-form-submit-handler

From Zope.FormLib

def _action_options(success=None, failure=None, condition=None, validator=None,
                    prefix='actions', name=None, data=None,
                    ):
    return (success, failure, condition, validator, prefix, name, data)


--- On Mon, 6/1/09, Steve Schmechel <steveschmechel at yahoo.com> wrote:

> From: Steve Schmechel <steveschmechel at yahoo.com>
> Subject: Re: [Grok-dev] AddForm Mystery Number
> To: grok-dev at zope.org
> Date: Monday, June 1, 2009, 10:09 PM
> 
> So my real mistake was introducing whitespace into the
> "label" parameter of @grok.action.
> 
> I understand why the whitespace would be a problem, but why
> isn't it simply removed instead of falling all the way back
> to ugly hex encoding?
> 
> Oh well, not a Grok problem.
> 
> I guess I am back to wondering if it is good practice to
> call an auto-generated form view from another
> template.  I could try to link the names together with
> a global constant in my code and expose it as a variable to
> my template.
> 
> Is there a more elegant way to inject an auto-generated
> AddForm into a portion of a Grok view?
> 
> I'm sure there is a whole world of viewlets and such about
> to be recommended to me. :-) 
> 
> More simply, I'll ask again:  
> 
> - Does anyone really use auto-generated forms as a basis
> for real world projects, or are they just cool tools to use
> in demos and example code?
> 
> - Should I just figure that any serious development
> requires a complete set of some flavor of custom page
> templates for each view/form?
> 
> Thanks,
> Steve
> 
> --- On Mon, 6/1/09, Steve Schmechel <steveschmechel at yahoo.com>
> wrote:
> 
> > From: Steve Schmechel <steveschmechel at yahoo.com>
> > Subject: Re: [Grok-dev] AddForm Mystery Number
> > To: grok-dev at zope.org
> > Date: Monday, June 1, 2009, 6:22 PM
> > 
> > 
> > Steve Schmechel wrote:
> > > 
> > > 
> > > When looking at the view created by:
> > > 
> > > class Upload(grok.AddForm):
> > >     grok.context(FileContainer)
> > >     form_fields =
> > >
> >
> grok.AutoFields(zope.app.file.interfaces.IFile).select('data')
> > > 
> > > you can see that the HTML for the submit button
> is
> > generated like:
> > > <input type="submit"
> > id="form.actions.4164642066696c65"
> > > name="form.actions.4164642066696c65" value="Add
> file"
> > class="button" />
> > > 
> > > Where does the "magic number" 4164642066696c65
> come
> > from?
> > > 
> > > 
> > 
> > I found the answer in the Action class of
> > zope/formlib/form.py.  (Seems
> > obvious now.)  So for anyone who was curious, read
> > below.  I also have a
> > question (following the explanation) for anyone with
> time
> > to offer me a
> > suggestion.
> > 
> > When a name is not passed to the constructor, the
> label
> > value is checked
> > against a regex and, if it passes, it is lower-cased
> and
> > used.  If not, the
> > label is hex encoded and that value becomes the name
> used
> > for the "id" and
> > "name" attributes.
> > 
> > Clever, if not entirely expected.  In any case, it
> > should be safe using this
> > value in a page template as long as the decorator on
> the
> > view's add method,
> > @grok.action('Add file'), does not change.  
> > 
> > However, I would prefer something a little more
> > deterministic.  
> > 
> > Is there a way to inject a name value into the
> > grok.AutoFields when
> > "zope.app.file.interfaces.IFile" does not seem to
> support
> > this?
> > 
> > ----------
> > 
> > _identifier = re.compile('[A-Za-z][a-zA-Z0-9_]*$')
> > class Action(object):
> > 
> >     interface.implements(interfaces.IAction)
> > 
> >     def __init__(self, label, **options):
> >         (success, failure, condition,
> > validator,
> >          prefix, name, data
> >          ) =
> > _action_options(**options)
> > 
> >         self.label = label
> > 
> >         [self.success_handler,
> > self.failure_handler,
> >          self.condition,
> > self.validator] = [
> >             _callify(f) for f
> > in (success, failure, condition, validator)]
> > 
> >         if name is None:
> >             if
> > _identifier.match(label):
> >                
> > name = unicode(label).lower()
> >             else:
> >                
> > name = label.encode('hex')
> > 
> > ...
> > 
> > @namedtemplate.implementation(interfaces.IAction)
> > def render_submit_button(self):
> >     if not self.available():
> >         return ''
> >     label = self.label
> >     if isinstance(label,
> > zope.i18nmessageid.Message):
> >         label =
> > zope.i18n.translate(self.label,
> context=self.form.request)
> >     return ('<input type="submit" id="%s"
> > name="%s" value="%s"'
> >             ' class="button"
> > />' %
> >             (self.__name__,
> > self.__name__, label)
> >             )
> > 
> > -- 
> > View this message in context: http://www.nabble.com/AddForm-Mystery-Number-tp23799424p23824030.html
> > Sent from the Grok mailing list archive at
> Nabble.com.
> > 
> > _______________________________________________
> > Grok-dev mailing list
> > Grok-dev at zope.org
> > http://mail.zope.org/mailman/listinfo/grok-dev
> > 
> 
> 
>       
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
> 


      


More information about the Grok-dev mailing list