[Checkins] SVN: z3c.form/trunk/ Initialize the field widget manager

Roger dev at projekt01.ch
Tue Oct 30 13:46:38 UTC 2012


Hi Malthe

I do not understand what you are trying to solve. Can you describe
your Problem with the previous implementation?

I see the supported prefix change, that's fine to me. But moving
the widget setup breaks all our code. Because a lot of our projets
use the updateWidget method for customization.

The widget setup in update is very bad because this now get
called in the super call order now.

Here is some sample code. You can see that the updateWidget
method needs to get cusomized before and after the IWidgets setup:


class IHouse(zope.interface.Interface):
    """House schema"""

    searchText = zope.schema.TextLine(
        title=u'Search Text',
        default=u'')

    doors = zope.schema.Int(
        title=u'Doors',
        default=1)

    garden = zope.schema.Bool(
        title=u'Garden',
        default=False)


class HouseForm(Form):
    """House building form"""

    bigHouse = False

    fields = field.Fields(IHouse).select('searchText', 'doors')

    def setDefaultValues(self):
        pass

    def updateWidgets(self):
        if self.bigHouse:
            self.fields += field.Field(IHouse).select('garden')
            self.fields['garden'].widgetFactory = getBoolWidget
        super(HouseForm, self).updateWidgets()
        if self.searchText:
            self.widgets['searchText'].value = self.searchText

    def update(self):
        super(HouseForm, self).update()
        if not self.actions.executedActions and not self.status:
            self.setDefaultValues()


class SimpleHouseForm(HouseForm):
    """Big house building form"""

    bigHouse = False


class BigHouseForm(HouseForm):
    """Big house building form"""

    bigHouse = True

    def setDefaultValues(self):
        self.widgets['doors'].value = 2

Regards
Roger Ineichen
_____________________________
END OF MESSAGE

> -----Ursprüngliche Nachricht-----
> Von: Malthe Borch [mailto:mborch at gmail.com]
> Gesendet: Dienstag, 30. Oktober 2012 10:36
> An: Adam GROSZER
> Cc: dev at projekt01.ch; Checkins at zope.org; Stephan Richter
> Betreff: Re: AW: [Checkins] SVN: z3c.form/trunk/ Initialize the field widget manager
> 
> I have applied your suggestion and committed this again, along with an
> updated test and interface documentation.
> 
> \malthe
> 
> On 17 September 2012 15:27, Adam GROSZER <agroszer at gmail.com> wrote:
> > Hello,
> >
> > I'd do:
> >
> > Index: src/z3c/form/form.py
> >
> ================================================================
> ===
> > --- src/z3c/form/form.py        (revision 127851)
> > +++ src/z3c/form/form.py        (working copy)
> > @@ -121,10 +121,12 @@
> >          '''See interfaces.IForm'''
> >          return self.context
> >
> > -    def updateWidgets(self):
> > +    def updateWidgets(self, prefix=None):
> >          '''See interfaces.IForm'''
> >          self.widgets = zope.component.getMultiAdapter(
> >              (self, self.request, self.getContent()), interfaces.IWidgets)
> > +        if prefix is not None:
> > +            self.widgets.prefix = prefix
> >          self.widgets.mode = self.mode
> >          self.widgets.ignoreContext = self.ignoreContext
> >          self.widgets.ignoreRequest = self.ignoreRequest
> >
> > of course with some tests added.
> >
> >
> > On 09/12/2012 03:35 PM, Roger wrote:
> >>
> >> Hi Marlthe
> >>
> >> Your commit is incompatilbe with many different concepts.
> >>
> >> For example:
> >> If you have a form which injects an interface with directlyProvides
> >> into the form. And this interface is responsible for adapt other
> >> widgets, this will break because the new widgets don't get
> >> adapted anymore during the updatWidgets call.
> >>
> >> Even more bad, you can't call update for force to switch to new
> >> widgets because this involves widget value extraction and this
> >> could badly fail if your form switched to other widgets based
> >> on action handling.
> >>
> >> What is the reason that you moved the widget setup from
> >> updateWidgets to form.update() call?
> >>
> >> Probably we can find another concept for your usecase?
> >>
> >> Regards
> >> Roger Ineichen
> >> _____________________________
> >> END OF MESSAGE>
> >>
> >>> Betreff: [Checkins] SVN: z3c.form/trunk/ Initialize the field widget
> >>
> >> manager in the
> >>>
> >>> update-step, such that the widget update step is only responsible for
> >>
> >> actually updating
> >>>
> >>> the widgets. The change is required to support the situation where you
> >>
> >> want to
> >>>
> >>> change the co
> >>>
> >>> Log message for revision 127831:
> >>>    Initialize the field widget manager in the update-step, such that the
> >>
> >> widget update
> >>>
> >>> step is only responsible for actually updating the widgets. The change is
> >>
> >> required to
> >>>
> >>> support the situation where you want to change the common widget prefix
> >>
> >> (which
> >>>
> >>> defaults to 'widgets.').
> >>>
> >>> Changed:
> >>>    U   z3c.form/trunk/CHANGES.txt
> >>>    U   z3c.form/trunk/src/z3c/form/form.py
> >>>    U   z3c.form/trunk/src/z3c/form/group.py
> >>>
> >>> -=-
> >>> Modified: z3c.form/trunk/CHANGES.txt
> >>>
> ================================================================
> >>> ===
> >>> --- z3c.form/trunk/CHANGES.txt  2012-09-12 10:17:42 UTC (rev 127830)
> >>> +++ z3c.form/trunk/CHANGES.txt  2012-09-12 11:52:05 UTC (rev 127831)
> >>> @@ -2,6 +2,16 @@
> >>>   CHANGES
> >>>   =======
> >>>
> >>> +In next release ...
> >>> +
> >>> +- Initialize widgets in ``update`` step. The ``updateWidgets`` method
> >>> +  is now responsible only for actually updating the widgets.
> >>> +
> >>> +  This allows updating the common widgets prefix before the individual
> >>> +  widgets are updated, useful for situations where neither a form, nor
> >>> +  a widgets prefix is desired.
> >>> +
> >>> +
> >>>   2.8.3 (unreleased)
> >>>   ------------------
> >>>
> >>>
> >>> Modified: z3c.form/trunk/src/z3c/form/form.py
> >>>
> ================================================================
> >>> ===
> >>> --- z3c.form/trunk/src/z3c/form/form.py 2012-09-12 10:17:42 UTC (rev
> >>
> >> 127830)
> >>>
> >>> +++ z3c.form/trunk/src/z3c/form/form.py 2012-09-12 11:52:05 UTC (rev
> >>
> >> 127831)
> >>>
> >>> @@ -126,8 +126,6 @@
> >>>
> >>>       def updateWidgets(self):
> >>>           '''See interfaces.IForm'''
> >>> -        self.widgets = zope.component.getMultiAdapter(
> >>> -            (self, self.request, self.getContent()),
> >>> interfaces.IWidgets)
> >>>           self.widgets.mode = self.mode
> >>>           self.widgets.ignoreContext = self.ignoreContext
> >>>           self.widgets.ignoreRequest = self.ignoreRequest
> >>> @@ -148,6 +146,8 @@
> >>>
> >>>       def update(self):
> >>>           '''See interfaces.IForm'''
> >>> +        self.widgets = zope.component.getMultiAdapter(
> >>> +            (self, self.request, self.getContent()),
> >>> interfaces.IWidgets)
> >>>           self.updateWidgets()
> >>>
> >>>       def render(self):
> >>>
> >>> Modified: z3c.form/trunk/src/z3c/form/group.py
> >>>
> ================================================================
> >>> ===
> >>> --- z3c.form/trunk/src/z3c/form/group.py        2012-09-12 10:17:42 UTC
> >>> (rev
> >>
> >> 127830)
> >>>
> >>> +++ z3c.form/trunk/src/z3c/form/group.py        2012-09-12 11:52:05 UTC
> >>> (rev
> >>> 127831)
> >>> @@ -34,8 +34,6 @@
> >>>
> >>>       def updateWidgets(self):
> >>>           '''See interfaces.IForm'''
> >>> -        self.widgets = zope.component.getMultiAdapter(
> >>> -            (self, self.request, self.getContent()),
> >>> interfaces.IWidgets)
> >>>           for attrName in ('mode', 'ignoreRequest', 'ignoreContext',
> >>>                            'ignoreReadonly'):
> >>>               value = getattr(self.parentForm.widgets, attrName)
> >>> @@ -44,7 +42,7 @@
> >>>
> >>>       def update(self):
> >>>           '''See interfaces.IForm'''
> >>> -        self.updateWidgets()
> >>> +        super(Group, self).update()
> >>>           groups = []
> >>>           for groupClass in self.groups:
> >>>               # only instantiate the groupClass if it hasn't already
> >>> @@ -121,9 +119,10 @@
> >>>
> >>>           return changed
> >>>
> >>> -    def update(self):
> >>> +    def updateWidgets(self):
> >>>           '''See interfaces.IForm'''
> >>> -        self.updateWidgets()
> >>> +        super(GroupForm, self).updateWidgets()
> >>> +
> >>>           groups = []
> >>>           for groupClass in self.groups:
> >>>               # only instantiate the groupClass if it hasn't already
> >>> @@ -135,7 +134,3 @@
> >>>               group.update()
> >>>               groups.append(group)
> >>>           self.groups = tuple(groups)
> >>> -        self.updateActions()
> >>> -        self.actions.execute()
> >>> -        if self.refreshActions:
> >>> -            self.updateActions()
> >>>
> >>> _______________________________________________
> >>> checkins mailing list
> >>> checkins at zope.org
> >>> https://mail.zope.org/mailman/listinfo/checkins
> >>
> >>
> >>
> >>
> >
> >
> > --
> > Best regards,
> >  Adam GROSZER
> > --
> > Quote of the day:
> > The soldiers fight, and the kings are heroes.
> > - Jewish Proverb
> 
> 
> 
> --
> Au revoir, et tous mes voeux pour un avenir plein de succès et de bonheur ––
> 
> Malthe Borch
> mborch at gmail.com




More information about the checkins mailing list