[Zope-dev] schema upgrades with zope.formlib + FIX

2011-02-15 Thread Johannes Raggam
dear zopistas,

while trying to add a new schema field to an already registered plone
portlet, i got following traceback:

2011-02-14 23:11:55 ERROR Zope.SiteErrorLog 1297721515.110.63984381121
http://localhost:8880/info/uber-uns/geschichte/++contextportlets
++plone.rightcolumn/show-galleries-portlet/edit
Traceback (innermost last):
  Module ZPublisher.Publish, line 127, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 47, in call_object
  Module plone.app.portlets.browser.formhelper, line 123, in __call__
  Module zope.formlib.form, line 782, in __call__
  Module five.formlib.formbase, line 50, in update
  Module zope.formlib.form, line 745, in update
  Module zope.formlib.form, line 820, in setUpWidgets
  Module zope.formlib.form, line 408, in setUpEditWidgets
  Module zope.schema._bootstrapfields, line 173, in get
AttributeError: image_size

while - IMO - zope.schema throws that error justifiably, i think
zope.formlib should handle it more gracefully in setUpEditWidgets. i
think, this error comes with every schema upgrade for zope.formlib based
edit-forms. so, what do you think of following patch?


From bcf7e2b7cb94b145cd502ad57e1363f2e6b4a879 Mon Sep 17 00:00:00 2001
From: Johannes Raggam raggam...@adm.at
Date: Tue, 15 Feb 2011 12:38:50 +0100
Subject: [PATCH] after schema upgrades (e.g. for plone.app.portlets),
the edit screen cannot be rendered because there is no v
alue for the new field attribute. in this case, use the default value.

---
 zope/formlib/form.py |6 +-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/zope/formlib/form.py b/zope/formlib/form.py
index 5df88cf..0d3ade3 100755
--- a/zope/formlib/form.py
+++ b/zope/formlib/form.py
@@ -405,7 +405,11 @@ def setUpEditWidgets(form_fields, form_prefix,
context, request,
 
 if ignore_request or readonly or not widget.hasInput():
 # Get the value to render
-value = field.get(adapter)
+try:
+value = field.get(adapter)
+except AttributeError:
+# value not available after schema upgrade
+value = field.default
 widget.setRenderedValue(value)
 
 widgets.append((not readonly, widget))
-- 
1.7.1



i'm not sure if this would cause any unwanted side effects... actually i
think it won't.



regards,
johannes raggam


-- 
johannes raggam / thet
python plone zope development
http://johannes.raggam.co.at/
mailto:johan...@raggam.co.at
http://bluedynamics.com/

___
Zope-Dev maillist  -  Zope-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 https://mail.zope.org/mailman/listinfo/zope-announce
 https://mail.zope.org/mailman/listinfo/zope )


Re: [Zope-dev] schema upgrades with zope.formlib + FIX

2011-02-15 Thread Hanno Schlichting
On Tue, Feb 15, 2011 at 1:21 PM, Johannes Raggam raggam...@adm.at wrote:
 while - IMO - zope.schema throws that error justifiably, i think
 zope.formlib should handle it more gracefully in setUpEditWidgets. i
 think, this error comes with every schema upgrade for zope.formlib based
 edit-forms. so, what do you think of following patch?

The usual approach is to add new attributes as class attributes to the
persistent object. Or write an actual database upgrade step, either
via something like zope.generations or with a GenericSetup upgrade
step.

But for zope.formlib it make indeed make sense to be more lenient
here. So +0 on your patch.

Hanno
___
Zope-Dev maillist  -  Zope-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 https://mail.zope.org/mailman/listinfo/zope-announce
 https://mail.zope.org/mailman/listinfo/zope )


Re: [Zope-dev] schema upgrades with zope.formlib + FIX

2011-02-15 Thread Johannes Raggam
well, for schema upgrades, also following patch seems to be necessary:


From c11064b8d8a7607d41071250f587f097f8c130a3 Mon Sep 17 00:00:00 2001
From: Johannes Raggam raggam...@adm.at
Date: Tue, 15 Feb 2011 14:29:56 +0100
Subject: [PATCH 2/2] gracefully set newvalue for upgraded schemas, where
oldvalue isn't available.

---
 zope/formlib/form.py |8 +++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/zope/formlib/form.py b/zope/formlib/form.py
index 0d3ade3..ff10168 100755
--- a/zope/formlib/form.py
+++ b/zope/formlib/form.py
@@ -532,7 +532,13 @@ def applyData(context, form_fields, data,
adapters=None):
 
 name = form_field.__name__
 newvalue = data.get(name, form_field) # using form_field as
marker
-if (newvalue is not form_field) and (field.get(adapter) !=
newvalue):
+try:
+oldvalue = field.get(adapter)
+except AttributeError:
+# value not available after schema upgrade
+# make sure that oldvalue != newwalue
+oldvalue = not bool(newvalue)
+if (newvalue is not form_field) and (oldvalue != newvalue):
 descriptions.setdefault(interface,
[]).append(field.__name__)
 field.set(adapter, newvalue)
 
-- 
1.7.1


On Tue, 2011-02-15 at 13:25 +0100, Hanno Schlichting wrote:
 On Tue, Feb 15, 2011 at 1:21 PM, Johannes Raggam raggam...@adm.at wrote:
  while - IMO - zope.schema throws that error justifiably, i think
  zope.formlib should handle it more gracefully in setUpEditWidgets. i
  think, this error comes with every schema upgrade for zope.formlib based
  edit-forms. so, what do you think of following patch?
 
 The usual approach is to add new attributes as class attributes to the
 persistent object. Or write an actual database upgrade step, either
 via something like zope.generations or with a GenericSetup upgrade
 step.
 
 But for zope.formlib it make indeed make sense to be more lenient
 here. So +0 on your patch.
 
 Hanno

-- 
johannes raggam / thet
python plone zope development
http://johannes.raggam.co.at/
mailto:johan...@raggam.co.at
http://bluedynamics.com/

___
Zope-Dev maillist  -  Zope-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 https://mail.zope.org/mailman/listinfo/zope-announce
 https://mail.zope.org/mailman/listinfo/zope )