示例#1
0
 def update(self):
     super(ObjectFieldWidget, self).update()
     value = self.component.dataManager(self.inputValue())
     form = cloneFormData(self.form, value, self.identifier)
     self.objectWidgets = Widgets(form=form, request=self.request)
     self.objectWidgets.extend(self.component.objectFields)
     self.objectWidgets.update()
示例#2
0
 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
示例#3
0
class ObjectFieldWidget(SchemaFieldWidget):

    template = getTemplate('objectfieldwidget.pt')

    def prepareContentValue(self, value):
        if value is NO_VALUE:
            return {self.identifier: []}
        return {self.identifier: value}

    def update(self):
        super(ObjectFieldWidget, self).update()
        value = self.component.dataManager(self.inputValue())
        form = cloneFormData(self.form, value, self.identifier)
        self.objectWidgets = Widgets(form=form, request=self.request)
        self.objectWidgets.extend(self.component.objectFields)
        self.objectWidgets.update()
示例#4
0
 def updateLines(self, mark_selected=False):
     self.lines = []
     self.lineWidgets = []
     self.batching = None
     items = self.getItems()
     for position, item in enumerate(items):
         # each line is like a form
         prefix = '%s.line-%d' % (self.prefix, position)
         form = self.generate_line_form(content=item, prefix=prefix)
         form.selected = False
         # Checkbox to select the line
         form.selectedField = self.createSelectedField(item)
         if mark_selected:
             self.mark_selected(form)
         lineWidget = Widgets(form=form, request=self.request)
         lineWidget.extend(form.selectedField)
         self.lines.append(form)
         self.lineWidgets.append(lineWidget)
示例#5
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]
示例#6
0
 def jsonTemplateWidget(self):
     widgets = Widgets()
     widgets.append(self.createValueWidget('{identifier}', None))
     widgets.update()
     return list(widgets)[0]
示例#7
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]
示例#8
0
 def __init__(self, context, request):
     super(BaseTable, self).__init__(context, request)
     self.fieldWidgets = Widgets(form=self, request=self.request)
     self.lines = []
     self.lineWidgets = []
     self.batcher = None
示例#9
0
class BaseTable(FormData):
    """Action-less table component (mainly for display)
    """
    mode = DISPLAY
    fields = Fields()
    template = get_template('table.pt')

    label = u""
    description = u""

    css_id = None
    css_class = None

    batchSize = 10
    createBatch = False

    tableFields = Fields()

    ignoreContent = False
    ignoreRequest = True
    _updated = False

    emptyDescription = _(u"There are no items.")

    def __init__(self, context, request):
        super(BaseTable, self).__init__(context, request)
        self.fieldWidgets = Widgets(form=self, request=self.request)
        self.lines = []
        self.lineWidgets = []
        self.batcher = None

    @property
    def tableDataManager(self):
        """By default tableDataManager is dataManager

        but you may override it"""
        return self.dataManager

    @property
    def target_language(self):
        return ILanguage(self.request, None)

    @property
    def title(self):
        return (u"<h1>%s</h1>" % self.label) or ''

    def getItems(self):
        return self.context.values()

    def items(self):
        if self.createBatch:
            self.batcher.update(self.getItems())
            return self.batcher.batch
        return self.getItems()

    def namespace(self):
        namespace = {}
        namespace['view'] = namespace['table'] = self
        namespace['table_fields'] = self.tableFields
        namespace['lines'] = self.lineWidgets
        namespace['fields'] = self.fieldWidgets
        return namespace

    def generate_line_form(self, content, prefix):
        """
        Generate form for a specific line, using content.
        """
        form = cloneFormData(self, content=content, prefix=prefix)
        # TODO : if cloneFormData would copy dataValidators
        # and accept kwargs to override eg. dataManager
        # this would not happen, fix it in forms.base!
        form.dataManager = self.tableDataManager
        form.dataValidators = self.dataValidators
        form.setContentData(content)
        # context is content so that vocabularies work !
        if not ILocation.providedBy(content):
            content = LocationProxy(content)
        form.context = content
        if getattr(content, '__parent__', None) is None:
            setattr(content, '__parent__', form)
        form.__parent__ = self
        return form

    def updateLines(self):
        self.lines = []
        self.lineWidgets = []
        for position, item in enumerate(self.items()):
            prefix = u'%s.line-%d' % (self.prefix, position)
            form = self.generate_line_form(content=item, prefix=prefix)
            lineWidget = Widgets(form=form, request=self.request)
            self.lines.append(form)
            self.lineWidgets.append(lineWidget)

    def updateWidgets(self):
        for widgets in self.lineWidgets:
            widgets.extend(self.tableFields)
        self.fieldWidgets.extend(self.fields)

        for widgets in self.lineWidgets:
            widgets.update()

        self.fieldWidgets.update()

    def update(self, *args, **kwargs):
        pass

    def update_batch(self):
        if self.createBatch:
            content = self.getContentData().getContent()
            if not ILocation.providedBy(content):
                content = LocationProxy(content)
                content.__parent__ = self
                content.__name__ = ''
            self.batcher = Batcher(
                content, self.request, self.prefix, size=self.batchSize)
            self.batcher.update(self.lines)

    def updateForm(self, *args, **kwargs):
        if self._updated is False:
            self.updateLines()
            self.updateWidgets()
            self.update_batch()
            self._updated = True

    def render(self, *args, **kwargs):
        html = HTMLWrapper()
        res = self.template.render(
            self, target_language=self.target_language, **self.namespace())
        return html(res.encode('utf-8'))
示例#10
0
 def __init__(self, context, request):
     super(TableFormCanvas, self).__init__(context, request)
     self.lines = []
     self.lineWidgets = []
     self.actionWidgets = Widgets(form=self, request=self.request)
示例#11
0
class TableFormCanvas(BaseTable):
    """A form which is able to edit more than one content as a table.
    """
    implements(interfaces.ITableFormCanvas)
    template = get_template('tablecanvas.pt')

    action_url = ""

    actions = Actions()
    tableActions = TableActions()

    ignoreRequest = True

    def __init__(self, context, request):
        super(TableFormCanvas, self).__init__(context, request)
        self.lines = []
        self.lineWidgets = []
        self.actionWidgets = Widgets(form=self, request=self.request)

    def createSelectedField(self, item):
        """Return a field to select the line.
        """
        return SelectField(identifier='select')

    def haveRequiredFields(self):
        return reduce(
            operator.or_,
            [False] + map(operator.attrgetter('required'), self.fields))

    def mark_selected(self, line):
        # Mark selected lines
        selectedExtractor = getWidgetExtractor(
            line.selectedField, line, self.request)
        if selectedExtractor is not None:
            value, error = selectedExtractor.extract()
            if value:
                line.selected = True

    def updateLines(self, mark_selected=False):
        self.lines = []
        self.lineWidgets = []
        self.batching = None
        items = self.getItems()
        for position, item in enumerate(items):
            # each line is like a form
            prefix = '%s.line-%d' % (self.prefix, position)
            form = self.generate_line_form(content=item, prefix=prefix)
            form.selected = False
            # Checkbox to select the line
            form.selectedField = self.createSelectedField(item)
            if mark_selected:
                self.mark_selected(form)
            lineWidget = Widgets(form=form, request=self.request)
            lineWidget.extend(form.selectedField)
            self.lines.append(form)
            self.lineWidgets.append(lineWidget)

    def updateActions(self):
        action, result = self.tableActions.process(self, self.request)
        if action is None:
            action, result = self.actions.process(self, self.request)
        return action, result

    def updateWidgets(self):
        super(TableFormCanvas, self).updateWidgets()
        self.actionWidgets.extend(self.tableActions)
        self.actionWidgets.extend(self.actions)
        self.actionWidgets.update()

    def updateForm(self, *args, **kwargs):
        if self._updated is False:
            self.updateLines()
            action, result = self.updateActions()
            if action and result is SUCCESS:
                self.ignoreRequest = True  # line number may have changed !
                # refresh lines
                self.updateLines()
            self.updateWidgets()
            self.update_batch()
            self._updated = True

    def getItems(self):
        return self.context.values()