<commit_before>from django.db import models
from django.conf import settings
from django.contrib.comments import get_form
from django.contrib.comments.signals import comment_was_posted
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import redirect, render_to_response
from django.template import loader, Context, RequestContext
from django.utils.translation import ugettext_lazy as _

from django_comments_xtd import signals, signed
from django_comments_xtd.models import (XtdComment, TmpXtdComment, 
                                        max_thread_level_for_content_type)
from django_comments_xtd.utils import send_mail


SALT = getattr(settings, 'COMMENTS_XTD_SALT', "")
CONFIRM_EMAIL = getattr(settings, 'COMMENTS_XTD_CONFIRM_EMAIL', True)
MAX_THREAD_LEVEL = getattr(settings, 'COMMENTS_XTD_MAX_THREAD_LEVEL', 0)

def send_email_confirmation_request(comment, target, key, text_template="django_comments_xtd/email_confirmation_request.txt", html_template="django_comments_xtd/email_confirmation_request.html"):
    """Send email requesting comment confirmation"""
    subject = _("comment confirmation request")
    confirmation_url = reverse("comments-xtd-confirm", args=[key])
    message_context = Context({ 'comment': comment, 
                                'content_object': target, 
                                'confirmation_url': confirmation_url,
                                'contact': settings.DEFAULT_FROM_EMAIL,
                                'site': Site.objects.get_current() })
    # prepare text message
    text_message_template = loader.get_template(text_template)
    text_message = text_message_template.render(message_context)
    # prepare html message
    html_message_template = loader.get_template(html_template)
    html_message = html_message_template.render(message_context)

    send_mail(subject, text_message, settings.DEFAULT_FROM_EMAIL,
              [ comment.user_email, ], html=html_message)

def _comment_exists(comment):
    """
    True if exists a XtdComment with same user_name, user_email and submit_date.
    """
    return (XtdComment.objects.filter(
            user_name=comment.user_name, 
            user_email=comment.user_email,
            submit_date=comment.submit_date).count() > 0)


def _create_comment(tmp_comment):
    """
    Creates a XtdComment from a TmpXtdComment.
    """
    comment = XtdComment(**tmp_comment)
    comment.is_public = True
    comment.save()
    return comment


def on_comment_was_posted(sender, comment, request, **kwargs):
    """
    Post the comment if a user is authenticated or send a confirmation email.
    
    On signal django.contrib.comments.signals.comment_was_posted check if the 
    user is authenticated or if settings.COMMENTS_XTD_CONFIRM_EMAIL is False. 
    In both cases will post the comment. Otherwise will send a confirmation
    email to the person who posted the comment.
    """
    if not CONFIRM_EMAIL or (comment.user and comment.user.is_authenticated()):
        if not _comment_exists(comment):
            new_comment = _create_comment(comment)
            comment.xtd_comment = new_comment
            notify_comment_followers(new_comment)
    else:
        ctype = request.POST["content_type"]
        object_pk = request.POST["object_pk"]
        model = models.get_model(*ctype.split("."))
        target = model._default_manager.get(pk=object_pk)
        key = signed.dumps(comment, compress=True, extra_key=SALT)
        send_email_confirmation_request(comment, target, key)

comment_was_posted.connect(on_comment_was_posted)


def sent(request):
    comment_pk = request.GET.get("c", None)
    try:
        comment_pk = int(comment_pk)
        comment = XtdComment.objects.get(pk=comment_pk)
    except (TypeError, ValueError, XtdComment.DoesNotExist):
        template_arg = ["django_comments_xtd/posted.html",
                        "comments/posted.html"]
        return render_to_response(template_arg, 
                                  context_instance=RequestContext(request))
    else:
        if request.is_ajax() and comment.user and comment.user.is_authenticated():
            template_arg = [
                "django_comments_xtd/%s/%s/comment.html" % (
                    comment.content_type.app_label, 
                    comment.content_type.model),
                "django_comments_xtd/%s/comment.html" % (
                    comment.content_type.app_label,),
                "django_comments_xtd/comment.html"
            ]
            return render_to_response(template_arg, {"comment": comment},
                                      context_instance=RequestContext(request))
        else:
            return redirect(comment)


def confirm(request, key, template_discarded="django_comments_xtd/discarded.html"):<commit_msg>Only connect signal when settings.COMMENTS_APP points to django_comments_xtd<commit_after>from django.db import models
from django.conf import settings
from django.contrib.comments import get_form
from django.contrib.comments.signals import comment_was_posted
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import redirect, render_to_response
from django.template import loader, Context, RequestContext
from django.utils.translation import ugettext_lazy as _

from django_comments_xtd import signals, signed
from django_comments_xtd.models import (XtdComment, TmpXtdComment, 
                                        max_thread_level_for_content_type)
from django_comments_xtd.utils import send_mail


SALT = getattr(settings, 'COMMENTS_XTD_SALT', "")
CONFIRM_EMAIL = getattr(settings, 'COMMENTS_XTD_CONFIRM_EMAIL', True)
MAX_THREAD_LEVEL = getattr(settings, 'COMMENTS_XTD_MAX_THREAD_LEVEL', 0)

def send_email_confirmation_request(comment, target, key, text_template="django_comments_xtd/email_confirmation_request.txt", html_template="django_comments_xtd/email_confirmation_request.html"):
    """Send email requesting comment confirmation"""
    subject = _("comment confirmation request")
    confirmation_url = reverse("comments-xtd-confirm", args=[key])
    message_context = Context({ 'comment': comment, 
                                'content_object': target, 
                                'confirmation_url': confirmation_url,
                                'contact': settings.DEFAULT_FROM_EMAIL,
                                'site': Site.objects.get_current() })
    # prepare text message
    text_message_template = loader.get_template(text_template)
    text_message = text_message_template.render(message_context)
    # prepare html message
    html_message_template = loader.get_template(html_template)
    html_message = html_message_template.render(message_context)

    send_mail(subject, text_message, settings.DEFAULT_FROM_EMAIL,
              [ comment.user_email, ], html=html_message)

def _comment_exists(comment):
    """
    True if exists a XtdComment with same user_name, user_email and submit_date.
    """
    return (XtdComment.objects.filter(
            user_name=comment.user_name, 
            user_email=comment.user_email,
            submit_date=comment.submit_date).count() > 0)


def _create_comment(tmp_comment):
    """
    Creates a XtdComment from a TmpXtdComment.
    """
    comment = XtdComment(**tmp_comment)
    comment.is_public = True
    comment.save()
    return comment


def on_comment_was_posted(sender, comment, request, **kwargs):
    """
    Post the comment if a user is authenticated or send a confirmation email.
    
    On signal django.contrib.comments.signals.comment_was_posted check if the 
    user is authenticated or if settings.COMMENTS_XTD_CONFIRM_EMAIL is False. 
    In both cases will post the comment. Otherwise will send a confirmation
    email to the person who posted the comment.
    """
    if not CONFIRM_EMAIL or (comment.user and comment.user.is_authenticated()):
        if not _comment_exists(comment):
            new_comment = _create_comment(comment)
            comment.xtd_comment = new_comment
            notify_comment_followers(new_comment)
    else:
        ctype = request.POST["content_type"]
        object_pk = request.POST["object_pk"]
        model = models.get_model(*ctype.split("."))
        target = model._default_manager.get(pk=object_pk)
        key = signed.dumps(comment, compress=True, extra_key=SALT)
        send_email_confirmation_request(comment, target, key)

if settings.COMMENTS_APP == "django_comments_xtd":
    comment_was_posted.connect(on_comment_was_posted)


def sent(request):
    comment_pk = request.GET.get("c", None)
    try:
        comment_pk = int(comment_pk)
        comment = XtdComment.objects.get(pk=comment_pk)
    except (TypeError, ValueError, XtdComment.DoesNotExist):
        template_arg = ["django_comments_xtd/posted.html",
                        "comments/posted.html"]
        return render_to_response(template_arg, 
                                  context_instance=RequestContext(request))
    else:
        if request.is_ajax() and comment.user and comment.user.is_authenticated():
            template_arg = [
                "django_comments_xtd/%s/%s/comment.html" % (
                    comment.content_type.app_label, 
                    comment.content_type.model),
                "django_comments_xtd/%s/comment.html" % (
                    comment.content_type.app_label,),
                "django_comments_xtd/comment.html"
            ]
            return render_to_response(template_arg, {"comment": comment},
                                      context_instance=RequestContext(request))
        else:
            return redirect(comment)


def confirm(request, key, template_discarded="django_comments_xtd/discarded.html"):
{'fraction_non_alphanumeric': 0.08205799436165928, 'fraction_numerical': 0.0010068465565847766, 'mean_word_length': 3.1045454545454545, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4304731', 'n_tokens_mistral': 2820, 'n_tokens_neox': 2715, 'n_words': 577}