Esempio n. 1
0
    def move(self, pos):
        """ AJAX method to change field position within its schema.
        The position is relative to the fieldset.
        """
        schema = IEditableSchema(self.schema)
        fieldname = self.field.__name__
        pos = int(pos)

        ordered_field_ids = [
            name for (name, field) in sortedFields(self.schema)
        ]
        for fieldset in self.schema.getTaggedValue(FIELDSETS_KEY):
            # if we are in a fieldset, pos is the position relative to the fieldset
            if fieldname in fieldset.fields:
                old_field_of_position = fieldset.fields[pos]
                absolute_position = ordered_field_ids.index(
                    old_field_of_position)
                break
        else:
            # in default fieldset, the relative position == the absolute position
            fieldset = None
            absolute_position = pos

        schema.moveField(fieldname, absolute_position)
        # if field is in a fieldset, also reorder fieldset tagged value
        ordered_field_ids = [
            name for (name, field) in sortedFields(self.schema)
        ]
        if fieldset is not None:
            fieldset.fields = sorted(fieldset.fields,
                                     key=lambda x: ordered_field_ids.index(x))

        notifyContainerModified(self.schema)
        notify(SchemaModifiedEvent(self.aq_parent.aq_parent))
Esempio n. 2
0
    def move(self, pos):
        """ AJAX method to change field position within its schema.
        The position is relative to the fieldset.
        """
        schema = IEditableSchema(self.schema)
        fieldname = self.field.__name__
        pos = int(pos)


        ordered_field_ids = [name for (name, field) in sortedFields(self.schema)]
        for fieldset in self.schema.getTaggedValue(FIELDSETS_KEY):
            # if we are in a fieldset, pos is the position relative to the fieldset
            if fieldname in fieldset.fields:
                old_field_of_position = fieldset.fields[pos]
                absolute_position = ordered_field_ids.index(old_field_of_position)
                break
        else:
            # in default fieldset, the relative position == the absolute position
            fieldset = None
            absolute_position = pos

        schema.moveField(fieldname, absolute_position)
        # if field is in a fieldset, also reorder fieldset tagged value
        ordered_field_ids = [name for (name, field) in sortedFields(self.schema)]
        if fieldset is not None:
            fieldset.fields = sorted(fieldset.fields,
                                     key=lambda x: ordered_field_ids.index(x))

        notifyContainerModified(self.schema)
        notify(SchemaModifiedEvent(self.aq_parent.aq_parent))
    def change(self, fieldset_index):
        """ AJAX method to change the fieldset of a field
        """
        fieldset_index = int(fieldset_index)
        fieldsets = self.schema.getTaggedValue(FIELDSETS_KEY)
        field_name = self.field.__name__

        # get current fieldset
        fieldset_fields = []
        current_fieldset = None
        for fieldset in fieldsets:
            if field_name in fieldset.fields:
                current_fieldset = fieldset

            fieldset_fields.extend(fieldset.fields)

        # get future fieldset
        if len(sortedFields(self.schema)) != len(fieldset_fields):
            # we have a default fieldset
            fieldset_index -= 1

        if fieldset_index >= 0:
            # the field has not been moved into default
            next_fieldset = fieldsets[fieldset_index]
        else:
            next_fieldset = None

        # computing new Position, which is the last position of the new fieldset
        ordered_field_ids = [
            name for (name, field) in sortedFields(self.schema)
        ]
        if next_fieldset is None:
            # if this is the default,
            new_position = ordered_field_ids.index(fieldset_fields[0])
        else:
            # first we get the first of the fieldsets after the new one
            new_position = None
            for fieldset in fieldsets[fieldsets.index(next_fieldset) + 1:]:
                if len(fieldset.fields) > 0:
                    new_position = ordered_field_ids.index(
                        fieldset.fields[0]) - 1
                    break
            else:
                new_position = len(ordered_field_ids) - 1

        schema = IEditableSchema(self.schema)
        schema.moveField(field_name, new_position)

        # move field
        if next_fieldset is not None:
            next_fieldset.fields.append(field_name)

        if current_fieldset is not None:
            current_fieldset.fields.remove(field_name)

        notifyContainerModified(self.schema)
        notify(SchemaModifiedEvent(self.aq_parent.aq_parent))
    def change(self, fieldset_index):
        """ AJAX method to change the fieldset of a field
        """
        fieldset_index = int(fieldset_index)
        fieldsets = self.schema.getTaggedValue(FIELDSETS_KEY)
        field_name = self.field.__name__

        # get current fieldset
        fieldset_fields = []
        current_fieldset = None
        for fieldset in fieldsets:
            if field_name in fieldset.fields:
                current_fieldset = fieldset

            fieldset_fields.extend(fieldset.fields)

        # get future fieldset
        if len(sortedFields(self.schema)) != len(fieldset_fields):
            # we have a default fieldset
            fieldset_index -= 1

        if fieldset_index >= 0:
            # the field has not been moved into default
            next_fieldset = fieldsets[fieldset_index]
        else:
            next_fieldset = None

        # computing new Position, which is the last position of the new fieldset
        ordered_field_ids = [name for (name, field) in sortedFields(self.schema)]
        if next_fieldset is None:
            # if this is the default,
            new_position = ordered_field_ids.index(fieldset_fields[0])
        else:
            # first we get the first of the fieldsets after the new one
            new_position = None
            for fieldset in fieldsets[fieldsets.index(next_fieldset)+1:]:
                if len(fieldset.fields) > 0:
                    new_position = ordered_field_ids.index(fieldset.fields[0]) - 1
                    break
            else:
                new_position = len(ordered_field_ids) - 1

        schema = IEditableSchema(self.schema)
        schema.moveField(field_name, new_position)

        # move field
        if next_fieldset is not None:
            next_fieldset.fields.append(field_name)

        if current_fieldset is not None:
            current_fieldset.fields.remove(field_name)

        notifyContainerModified(self.schema)
        notify(SchemaModifiedEvent(self.aq_parent.aq_parent))
Esempio n. 5
0
    def move(self, pos, fieldset_index):
        """ AJAX method to change field position within its schema.
        The position is relative to the fieldset.
        """
        schema = IEditableSchema(self.schema)
        fieldname = self.field.__name__
        pos = int(pos)
        fieldset_index = int(fieldset_index)
        fieldset_index -= 1  # index 0 is default fieldset

        fieldsets = self.schema.queryTaggedValue(FIELDSETS_KEY, [])
        new_fieldset = fieldset_index >= 0 and fieldsets[fieldset_index] or None
        schema.changeFieldFieldset(fieldname, new_fieldset)

        ordered_field_ids = [info[0] for info in sortedFields(self.schema)]
        if new_fieldset:
            old_field_of_position = new_fieldset.fields[pos]
            new_absolute_position = ordered_field_ids.index(
                old_field_of_position)
        else:
            new_absolute_position = pos

        # if fieldset changed, update fieldsets
        schema.moveField(fieldname, new_absolute_position)

        notifyContainerModified(self.schema)
        notify(SchemaModifiedEvent(self.aq_parent.aq_parent))
Esempio n. 6
0
    def move(self, pos, fieldset_index):
        """ AJAX method to change field position within its schema.
        The position is relative to the fieldset.
        """
        schema = IEditableSchema(self.schema)
        fieldname = self.field.__name__
        pos = int(pos)
        fieldset_index = int(fieldset_index)
        fieldset_index -= 1  # index 0 is default fieldset

        fieldsets = self.schema.queryTaggedValue(FIELDSETS_KEY, [])
        new_fieldset = fieldset_index >= 0 and fieldsets[
            fieldset_index] or None
        schema.changeFieldFieldset(fieldname, new_fieldset)

        ordered_field_ids = [info[0] for info in sortedFields(self.schema)]
        if new_fieldset:
            old_field_of_position = new_fieldset.fields[pos]
            new_absolute_position = ordered_field_ids.index(
                old_field_of_position)
        else:
            new_absolute_position = pos

        # if fieldset changed, update fieldsets
        schema.moveField(fieldname, new_absolute_position)

        notifyContainerModified(self.schema)
        notify(SchemaModifiedEvent(self.__parent__.__parent__))
Esempio n. 7
0
    def add(self, field):
        context = self.context
        schema = IEditableSchema(context.schema)

        # move it after the last field that is not in a fieldset
        # or at top if there is no field yet in "default" fieldset
        ordered_fields = [name for (name, f) in sortedFields(context.schema)]
        default_fields = non_fieldset_fields(context.schema)
        if len(default_fields) > 0:
            position = ordered_fields.index(default_fields[-1]) + 1
        else:
            position = 0

        try:
            schema.addField(field)
        except ValueError:
            raise WidgetActionExecutionError('__name__',
                Invalid(
                    u'Please select a field name that is not already used.'
                ))

        schema.moveField(field.__name__, position)
        notify(ObjectAddedEvent(field, context.schema))
        notify(FieldAddedEvent(context, field))
        IStatusMessage(self.request).addStatusMessage(
            _(u"Field added successfully."), type='info')
    def add(self, field):
        context = self.context
        schema = IEditableSchema(context.schema)

        # move it after the last field that is not in a fieldset
        # or at top if there is no field yet in "default" fieldset
        ordered_fields = [name for (name, f) in sortedFields(context.schema)]
        default_fields = non_fieldset_fields(context.schema)
        if len(default_fields) > 0:
            position = ordered_fields.index(default_fields[-1]) + 1
        else:
            position = 0

        try:
            schema.addField(field)
        except ValueError:
            raise WidgetActionExecutionError('__name__',
                Invalid(u'Please select a field name that is not already used.'))

        schema.moveField(field.__name__, position)
        notify(ObjectAddedEvent(field, context.schema))
        notify(FieldAddedEvent(context, field))
        self.status = _(u"Field added successfully.")