Ejemplo n.º 1
0
def comment_reply_post_create_handler(sender, instance, action, model, pk_set,
    using, **kwargs):
    if action == 'post_add':
        for replied_to_comment in instance.replied_to_comments.all():
            moderator_settings = getattr(settings, 'MODERATOR', None)
            offset_timedelta = timedelta(seconds=1)
            if moderator_settings:
                if 'REPLY_BEFORE_COMMENT' in moderator_settings:
                    if moderator_settings['REPLY_BEFORE_COMMENT']:
                        offset_timedelta = timedelta(seconds=-1)

            created = False
            # We use try except DoesNotExist instead of get or create to
            # allow us to add a is_reply_comment to a newly created comment
            # which facilitates realtime_comment_classifier below to distinguish
            # between normal comments and reply comments.
            try:
                comment_obj = Comment.objects.get(
                    content_type=replied_to_comment.content_type,
                    object_pk=replied_to_comment.object_pk,
                    site=replied_to_comment.site,
                    submit_date=replied_to_comment.submit_date + offset_timedelta,
                    user=instance.user,
                )
            except Comment.DoesNotExist:
                comment_obj = Comment(
                    content_type=replied_to_comment.content_type,
                    object_pk=replied_to_comment.object_pk,
                    site=replied_to_comment.site,
                    submit_date=replied_to_comment.submit_date + offset_timedelta,
                    user=instance.user,
                    comment=instance.comment_text,
                )
                comment_obj.is_reply_comment = True
                comment_obj.save()
                created = True

            if not created:
                comment_obj.comment = instance.comment_text
                comment_obj.save()

            if comment_obj not in instance.reply_comments.all():
                instance.reply_comments.add(comment_obj)