[Zope3-dev] A slight change in AddView.createAndAdd
Jim Fulton
jim at zope.com
Mon Jan 31 17:42:16 EST 2005
Gintautas Miliauskas wrote:
> Hello,
>
> I would like to suggest a slight modification to AddView.createAndAdd()
> (the class is in zope.app.form.browser.add).
>
> In the code, (before adding the object and after adding the object) the
> content object is unconditionally adapted to self.schema in two places.
> However, the adapter object is only used conditionally (if
> _set_before_add / _set_after_add are set).
>
> In our application we are subclassing AddView but the createAndAdd()
> implementation is causing a bit of a pain, because it is forcing us to
> use adapters, whereas it would be much easier and more lightweight to
> override create() (in fact, we already override add()). I don't want to
> just override the createAndAdd() method and duplicate all its code,
> because this method is not short or trivial, and it does low-level
> stuff.
>
> Is there a specific reason why the adaptation is not done conditionally?
> If not, can I move the adaptation code into the if: corresponding
> blocks? It would make life easier in some cases, such as the one
> outlined above.
>
> A patch would look like this:
>
> Index: src/zope/app/form/browser/add.py
> ===================================================================
> --- src/zope/app/form/browser/add.py (revision 28990)
> +++ src/zope/app/form/browser/add.py (working copy)
> @@ -88,11 +88,11 @@
> kw[str(name)] = data[name]
>
> content = self.create(*args, **kw)
> - adapted = self.schema(content)
>
> errors = []
>
> if self._set_before_add:
> + adapted = self.schema(content)
> for name in self._set_before_add:
> if name in data:
> field = self.schema[name]
> @@ -108,9 +108,8 @@
>
> content = self.add(content)
>
> - adapted = self.schema(content)
> -
> if self._set_after_add:
> + adapted = self.schema(content)
> for name in self._set_after_add:
> if name in data:
> field = self.schema[name]
>
Looks good.
Jim
--
Jim Fulton mailto:jim at zope.com Python Powered!
CTO (540) 361-1714 http://www.python.org
Zope Corporation http://www.zope.com http://www.zope.org
More information about the Zope3-dev
mailing list