def update(self): """See z3c.form.interfaces.IActions.""" # Create a unique prefix. prefix = util.expandPrefix(self.form.prefix) prefix += util.expandPrefix(self.form.buttons.prefix) # Walk through each field, making an action out of it. uniqueOrderedKeys = [] for name, button in self.form.buttons.items(): # Step 1: Only create an action for the button, if the condition is # fulfilled. if button.condition is not None and not button.condition( self.form): # Step 1.1: If the action already existed, but now the # condition became false, remove the old action. if name in self._data: del self._data[name] continue # Step 2: Get the action for the given button. newButton = True if name in self._data: buttonAction = self._data[name] newButton = False elif button.actionFactory is not None: buttonAction = button.actionFactory(self.request, button) else: buttonAction = zope.component.getMultiAdapter( (self.request, button), interfaces.IButtonAction) # Step 3: Set the name on the button buttonAction.name = prefix + name # Step 4: Set any custom attribute values. title = zope.component.queryMultiAdapter( (self.form, self.request, self.content, button, self), interfaces.IValue, name='title') if title is not None: buttonAction.title = title.get() # Step 5: Set the form buttonAction.form = self.form if not interfaces.IFormAware.providedBy(buttonAction): zope.interface.alsoProvides(buttonAction, interfaces.IFormAware) # Step 6: Update the new action buttonAction.update() zope.event.notify(AfterWidgetUpdateEvent(buttonAction)) # Step 7: Add the widget to the manager uniqueOrderedKeys.append(name) if newButton: self._data[name] = buttonAction zope.location.locate(buttonAction, self, name) # always ensure that we add all keys and keep the order given from # button items self._data_keys = uniqueOrderedKeys self._data_values = [self._data[name] for name in uniqueOrderedKeys]
def update(self): """See interfaces.IWidgets""" # Create a unique prefix. prefix = util.expandPrefix(self.form.prefix) prefix += util.expandPrefix(self.prefix) # Walk through each field, making a widget out of it. for field in self.form.fields.values(): # Step 1: Determine the mode of the widget. mode = self.mode if field.mode is not None: mode = field.mode elif field.field.readonly and not self.ignoreReadonly: mode = interfaces.DISPLAY_MODE elif not self.ignoreContext and self.content is not None: # If we do not have enough permissions to write to the # attribute, then switch to display mode. dm = zope.component.getMultiAdapter( (self.content, field.field), interfaces.IDataManager) if not dm.canWrite(): mode = interfaces.DISPLAY_MODE # Step 2: Get the widget for the given field. factory = field.widgetFactory.get(mode) if factory is not None: widget = factory(field.field, self.request) else: widget = zope.component.getMultiAdapter( (field.field, self.request), interfaces.IFieldWidget) # Step 3: Set the prefix for the widget shortName = field.__name__ widget.name = prefix + shortName widget.id = (prefix + shortName).replace('.', '-') # Step 4: Set the context widget.context = self.content zope.interface.alsoProvides(widget, interfaces.IContextAware) # Step 5: Set the form widget.form = self.form zope.interface.alsoProvides(widget, interfaces.IFormAware) # Step 6: Set some variables widget.ignoreContext = self.ignoreContext widget.ignoreRequest = self.ignoreRequest # Step 7: Set the mode of the widget widget.mode = mode # Step 8: Update the widget widget.update() zope.event.notify(AfterWidgetUpdateEvent(widget)) # Step 9: Add the widget to the manager self._data_keys.append(shortName) self._data_values.append(widget) self._data[shortName] = widget zope.location.locate(widget, self, shortName)
def update(self): """See z3c.form.interfaces.IActions.""" # Create a unique prefix. prefix = util.expandPrefix(self.form.prefix) prefix += util.expandPrefix(self.form.buttons.prefix) # Walk through each field, making an action out of it. for name, button in self.form.buttons.items(): # Step 1: Only create an action for the button, if the condition is # fulfilled. if button.condition is not None and not button.condition( self.form): continue # Step 2: Get the action for the given button. if button.actionFactory is not None: buttonAction = button.actionFactory(self.request, button) else: buttonAction = zope.component.getMultiAdapter( (self.request, button), interfaces.IButtonAction) # Step 3: Set the name on the button fullName = prefix + name buttonAction.name = fullName # Step 4: Set any custom attribute values. title = zope.component.queryMultiAdapter( (self.form, self.request, self.content, button, self), interfaces.IValue, name='title') if title is not None: buttonAction.title = title.get() # Step 5: Set the form buttonAction.form = self.form zope.interface.alsoProvides(buttonAction, interfaces.IFormAware) # Step 6: Update the new action buttonAction.update() zope.event.notify(AfterWidgetUpdateEvent(buttonAction)) # Step 7: Add the widget to the manager self._data_keys.append(name) self._data_values.append(buttonAction) self._data[name] = buttonAction zope.location.locate(buttonAction, self, name)
def update(self): """See interfaces.IWidgets""" # Create a unique prefix. prefix = util.expandPrefix(self.form.prefix) prefix += util.expandPrefix(self.prefix) # Walk through each field, making a widget out of it. uniqueOrderedKeys = [] for field in self.form.fields.values(): # Step 0. Determine whether the context should be ignored. ignoreContext = self.ignoreContext if field.ignoreContext is not None: ignoreContext = field.ignoreContext # Step 1: Determine the mode of the widget. mode = self.mode if field.mode is not None: mode = field.mode elif field.field.readonly and not self.ignoreReadonly: mode = interfaces.DISPLAY_MODE elif not ignoreContext: # If we do not have enough permissions to write to the # attribute, then switch to display mode. dm = zope.component.getMultiAdapter( (self.content, field.field), interfaces.IDataManager) if not dm.canWrite(): mode = interfaces.DISPLAY_MODE # Step 2: Get the widget for the given field. shortName = field.__name__ newWidget = True if shortName in self._data: # reuse existing widget widget = self._data[shortName] newWidget = False elif field.widgetFactory.get(mode) is not None: factory = field.widgetFactory.get(mode) widget = factory(field.field, self.request) else: widget = zope.component.getMultiAdapter( (field.field, self.request), interfaces.IFieldWidget) # Step 3: Set the prefix for the widget widget.name = prefix + shortName widget.id = (prefix + shortName).replace('.', '-') # Step 4: Set the context widget.context = self.content # Step 5: Set the form widget.form = self.form # Optimization: Set both interfaces here, rather in step 4 and 5: # ``alsoProvides`` is quite slow zope.interface.alsoProvides(widget, interfaces.IContextAware, interfaces.IFormAware) # Step 6: Set some variables widget.ignoreContext = ignoreContext widget.ignoreRequest = self.ignoreRequest if field.showDefault is not None: widget.showDefault = field.showDefault # Step 7: Set the mode of the widget widget.mode = mode # Step 8: Update the widget widget.update() zope.event.notify(AfterWidgetUpdateEvent(widget)) # Step 9: Add the widget to the manager if widget.required: self.hasRequiredFields = True uniqueOrderedKeys.append(shortName) if newWidget: self._data_values.append(widget) self._data[shortName] = widget zope.location.locate(widget, self, shortName) # always ensure that we add all keys and keep the order given from # button items self._data_keys = uniqueOrderedKeys
def handle_edit(self, action): success = u"Successfully updated" partly_success = u"Some of your changes could not be applied." status = no_changes = u"No changes made." for subform in self.subforms: # With the ``extractData()`` call, validation will occur, # and errors will be stored on the widgets amongst other # places. After this we have to be extra careful not to # call (as in ``__call__``) the subform again, since # that'll remove the errors again. With the results that # no changes are applied but also no validation error is # shown. data, errors = subform.extractData() if errors: if status is no_changes: status = subform.formErrorsMessage elif status is success: status = partly_success continue # Wo have no select field in our editsubform # del data['select'] self.context.before_update(subform.content, data) changes = subform.applyChanges(data) if changes: if status is no_changes: status = success elif status is subform.formErrorsMessage: status = partly_success # If there were changes, we'll update the view widgets # again, so that they'll actually display the changes for widget in subform.widgets.values(): if widget.mode == DISPLAY_MODE: widget.update() notify(AfterWidgetUpdateEvent(widget)) # update IBCCVLMetadata['layers'] with current entered values ds = self.context.context md = IBCCVLMetadata(ds) layers = md.get('layers', {}) # map filenames to layers file_map = {} for layer in layers.values(): file_map[layer['filename']] = layer # rebuild layers dict with correct keys and datatypes layers = {} for subform in self.subforms: if subform.content['layer']: layer_id = subform.content['layer'] else: layer_id = subform.content['filename'] layer = file_map[subform.content['filename']] layer['datatype'] = subform.content['datatype'] layer['layer'] = layer_id layers[layer_id] = layer # write changes back md['layers'] = layers modified(self.context.context) self.status = status self.context.redirect()
def handle_edit(self, action): success = u"Successfully updated" partly_success = u"Some of your changes could not be applied." status = no_changes = u"No changes made." for subform in self.subforms: # With the ``extractData()`` call, validation will occur, # and errors will be stored on the widgets amongst other # places. After this we have to be extra careful not to # call (as in ``__call__``) the subform again, since # that'll remove the errors again. With the results that # no changes are applied but also no validation error is # shown. data, errors = subform.extractData() if errors: if status is no_changes: status = subform.formErrorsMessage elif status is success: status = partly_success continue # Wo have no select field in our editsubform #del data['select'] self.context.before_update(subform.content, data) changes = subform.applyChanges(data) if changes: if status is no_changes: status = success elif status is subform.formErrorsMessage: status = partly_success # If there were changes, we'll update the view widgets # again, so that they'll actually display the changes for widget in subform.widgets.values(): if widget.mode == DISPLAY_MODE: widget.update() notify(AfterWidgetUpdateEvent(widget)) # don't forget to update our property we manage urilist = [] for subform in self.subforms: # TODO: this would handle adds? #if not subform.content_id: # we had no filename # import ipdb; ipdb.set_trace() urilist.append(subform.content.identifier) if IResource.providedBy(subform.content): handler.put(subform.content.graph) else: handler.put(subform.content) data = {self.context.property: urilist} # here we applyData to the context (actually Crud context) # should be fine but cleaner solution would be better? # also applyChanges needs self.field which we don't have here content = self.context.getContent() content.remove((content.identifier, self.context.property, None)) for uri in urilist: content.add((content.identifier, self.context.property, uri)) # TODO: do only if things have changed # - put current graph into changed queue, to persist changes # - got ordf tool and push graph handler.put(content) # TODO: update status if necessary modified(self.context.context) self.status = status self.context.redirect()