def get_class_with_tablename(cls):
    """
    Returns the first parent found (or the class itself) for given class which
    has __tablename__ attribute set.

    This function is needed for slug uniqueness testing when using concrete
    inheritance.

    :param cls: class to inspect
    """
    mapper_args = {}
    if hasattr(cls, '__mapper_args__'):
        mapper_args = cls.__mapper_args__

    if 'inherits' not in mapper_args:
        return cls
    if cls.__tablename__ != mapper_args['inherits'].__tablename__:
        return cls
    for parent in cls.__bases__:
        result = get_class_with_tablename(parent)
        if result:
            return result
    return None