[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Forms/Views/Browser - FormView.py:1.8 Widget.py:1.5
Stephan Richter
srichter@cbu.edu
Fri, 19 Jul 2002 09:13:01 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/Forms/Views/Browser
In directory cvs.zope.org:/tmp/cvs-serv24805/lib/python/Zope/App/Forms/Views/Browser
Modified Files:
FormView.py Widget.py
Log Message:
Okay, I finished the Forms work. Schema and Forms completely replace the
old Formulator code now. I have switched all the Content objects to using
Schema + Forms; especially the SQL Script has an interesting demo on how
to write your custom fields.
However, I am not satisfied with all my design decisions. There is still
a lot of work to be done in Converters and Widgets. Please contact Martijn
and/or me if you would like to help.
=== Zope3/lib/python/Zope/App/Forms/Views/Browser/FormView.py 1.7 => 1.8 ===
from Schema.Exceptions import StopValidation, ValidationError, \
ConversionError, ValidationErrorsAll, ConversionErrorsAll
+from Zope.Proxy.ContextWrapper import ContextWrapper
from Zope.ComponentArchitecture import getView
+from Zope.Proxy.ProxyIntrospection import removeAllProxies
from Zope.Publisher.Browser.BrowserView import BrowserView
from IForm import IForm
@@ -36,7 +38,8 @@
def getFields(self):
'See Zope.App.Forms.Views.Browser.IForm.IReadForm'
- interfaces = self.context.__implements__
+ context = removeAllProxies(self.context)
+ interfaces = context.__implements__
if isinstance(interfaces, (tuple, list)):
interfaces = flattenInterfaces(interfaces)
else:
@@ -47,7 +50,10 @@
for name in interface.names(1):
attr = interface.getDescriptionFor(name)
if IField.isImplementedBy(attr):
- fields[name] = attr
+ # Give the field a context before adding, so they
+ # know how to retrieve data.
+ fields[name] = ContextWrapper(attr, self.context,
+ name=name)
if self.fields_order is None:
return fields.values()
@@ -129,10 +135,11 @@
def storeAllDataInContext(self, mapping):
"""Store the data back into the context object."""
+ context = removeAllProxies(self.context)
for field in mapping:
value = mapping[field]
- if value != getattr(self.context, field.id):
- setattr(self.context, field.id, value)
+ if value != getattr(context, field.id):
+ setattr(context, field.id, value)
def saveValuesInContext(self):
@@ -150,5 +157,7 @@
except (ValidationErrorsAll, ConversionErrorsAll), e:
return self.form(self, errors=e)
else:
+ # XXX This should do a redirect by looking up the object in
+ # the view registry
return self.form(self)
=== Zope3/lib/python/Zope/App/Forms/Views/Browser/Widget.py 1.4 => 1.5 ===
class FileWidget(TextWidget):
"""File Widget"""
+ converter = Converter.FileToStrConverter()
type = 'file'
+ def render(self, value):
+ 'See Zope.App.Forms.Views.Browser.IBrowserWidget.IBrowserWidget'
+ displayMaxWidth = self.getValue('displayMaxWidth') or 0
+ if displayMaxWidth > 0:
+ return renderElement(self.getValue('tag'),
+ type = self.getValue('type'),
+ name = self.context.id,
+ cssClass = self.getValue('cssClass'),
+ size = self.getValue('displayWidth'),
+ maxlength = displayMaxWidth,
+ extra = self.getValue('extra'))
+ else:
+ return renderElement(self.getValue('tag'),
+ type = self.getValue('type'),
+ name = self.context.id,
+ cssClass = self.getValue('cssClass'),
+ size = self.getValue('displayWidth'),
+ extra = self.getValue('extra'))
+
class ItemsWidget(BrowserWidget):
"""A widget that has a number of items in it."""
@@ -164,9 +184,11 @@
def renderItems(self, value):
name = self.context.id
# get items
- items = self.context.get('items')
+ items = self.context.items
+ if callable(items):
+ items = items()
# check if we want to select first item
- if not value and self.context.get_value('firstItem') and \
+ if not value and getattr(self.context, 'firstItem', None) and \
len(items) > 0:
try:
text, value = items[0]