def GroupForm_update(self): # This monkey patch ensures that processInputs() is called before # z3c.form does any work on the request. This is because z3c.form expects # charset negotiation to have taken place in the publisher, and will # complain about non-unicode strings processInputs(self.request) _original_GroupForm_update(self)
def update(self): if super(ControlPanel, self).update(): if 'form.button.Save' in self.request.form: if six.PY3: # decode the inputs recursively to unicode processInputs(self.request) self.processSave() elif 'form.button.Cancel' in self.request.form: self.request.response.redirect( '{0}/@@overview-controlpanel'.format( self.context.absolute_url(), ), )
def dexterity_update(obj, request=None): """ Utility method to update the fields of all the schemas of the Dexterity object 'obj'. """ modified = False if not request: request = obj.REQUEST # Call processInputs to decode strings to unicode, otherwise the # z3c.form dataconverters complain. processInputs(request) errors = [] for schema in get_dexterity_schemas(context=obj): for name in getFieldNames(schema): field = schema[name] widget = component.getMultiAdapter( (field, request), IFieldWidget) widget.context = obj value = field.missing_value widget.update() try: raw = widget.extract() except MultipleErrors, e: errors.append(e) log.warn("Multiple errors while extracting field: %s" % name) continue if raw is NOT_CHANGED: continue if raw is NO_VALUE: continue value = IDataConverter(widget).toFieldValue(safe_unicode(raw)) try: field.validate(value) except interfaces.RequiredMissing, e: errors.append(e) log.warn("Required field have missing value: %s" % name) # XXX: we might not want to continue here, since we then remove # the ability to remove field values. Client side validation # should prevent this situation from arising, but it's not yet # perfect. # continue except interfaces.ConstraintNotSatisfied, e: log.warn("Constraint not satisfied for field: %s" % name) log.warn(e) continue
def dexterity_update(obj, request=None): """ Utility method to update the fields of all the schemas of the Dexterity object 'obj'. """ modified = defaultdict(list) if not request: request = obj.REQUEST # Call processInputs to decode strings to unicode, otherwise the # z3c.form dataconverters complain. processInputs(request) errors = [] for schema in get_dexterity_schemas(context=obj): for name in getFieldNames(schema): # Only update fields which are included in the form # Look specifically for the empty-marker used to mark # empty checkboxes if name not in request.form and \ '%s-empty-marker' % name not in request.form: continue field = schema[name] autoform_widgets = schema.queryTaggedValue(WIDGETS_KEY, default={}) if name in autoform_widgets: widget = autoform_widgets[name] if not isinstance(widget, ParameterizedWidget): widget = ParameterizedWidget(widget) widget = widget(field, request) else: widget = component.getMultiAdapter((field, request), IFieldWidget) widget.context = obj value = field.missing_value widget.update() try: raw = widget.extract() except MultipleErrors, e: errors.append(e) log.warn("Multiple errors while extracting field: %s" % name) continue if raw is NOT_CHANGED: continue if raw is NO_VALUE: continue if (IRichTextValue.providedBy(raw) and api.portal.get_registry_record( 'ploneintranet.workspace.sanitize_html')): sanitized = RichTextValue(raw=sanitize_html( safe_unicode(raw.raw)), mimeType=raw.mimeType, outputMimeType=raw.outputMimeType) if sanitized.raw != raw.raw: log.info( 'The HTML content of field "{}" on {} was sanitised.'. format( # noqa name, obj.absolute_url())) raw = sanitized if isinstance(field, TextLine): # textLines must not contain line breaks (field contraint) # to prevent a ConstraintNotSatisfied error in `toFieldValue` raw = u" ".join(safe_unicode(raw).splitlines()) try: value = IDataConverter(widget).toFieldValue(safe_unicode(raw)) except Exception, exc: log.warn("Error in converting value for %s: %s (%s)" % (name, raw, str(exc))) raise exc try: field.validate(value) except interfaces.RequiredMissing, e: errors.append(e) log.warn("Required field have missing value: %s" % name)