def update(self): super(MultiGenericFieldWidget, self).update() self.valueWidgets.update() self.jsonAddIdentifier = None self.jsonAddTemplate = None self.includeEmptyMessage = self.allowRemove if self.allowAdding: self.jsonAddIdentifier = 'id' + md5hash(self.identifier) widgets = Widgets() widgets.append(self.createValueWidget( '{' + self.jsonAddIdentifier + '}', None)) widgets.update() self.jsonAddTemplate = list(widgets)[0]
def jsonTemplateWidget(self): widgets = Widgets() widgets.append(self.createValueWidget('{identifier}', None)) widgets.update() return list(widgets)[0]
class MultiGenericFieldWidget(SchemaFieldWidget): allowAdding = True allowRemove = True template = getTemplate('multigenericfieldwidget.pt') def __init__(self, field, value_field, form, request): super(MultiGenericFieldWidget, self).__init__(field, form, request) self.allowAdding = field.allowAdding self.allowRemove = field.allowRemove self.valueField = value_field self.valueWidgets = Widgets() self.haveValues = False def createValueWidget(self, new_identifier, value): field = self.valueField.clone(new_identifier=str(new_identifier)) form = cloneFormData(self.form, prefix=self.identifier) if value is not None: form.ignoreContent = False form.setContentData(NoneDataManager(value)) else: form.ignoreRequest = False form.ignoreContent = True return createWidget(field, form, self.request) def addValueWidget(self, new_identifier, value): widget = self.createValueWidget(new_identifier, value) if widget is not None: self.valueWidgets.append(widget) return widget def prepareContentValue(self, values): if values is NO_VALUE: return {self.identifier: '0'} for position, value in enumerate(values): # Create new widgets for each value self.addValueWidget(position, value) count = len(values) if count: self.haveValues = True return {self.identifier: str(count)} def prepareRequestValue(self, values): value_count = 0 identifier_count = int(values.get(self.identifier, '0')) remove_something = self.identifier + '.remove' in values for position in range(0, identifier_count): value_marker = (self.identifier, position,) value_present = '%s.present.%d' % value_marker in values if not value_present: continue value_selected = '%s.checked.%d' % value_marker in values if remove_something and value_selected: continue self.addValueWidget(position, None) value_count += 1 if self.identifier + '.add' in values: self.addValueWidget(identifier_count, None) value_count += 1 values[self.identifier] = str(identifier_count + 1) if value_count: self.haveValues = True return values @property def jsonTemplateWidget(self): widgets = Widgets() widgets.append(self.createValueWidget('{identifier}', None)) widgets.update() return list(widgets)[0] def update(self): super(MultiGenericFieldWidget, self).update() self.valueWidgets.update() self.jsonAddIdentifier = None self.jsonAddTemplate = None self.includeEmptyMessage = self.allowRemove if self.allowAdding: self.jsonAddIdentifier = 'id' + md5hash(self.identifier) widgets = Widgets() widgets.append(self.createValueWidget( '{' + self.jsonAddIdentifier + '}', None)) widgets.update() self.jsonAddTemplate = list(widgets)[0]