Пример #1
0
    def clean(self):
        data = super(ConditionForm, self).clean()

        msg = is_legal_name(data['name'])
        if msg:
            self.add_error('name', msg)
            return data

        return data
Пример #2
0
    def clean(self):
        data = super(ConditionForm, self).clean()

        if not data.get('name'):
            self.add_error('name', _('Name cannot be empty'))
            return data

        msg = is_legal_name(data['name'])
        if msg:
            self.add_error('name', msg)
            return data

        return data
Пример #3
0
    def clean(self):
        data = super(AttributeItemForm, self).clean()

        # Name is legal
        msg = is_legal_name(data['key'])
        if msg:
            self.add_error('key', msg)
            return data

        if data['key'] in self.keys:
            self.add_error('key',
                           'Name has to be different from all existing ones.')
            return data

        return data
Пример #4
0
    def clean(self):
        """Check that the name is correct and is not duplicated."""
        form_data = super().clean()

        attr_name = form_data['key']

        # Name is legal
        msg = is_legal_name(attr_name)
        if msg:
            self.add_error('key', msg)
            return form_data

        if attr_name in self.keys:
            self.add_error(
                'key',
                _('Name has to be different from all existing ones.'))
            return form_data

        # Enforce the property that Attribute names, column names and
        # condition names cannot overlap.
        if attr_name in self.workflow.get_column_names():
            self.add_error(
                'key',
                _('There is a column with this name. Please change.'),
            )
            return form_data

        # Check if there is a condition with that name
        if (
            Condition.objects.filter(
                action__workflow=self.workflow,
                name=attr_name,
            ).exists()
        ):
            self.add_error(
                'key',
                _('There is a condition already with this name.'),
            )
            return form_data

        return form_data
Пример #5
0
    def clean(self):
        """Check that data is not empty."""
        form_data = super().clean()

        name = form_data.get('name')
        if not name:
            self.add_error('name', _('Name cannot be empty'))
            return form_data

        msg = is_legal_name(form_data['name'])
        if msg:
            self.add_error('name', msg)

        # Check if the name already exists
        name_exists = self.action.conditions.filter(
            name=name,
        ).exclude(id=self.instance.id).exists()
        if name_exists:
            self.add_error(
                'name',
                _('There is already a condition with this name.'),
            )

        # Check that the name does not collide with other attribute or column
        if name in self.action.workflow.get_column_names():
            self.add_error(
                'name',
                _('The workflow has a column with this name.'),
            )

        # New condition name does not collide with attribute names
        if name in list(self.action.workflow.attributes.keys()):
            self.add_error(
                'name',
                _('The workflow has an attribute with this name.'),
            )

        return form_data
Пример #6
0
    def clean(self):
        data = super(ColumnBasicForm, self).clean()

        # Load the data frame from the DB for various checks and leave it in
        # the form for future use
        self.data_frame = pandas_db.load_from_db(self.workflow.id)

        # Column name must be a legal variable name
        if 'name' in self.changed_data:
            # Name is legal
            msg = is_legal_name(data['name'])
            if msg:
                self.add_error('name', msg)
                return data

            # Check that the name is not present already
            if next((c for c in self.workflow.columns.all()
                     if c.id != self.instance.id and c.name == data['name']),
                    None):
                # New column name collides with existing one
                self.add_error('name',
                               'There is a column already with this name')
                return data

        # Categories must be valid types
        if 'raw_categories' in self.changed_data:
            if data['raw_categories']:
                # Condition 1: Values must be valid for the type of the column
                category_values = [
                    x.strip() for x in data['raw_categories'].split(',')
                ]
                try:
                    valid_values = Column.validate_column_values(
                        data['data_type'], category_values)
                except ValueError:
                    self.add_error('raw_categories',
                                   'Incorrect list of values')
                    return data

                # Condition 2: The values in the dataframe column must be in
                # these categories (only if the column is being edited, though
                if self.instance.name and \
                        not all([x in valid_values
                                 for x in self.data_frame[self.instance.name]
                                 if x and not pd.isnull(x)]):
                    self.add_error(
                        'raw_categories',
                        'The values in the column are not compatible with ' +
                        'these ones.')
                    return data
            else:
                valid_values = []

            self.instance.set_categories(valid_values)

        # Check the datetimes. One needs to be after the other
        a_from = self.cleaned_data['active_from']
        a_to = self.cleaned_data['active_to']
        if a_from and a_to and a_from >= a_to:
            self.add_error('active_from', 'Incorrect date/time window')
            self.add_error('active_to', 'Incorrect date/time window')

        return data
Пример #7
0
    def clean(self) -> Dict:
        """Check that the name is legal and the categories have right value."""
        form_data = super().clean()

        # Load the data frame from the DB for various checks and leave it in
        # the form for future use
        self.data_frame = pandas.load_table(
            self.workflow.get_data_frame_table_name())

        # Column name must be a legal variable name
        if 'name' in self.changed_data:
            # Name is legal
            msg = is_legal_name(form_data['name'])
            if msg:
                self.add_error('name', msg)
                return form_data

            # Check that the name is not present already
            if self.workflow.columns.filter(name=form_data['name']).exists():
                # New column name collides with existing one
                self.add_error(
                    'name',
                    _('There is a column already with this name'))
                return form_data

        # Categories must be valid types
        if 'raw_categories' in self.changed_data:
            if form_data['raw_categories']:
                # Detect if an interval syntax is used (number column)
                match = re.search(
                    INTERVAL_PATTERN,
                    form_data['raw_categories'])
                if (
                    self.allow_interval_as_initial
                    and (
                        form_data['data_type'] == 'double'
                        or form_data['data_type'] == 'integer')
                    and match
                ):
                    # If it is a number column, and there is an interval string
                    from_val = int(match.group('from'))
                    to_val = int(match.group('to'))
                    if from_val > to_val:
                        tmp = from_val
                        from_val = to_val
                        to_val = tmp
                    category_values = [
                        str(val) for val in range(from_val, to_val + 1)]
                else:
                    category_values = [
                        cat.strip()
                        for cat in form_data['raw_categories'].split(',')]

                # Condition 1: There must be more than one value
                if len(category_values) < 2:
                    self.add_error(
                        'raw_categories',
                        _('More than a single value needed.'))
                    return form_data

                # Condition 2: Values must be valid for the type of the column
                try:
                    valid_values = models.Column.validate_column_values(
                        form_data['data_type'],
                        category_values)
                except ValueError:
                    self.add_error(
                        'raw_categories',
                        _('Incorrect list of values'),
                    )
                    return form_data

                # Condition 3: The values in the dataframe column must be in
                # these categories (only if the column is being edited, though
                if self.instance.name and not all(
                    vval in valid_values
                    for vval in self.data_frame[self.instance.name]
                    if vval and not pd.isnull(vval)
                ):
                    self.add_error(
                        'raw_categories',
                        _(
                            'The values in the column are not compatible '
                            + ' with these ones.'))
                    return form_data
            else:
                valid_values = []

            self.instance.set_categories(valid_values, update=False)

        # Check the datetimes. One needs to be after the other
        a_from = self.cleaned_data.get('active_from')
        a_to = self.cleaned_data.get('active_to')
        if a_from and a_to and a_from >= a_to:
            self.add_error('active_from', _('Incorrect date/time window'))
            self.add_error('active_to', _('Incorrect date/time window'))

        # Check and force a correct column index
        ncols = self.workflow.columns.count()
        if form_data['position'] < 1 or form_data['position'] > ncols:
            form_data['position'] = ncols + 1

        return form_data
Пример #8
0
    def clean(self):
        """Check that the name is legal and the categories have right value."""
        form_data = super().clean()

        # Load the data frame from the DB for various checks and leave it in
        # the form for future use
        self.data_frame = load_table(self.workflow.get_data_frame_table_name())

        # Column name must be a legal variable name
        if 'name' in self.changed_data:
            # Name is legal
            msg = is_legal_name(form_data['name'])
            if msg:
                self.add_error('name', msg)
                return form_data

            # Check that the name is not present already
            if next(
                (col for col in self.workflow.columns.all() if
                 col.id != self.instance.id and col.name == form_data['name']),
                    None,
            ):
                # New column name collides with existing one
                self.add_error('name',
                               _('There is a column already with this name'))
                return form_data

        # Categories must be valid types
        if 'raw_categories' in self.changed_data:
            if form_data['raw_categories']:
                # Condition 1: Values must be valid for the type of the column
                category_values = [
                    cat.strip()
                    for cat in form_data['raw_categories'].split(',')
                ]
                try:
                    valid_values = Column.validate_column_values(
                        form_data['data_type'], category_values)
                except ValueError:
                    self.add_error(
                        'raw_categories',
                        _('Incorrect list of values'),
                    )
                    return form_data

                # Condition 2: The values in the dataframe column must be in
                # these categories (only if the column is being edited, though
                if self.instance.name and not all(
                        vval in valid_values
                        for vval in self.data_frame[self.instance.name]
                        if vval and not pd.isnull(vval)):
                    self.add_error(
                        'raw_categories',
                        _('The values in the column are not compatible ' +
                          ' with these ones.'))
                    return form_data
            else:
                valid_values = []

            self.instance.set_categories(valid_values)

        # Check the datetimes. One needs to be after the other
        a_from = self.cleaned_data.get('active_from')
        a_to = self.cleaned_data.get('active_to')
        if a_from and a_to and a_from >= a_to:
            self.add_error('active_from', _('Incorrect date/time window'))
            self.add_error('active_to', _('Incorrect date/time window'))

        return form_data