[Zope3-checkins] CVS: Zope3/src/zope/app/form - utility.py:1.19.2.1 widget.py:1.4.24.1
Grégoire Weber
zope@i-con.ch
Sun, 22 Jun 2003 10:24:03 -0400
Update of /cvs-repository/Zope3/src/zope/app/form
In directory cvs.zope.org:/tmp/cvs-serv24874/src/zope/app/form
Modified Files:
Tag: cw-mail-branch
utility.py widget.py
Log Message:
Synced up with HEAD
=== Zope3/src/zope/app/form/utility.py 1.19 => 1.19.2.1 ===
--- Zope3/src/zope/app/form/utility.py:1.19 Wed May 21 12:10:07 2003
+++ Zope3/src/zope/app/form/utility.py Sun Jun 22 10:23:02 2003
@@ -34,6 +34,7 @@
"""
__metaclass__ = type
+from warnings import warn
from zope.component import getView, getDefaultViewName
from zope.schema import getFieldsInOrder
from zope.schema.interfaces import ValidationError
@@ -49,6 +50,44 @@
fields = [ (name, schema[name]) for name in names ]
return fields
+def _whine(view, name):
+ url = view.request.URL
+ vname = view.__class__.__name__
+ warn(
+ "View (%s) saved a widget (%s) without a '_widget' suffix.\n"
+ "Url: %s"
+ % (vname, name, url),
+ DeprecationWarning, stacklevel=4,
+ )
+
+class WhiningWidget:
+
+ def __init__(self, view, name, widget):
+ self.__widget = widget
+ self.__whineargs = view, name
+
+ def __whine(self):
+ whineargs = self.__whineargs
+ if whineargs:
+ _whine(*whineargs)
+ self.__whineargs = ()
+
+ def __call__(self, *args, **kw):
+ self.__whine()
+ return self.__widget(*args, **kw)
+
+ def __repr__(self):
+ self.__whine()
+ return `self.__widget`
+
+ def __str__(self):
+ self.__whine()
+ return str(self.__widget)
+
+ def __getattr__(self, name):
+ self.__whine()
+ return getattr(self.__widget, name)
+
def setUpWidget(view, name, field, value=None, prefix=None,
force=False, vname=None, context=None):
"""Set up a single view widget
@@ -61,7 +100,28 @@
widget will be created and assigned to the attribute.
"""
# Has a (custom) widget already been defined?
- widget = getattr(view, name, None)
+
+ wname = name+'_widget'
+
+ widget = getattr(view, wname, None)
+ installold = False
+ if widget is None:
+ widget = getattr(view, name, None)
+ if widget is not None:
+ if IViewFactory.isImplementedBy(widget):
+ # Old custom widget definition.
+ # We'll accept it, but we'll whine
+ _whine(view, name)
+
+ # we also need to remember to install the widget
+ installold = True
+ elif IWidget.isImplementedBy(widget):
+ # Old widget definition. We'll accept it, but we'll whine
+ _whine(view, name)
+ else:
+ # we found something else, which is innocent.
+ widget = None
+ installold = True
if context is None:
context = view.context
@@ -72,7 +132,9 @@
if vname is None:
vname = getDefaultViewName(field, view.request)
widget = getView(field, vname, view.request)
- setattr(view, name, widget)
+ setattr(view, wname, widget)
+ if not hasattr(view, name):
+ setattr(view, name, WhiningWidget(view, name, widget))
else:
# We have an attribute of the right name, is it really a widget
@@ -83,7 +145,9 @@
widget = widget(field, view.request)
if IWidget.isImplementedBy(widget):
# Yee ha! We have a widget now, save it
- setattr(view, name, widget)
+ setattr(view, wname, widget)
+ if installold or not hasattr(view, name):
+ setattr(view, name, WhiningWidget(view, name, widget))
if not IWidget.isImplementedBy(widget):
raise TypeError(
@@ -91,6 +155,9 @@
"but isn't."
% (view.__class__.__name__, name))
+ if not hasattr(view, wname):
+ setattr(view, wname, widget)
+
if prefix:
widget.setPrefix(prefix)
@@ -181,7 +248,7 @@
that was entered by the user.
"""
for name, field in _fieldlist(names, schema):
- if getattr(view, name).haveData():
+ if getattr(view, name+'_widget').haveData():
return True
return False
@@ -224,7 +291,7 @@
errors = []
for name, field in _fieldlist(names, schema):
- widget = getattr(view, name)
+ widget = getattr(view, name+'_widget')
if exclude_readonly and widget.context.readonly:
continue
if widget.haveData():
=== Zope3/src/zope/app/form/widget.py 1.4 => 1.4.24.1 ===
--- Zope3/src/zope/app/form/widget.py:1.4 Thu Jan 9 09:13:08 2003
+++ Zope3/src/zope/app/form/widget.py Sun Jun 22 10:23:02 2003
@@ -16,13 +16,14 @@
"""
from zope.app.interfaces.form import IWidget
from zope.component.interfaces import IViewFactory
+from zope.interface import implements
__metaclass__ = type
class Widget:
"""Mix-in class providing some functionality common accross view types
"""
- __implements__ = IWidget
+ implements(IWidget)
_prefix = 'field.'
_data = None
@@ -61,7 +62,7 @@
class CustomWidget:
"""Custom Widget."""
- __implements__ = IViewFactory
+ implements(IViewFactory)
def __init__(self, widget, **kw):
self.widget = widget