/usr/lib/python2.7/dist-packages/zope/formlib/objectwidget.py is in python-zope.formlib 4.3.0a2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | ##############################################################################
#
# Copyright (c) 2004 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Browser widgets for text-like data
"""
__docformat__ = 'restructuredtext'
from zope import component
from zope.interface import implementer
from zope.schema import getFieldNamesInOrder
from zope.formlib.interfaces import IInputWidget
from zope.formlib.widget import InputWidget
from zope.formlib.widget import BrowserWidget
from zope.formlib.utility import setUpWidgets, applyWidgetsChanges
from zope.browserpage import ViewPageTemplateFile
from zope.formlib.interfaces import IWidgetInputErrorView
class ObjectWidgetView:
template = ViewPageTemplateFile('objectwidget.pt')
def __init__(self, context, request):
self.context = context
self.request = request
def __call__(self):
return self.template()
@implementer(IInputWidget)
class ObjectWidget(BrowserWidget, InputWidget):
"""A widget over an Interface that contains Fields.
``factory``
factory used to create content that this widget (field) represents
``*_widget``
Optional CustomWidgets used to generate widgets for the fields in this
widget
"""
_object = None # the object value (from setRenderedValue & request)
_request_parsed = False
def __init__(self, context, request, factory, **kw):
super(ObjectWidget, self).__init__(context, request)
# define view that renders the widget
self.view = ObjectWidgetView(self, request)
# factory used to create content that this widget (field)
# represents
self.factory = factory
# handle foo_widget specs being passed in
self.names = getFieldNamesInOrder(self.context.schema)
for k, v in kw.items():
if k.endswith('_widget'):
setattr(self, k, v)
# set up my subwidgets
self._setUpEditWidgets()
def setPrefix(self, prefix):
super(ObjectWidget, self).setPrefix(prefix)
self._setUpEditWidgets()
def _setUpEditWidgets(self):
# subwidgets need a new name
setUpWidgets(self, self.context.schema, IInputWidget,
prefix=self.name, names=self.names,
context=self.context)
def __call__(self):
return self.view()
def legendTitle(self):
return self.context.title or self.context.__name__
def getSubWidget(self, name):
return getattr(self, '%s_widget' % name)
def subwidgets(self):
return [self.getSubWidget(name) for name in self.names]
def hidden(self):
"""Render the object as hidden fields."""
result = []
for name in self.names:
result.append(self.getSubwidget(name).hidden())
return "".join(result)
def error(self):
if self._error:
errormessages = []
keys = sorted(self._error.keys())
for key in keys:
errormessages.append(str(key) + ': ')
errormessages.append(component.getMultiAdapter(
(self._error[key], self.request),
IWidgetInputErrorView).snippet())
errormessages.append(str(key) + ', ')
return ''.join(errormessages[0:-1])
return ""
def getInputValue(self):
"""Return converted and validated widget data.
The value for this field will be represented as an `ObjectStorage`
instance which holds the subfield values as attributes. It will
need to be converted by higher-level code into some more useful
object (note that the default EditView calls `applyChanges`, which
does this).
"""
errors = []
content = self.factory()
for name in self.names:
try:
setattr(content, name, self.getSubWidget(name).getInputValue())
except Exception as e:
errors.append(e)
if self._error is None:
self._error = {}
if name not in self._error:
self._error[name] = e
if errors:
raise errors[0]
return content
def applyChanges(self, content):
field = self.context
# create our new object value
value = field.query(content, None)
if value is None:
# TODO: ObjectCreatedEvent here would be nice
value = self.factory()
# apply sub changes, see if there *are* any changes
# TODO: ObjectModifiedEvent here would be nice
changes = applyWidgetsChanges(self, field.schema, target=value,
names=self.names)
# if there's changes, then store the new value on the content
if changes:
field.set(content, value)
# TODO: If value implements ILocation, set name to field name and
# parent to content
return changes
def hasInput(self):
"""Is there input data for the field
Return ``True`` if there is data and ``False`` otherwise.
"""
for name in self.names:
if self.getSubWidget(name).hasInput():
return True
return False
def setRenderedValue(self, value):
"""Set the default data for the widget.
The given value should be used even if the user has entered
data.
"""
# re-call setupwidgets with the content
self._setUpEditWidgets()
for name in self.names:
self.getSubWidget(name).setRenderedValue(getattr(value, name, None))
|