<commit_before>import datetime

from django.conf import settings
from django.core.mail import send_mail
from django.db.models import signals
from django.dispatch import dispatcher
from django.template import Context, loader
from django.contrib.comments.models import Comment, FreeComment
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site


registered_models = {}


def register_for_moderation(model, enable_field_name=None, auto_moderate=None, akismet=False, email_notification=False):
    """
    Registers a model for automatic moderation and management.
    
    Pass the model class as the first argument, followed by keyword
    arguments specifying the management options to enable::
    
        akismet
            Causes new comments to instances of this model to be
            subjected to an Akismet spam check before saving; if
            Akismet thinks the comment is spam, its ``is_public``
            field will be set to ``False``. The value of this argument
            should be ``True`` or ``False``.
    
        auto_close
            Causes new comments to instances of this model to be
            automatically rejected when a certain number of days after
            the original publication have elapsed. The value of this
            argument should be a ``DateTimeField`` on the model which
            indicates the publication date.
    
        auto_moderate
            Causes new comments to instances of this model to
            automatically have ``is_public`` set to ``False`` when a
            certain number of days after the original publication have
            elapsed. The value of this argument should be a
            ``DateTimeField`` on the model which indicates the
            publication date.
    
        email_notification
            Causes an email to be sent to each person listed in the
            ``MANAGERS`` setting whenever a new comment is posted to
            an instance of this model. The value of this argument
            should be ``True`` or ``False``.
    
        enable_field_name
            The name of a ``BooleanField`` on the model which is
            ``True`` when comments are open for an instance of the
            model, and ``False`` when comments are closed. When
            comments are closed, no new comments on that object will
            be accepted.
    
    At least one option must be enabled; passing ``False`` values for
    all options, or passing no options, will raise ``ValueError``.
    
    If ``auto_close`` or ``auto_moderate`` are used, you must add the
    setting ``COMMENTS_MODERATE_AFTER`` to your Django settings file;
    the value of this setting should be an integer specifying the
    number of days past publication of an object when ``auto_close``
    or ``auto_moderate`` should begin to operate.
    
    If ``akismet`` is used, you must add the setting
    ``AKISMET_API_KEY`` to your Django settings file; the value of
    this setting should be a valid Akismet API key for use in
    performing Akismet spam checks.
    
    Examples
    --------
    
    Consider the following model::
    
        class Entry(models.Model):
            title = models.CharField(maxlength=250)
            slug = models.SlugField(prepopulate_from=('title',))
            pub_date = models.DateTimeField()
            enable_comments = models.BooleanField()
            body = models.TextField()
    
    To set up automatic Akismet moderation of Entries, register like so::
    
        from comment_utils.moderation import register_for_moderation
        register_for_moderation(Entry, akismet=True)
    
    To have comments automatically rejected when the
    ``enable_comments`` field is ``False``::
    
        register_for_moderation(Entry, enable_field_name='enable_comments')
    
    To have comments automatically be marked non-public when posted to
    Entries published more than ``settings.COMMENTS_MODERATE_AFTER``
    days ago::
    
        register_for_moderation(Entry, auto_moderate='pub_date')
    
    To use all three of these options together::
    
        register_for_moderation(Entry, akismet=True, auto_moderate='pub_date',
                                enable_field_name='enable_comments')
    
    Note that ``auto_close`` and ``auto_moderate`` are exclusive of
    one another; if both are used, ``auto_close`` will "win" and new
    comments will be deleted, rather than be marked non-public.
    
    """
    key = '%s.%s' % (model._meta.app_label, model._meta.module_name)
    if not (akismet or auto_close or auto_moderate or email_notification or enable_field_name):
        raise ValueError("To register the model '%s' for comment moderation, you must specify at least one moderation option")
    registered_models[key] = { 'enable_field_name': enable_field_name,
                               'auto_moderate': auto_moderate,
                               'akismet': akismet,
                               'email_notification': email_notification }


def disallowed_by_field(content_object, field_name):
    return not getattr(content_object, field_name)


def auto_moderated(content_object, field_name):
    return not (datetime.datetime.today() - datetime.timedelta(settings.COMMENTS_MODERATE_AFTER) <= getattr(content_object, field_name))

def akismet_flagged(instance):
    from akismet import Akismet<commit_msg>Docstring tweaks<commit_after>import datetime

from django.conf import settings
from django.core.mail import send_mail
from django.db.models import signals
from django.dispatch import dispatcher
from django.template import Context, loader
from django.contrib.comments.models import Comment, FreeComment
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site


registered_models = {}


def register_for_moderation(model, enable_field_name=False, auto_moderate=False, akismet=False, email_notification=False):
    """
    Registers a model for automatic moderation and management.
    
    Pass the model class as the first argument, followed by keyword
    arguments specifying the management options to enable::
    
        akismet
            Causes new comments to instances of this model to be
            subjected to an Akismet spam check before saving; if
            Akismet thinks the comment is spam, its ``is_public``
            field will be set to ``False``. The value of this argument
            should be ``True`` or ``False``.
    
        auto_close
            Causes new comments to instances of this model to be
            automatically rejected when a certain number of days after
            the original publication have elapsed. The value of this
            argument should be a ``DateTimeField`` on the model which
            indicates the publication date; do not pass this argument
            if you do not want to enable it.
    
        auto_moderate
            Causes new comments to instances of this model to
            automatically have ``is_public`` set to ``False`` when a
            certain number of days after the original publication have
            elapsed. The value of this argument should be a
            ``DateTimeField`` on the model which indicates the
            publication date. Do not pass this argument if you do not
            want to enable it.
    
        email_notification
            Causes an email to be sent to each person listed in the
            ``MANAGERS`` setting whenever a new comment is posted to
            an instance of this model. The value of this argument
            should be ``True`` or ``False``.
    
        enable_field_name
            The name of a ``BooleanField`` on the model which is
            ``True`` when comments are open for an instance of the
            model, and ``False`` when comments are closed. When
            comments are closed, no new comments on that object will
            be accepted. Do not pass this argument if you do not want
            to enable it.
    
    At least one option must be enabled; passing ``False`` values for
    all options, or passing no options, will raise ``ValueError``.
    
    If ``auto_close`` or ``auto_moderate`` are used, you must add the
    setting ``COMMENTS_MODERATE_AFTER`` to your Django settings file;
    the value of this setting should be an integer specifying the
    number of days past publication of an object when ``auto_close``
    or ``auto_moderate`` should begin to operate.
    
    If ``akismet`` is used, you must add the setting
    ``AKISMET_API_KEY`` to your Django settings file; the value of
    this setting should be a valid Akismet API key for use in
    performing Akismet spam checks.
    
    Examples
    --------
    
    Consider the following model::
    
        class Entry(models.Model):
            title = models.CharField(maxlength=250)
            slug = models.SlugField(prepopulate_from=('title',))
            pub_date = models.DateTimeField()
            enable_comments = models.BooleanField()
            body = models.TextField()
    
    To set up automatic Akismet moderation of Entries, register like so::
    
        from comment_utils.moderation import register_for_moderation
        register_for_moderation(Entry, akismet=True)
    
    To have comments automatically rejected when the
    ``enable_comments`` field is ``False``::
    
        register_for_moderation(Entry, enable_field_name='enable_comments')
    
    To have comments automatically be marked non-public when posted to
    Entries published more than ``settings.COMMENTS_MODERATE_AFTER``
    days ago (using the ``pub_date`` field to determine the date of
    publication)::
    
        register_for_moderation(Entry, auto_moderate='pub_date')
    
    To use all three of these options together::
    
        register_for_moderation(Entry, akismet=True, auto_moderate='pub_date',
                                enable_field_name='enable_comments')
    
    Note that ``auto_close`` and ``auto_moderate`` are exclusive of
    one another; if both are used, ``auto_close`` will "win" and new
    comments will be deleted, rather than be marked non-public.
    
    """
    key = '%s.%s' % (model._meta.app_label, model._meta.module_name)
    if not (akismet or auto_close or auto_moderate or email_notification or enable_field_name):
        raise ValueError("To register the model '%s' for comment moderation, you must specify at least one moderation option")
    registered_models[key] = { 'enable_field_name': enable_field_name,
                               'auto_moderate': auto_moderate,
                               'akismet': akismet,
                               'email_notification': email_notification }


def disallowed_by_field(content_object, field_name):
    return not getattr(content_object, field_name)


def auto_moderated(content_object, field_name):
    return not (datetime.datetime.today() - datetime.timedelta(settings.COMMENTS_MODERATE_AFTER) <= getattr(content_object, field_name))

def akismet_flagged(instance):
    from akismet import Akismet
{'fraction_non_alphanumeric': 0.06326274228284279, 'fraction_numerical': 0.0005384063173007897, 'mean_word_length': 2.6965174129353233, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1046675', 'n_tokens_mistral': 2852, 'n_tokens_neox': 2754, 'n_words': 1139}