Пример #1
0
    def validateUnique(self, action, data):
        """Validate unique.
        
        Since this class always adds a single object, we can safely
        return an empty list of errors.
        
        """
        errors = []
        domain_model = removeSecurityProxy(self.getDomainModel())

        # find unique columns in data model.. TODO do this statically
        mapper = rdb.orm.class_mapper(domain_model)
        ucols = list(ui.unique_columns(mapper))

        # query out any existing values with the same unique values,
        session = Session()
        # find data matching unique columns
        for key, col in ucols:
            if key in data:
                # on edit ignore new value if its the same as the previous value
                if isinstance(self.context, domain_model) \
                   and data[key] == getattr(self.context, key, None):
                    continue
                value = session.query(domain_model).filter(
                    col == data[key]).count()
                if not value:
                    continue
                widget = self.widgets[key]
                error = formlib.form.WidgetInputError(
                    widget.name, widget.label,
                    _(u"A record with this value already exists"))
                widget._error = error
                errors.append(error)
        return errors
Пример #2
0
    def validateUnique(self, action, data):
        """Validate unique.
        
        Since this class always adds a single object, we can safely
        return an empty list of errors.
        
        """
        errors = []
        domain_model = removeSecurityProxy(self.getDomainModel())

        # find unique columns in data model.. TODO do this statically
        mapper = rdb.orm.class_mapper(domain_model)
        ucols = list(ui.unique_columns(mapper))

        # query out any existing values with the same unique values,
        session = Session()
        # find data matching unique columns
        for key, col in ucols:
            if key in data:
                # on edit ignore new value if its the same as the previous value
                if isinstance(self.context, domain_model) \
                   and data[key] == getattr(self.context, key, None):
                   continue
                value = session.query(domain_model
                    ).filter(col == data[key]).count()
                if not value:
                    continue
                widget = self.widgets[ key ]
                error = formlib.form.WidgetInputError(
                    widget.name, widget.label,
                    _(u"Duplicate Value for Unique Field"))
                widget._error = error
                errors.append(error)
        return errors