<commit_before>"""Spam checker for Zinnia"""
import warnings

from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured

from zinnia.settings import SPAM_CHECKER_BACKENDS


def get_spam_checker(backend_path):
    """Return the selected spam checker backend"""
    try:
        backend_module = import_module(backend_path)
        backend = getattr(backend_module, 'backend')
    except (ImportError, AttributeError):
        warnings.warn('%s backend cannot be imported' % backend_path,
                      RuntimeWarning)
        backend = None
    except ImproperlyConfigured, e:
        warnings.warn(str(e), RuntimeWarning)
        backend = None

    return backend


def check_is_spam(content, content_object, request,
                  backends=SPAM_CHECKER_BACKENDS):
    """Return True if the content is a spam, else False"""
    for backend_path in backends:
        spam_checker = get_spam_checker(backend_path)
        is_spam = spam_checker(content, content_object, request)
        if is_spam:
            return True

    return False
<commit_msg>fix check_is_spam is the spam-checker backend is None, close #199<commit_after>"""Spam checker for Zinnia"""
import warnings

from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured

from zinnia.settings import SPAM_CHECKER_BACKENDS


def get_spam_checker(backend_path):
    """Return the selected spam checker backend"""
    try:
        backend_module = import_module(backend_path)
        backend = getattr(backend_module, 'backend')
    except (ImportError, AttributeError):
        warnings.warn('%s backend cannot be imported' % backend_path,
                      RuntimeWarning)
        backend = None
    except ImproperlyConfigured, e:
        warnings.warn(str(e), RuntimeWarning)
        backend = None

    return backend


def check_is_spam(content, content_object, request,
                  backends=SPAM_CHECKER_BACKENDS):
    """Return True if the content is a spam, else False"""
    for backend_path in backends:
        spam_checker = get_spam_checker(backend_path)
        if spam_checker is not None:
            is_spam = spam_checker(content, content_object, request)
            if is_spam:
                return True

    return False

{'fraction_non_alphanumeric': 0.06973740852346104, 'fraction_numerical': 0.0012914334911752045, 'mean_word_length': 3.15, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7127149', 'n_tokens_mistral': 674, 'n_tokens_neox': 654, 'n_words': 184}