Ejemplo n.º 1
0
    def moderate(self, comment, content_object, request):
        """Determine whether a given comment on a given object should be
        allowed to show up immediately, or should be marked non-public
        and await approval."""
        if self.auto_moderate_comments:
            return True

        if check_is_spam(comment, content_object, request,
                         self.spam_checker_backends):
            return True

        return False
Ejemplo n.º 2
0
    def moderate(self, comment, content_object, request):
        """Determine if a new comment should be marked non-public and await
        approval. Return ``True`` to put the comment into the moderator queue,
        or ``False`` to allow it to show up immediately."""
        if self.auto_moderate_comments:
            return True

        if check_is_spam(comment, content_object, request,
                         self.spam_checker_backends):
            return True

        return False
Ejemplo n.º 3
0
    def moderate(self, comment, content_object, request):
        """Determine whether a given comment on a given object should be
        allowed to show up immediately, or should be marked non-public
        and await approval."""
        if self.auto_moderate_comments:
            return True

        if check_is_spam(comment, content_object, request,
                         self.spam_checker_backends):
            return True

        return False
Ejemplo n.º 4
0
    def moderate(self, comment, content_object, request):
        """Determine if a new comment should be marked non-public and await
        approval. Return ``True`` to put the comment into the moderator queue,
        or ``False`` to allow it to show up immediately."""
        if self.auto_moderate_comments:
            return True

        if check_is_spam(comment, content_object, request,
                         self.spam_checker_backends):
            return True

        return False
Ejemplo n.º 5
0
    def post(self, request, *args, **kwargs):
        """
        Check if an URL is provided and if trackbacks
        are enabled on the Entry.
        If so the URL is registered one time as a trackback.
        """
        url = request.POST.get('url')

        if not url:
            return self.get(request, *args, **kwargs)

        entry = self.get_object()
        site = Site.objects.get_current()

        if not entry.trackbacks_are_open:
            return self.render_to_response(
                {'error': 'Trackback is not enabled for %s' % entry.title})

        title = request.POST.get('title') or url
        excerpt = request.POST.get('excerpt') or title
        blog_name = request.POST.get('blog_name') or title
        ip_address = request.META.get('REMOTE_ADDR', None)

        trackback_klass = comments.get_model()
        trackback_datas = {
            'content_type': ContentType.objects.get_for_model(Entry),
            'object_pk': entry.pk,
            'site': site,
            'user_url': url,
            'user_name': blog_name,
            'ip_address': ip_address,
            'comment': excerpt
        }

        trackback = trackback_klass(**trackback_datas)
        if check_is_spam(trackback, entry, request):
            return self.render_to_response(
                {'error': 'Trackback considered like spam'})

        trackback_defaults = {'comment': trackback_datas.pop('comment')}
        trackback, created = trackback_klass.objects.get_or_create(
            defaults=trackback_defaults,
            **trackback_datas)
        if created:
            trackback.flags.create(user=get_user_flagger(), flag=TRACKBACK)
            trackback_was_posted.send(trackback.__class__,
                                      trackback=trackback,
                                      entry=entry)
        else:
            return self.render_to_response(
                {'error': 'Trackback is already registered'})
        return self.render_to_response({})
Ejemplo n.º 6
0
    def post(self, request, *args, **kwargs):
        """
        Check if an URL is provided and if trackbacks
        are enabled on the Entry.
        If so the URL is registered one time as a trackback.
        """
        url = request.POST.get('url')

        if not url:
            return self.get(request, *args, **kwargs)

        entry = self.get_object()
        site = Site.objects.get_current()

        if not entry.trackbacks_are_open:
            return self.render_to_response(
                {'error': 'Trackback is not enabled for %s' % entry.title})

        title = request.POST.get('title') or url
        excerpt = request.POST.get('excerpt') or title
        blog_name = request.POST.get('blog_name') or title
        ip_address = request.META.get('REMOTE_ADDR', None)

        trackback_klass = comments.get_model()
        trackback_datas = {
            'content_type': ContentType.objects.get_for_model(Entry),
            'object_pk': entry.pk,
            'site': site,
            'user_url': url,
            'user_name': blog_name,
            'ip_address': ip_address,
            'comment': excerpt
        }

        trackback = trackback_klass(**trackback_datas)
        if check_is_spam(trackback, entry, request):
            return self.render_to_response(
                {'error': 'Trackback considered like spam'})

        trackback_defaults = {'comment': trackback_datas.pop('comment')}
        trackback, created = trackback_klass.objects.get_or_create(
            defaults=trackback_defaults, **trackback_datas)
        if created:
            trackback.flags.create(user=get_user_flagger(), flag=TRACKBACK)
            trackback_was_posted.send(trackback.__class__,
                                      trackback=trackback,
                                      entry=entry)
        else:
            return self.render_to_response(
                {'error': 'Trackback is already registered'})
        return self.render_to_response({})
Ejemplo n.º 7
0
    def moderate(self, comment, content_object, request):
        """Determine whether a given comment on a given object should be
        allowed to show up immediately, or should be marked non-public
        and await approval."""
        if self.auto_moderate_comments:
            return True

        if check_is_spam(comment, content_object, request,
                         self.spam_checker_backends):
            comment.save()
            user = comment.content_object.authors.all()[0]
            comment.flags.create(user=user, flag='spam')
            return True

        return False
Ejemplo n.º 8
0
    def moderate(self, comment, content_object, request):
        """Determine whether a given comment on a given object should be
        allowed to show up immediately, or should be marked non-public
        and await approval."""
        if self.auto_moderate_comments:
            return True

        if check_is_spam(comment, content_object, request,
                         self.spam_checker_backends):
            comment.save()
            user = comment.content_object.authors.all()[0]
            comment.flags.create(user=user, flag='spam')
            return True

        return False
Ejemplo n.º 9
0
    def moderate(self, comment, content_object, request):
        """Determine whether a given comment on a given object should be
        allowed to show up immediately, or should be marked non-public
        and await approval."""
        if self.auto_moderate_comments:
            return True

        if check_is_spam(comment, content_object, request,
                         self.spam_checker_backends):
            comment.save()
            flag, created = CommentFlag.objects.get_or_create(
                comment=comment, user=get_user_flagger(), flag=SPAM)
            signals.comment_was_flagged.send(
                sender=comment.__class__, comment=comment,
                flag=flag, created=created, request=request)
            return True

        return False
Ejemplo n.º 10
0
    def moderate(self, comment, content_object, request):
        """Determine whether a given comment on a given object should be
        allowed to show up immediately, or should be marked non-public
        and await approval."""
        if self.auto_moderate_comments:
            return True

        if check_is_spam(comment, content_object, request,
                         self.spam_checker_backends):
            comment.save()
            flag, created = CommentFlag.objects.get_or_create(
                comment=comment, user=get_user_flagger(), flag=SPAM)
            signals.comment_was_flagged.send(sender=comment.__class__,
                                             comment=comment,
                                             flag=flag,
                                             created=created,
                                             request=request)
            return True

        return False
Ejemplo n.º 11
0
def pingback_ping(source, target):
    """
    pingback.ping(sourceURI, targetURI) => 'Pingback message'

    Notifies the server that a link has been added to sourceURI,
    pointing to targetURI.

    See: http://hixie.ch/specs/pingback/pingback-1.0
    """
    try:
        if source == target:
            return UNDEFINED_ERROR

        site = Site.objects.get_current()
        try:
            document = ''.join(map(
                lambda byte_line: byte_line.decode('utf-8'),
                urlopen(source).readlines()))
        except (HTTPError, URLError):
            return SOURCE_DOES_NOT_EXIST

        if target not in document:
            return SOURCE_DOES_NOT_LINK

        target_splitted = urlsplit(target)
        if target_splitted.netloc != site.domain:
            return TARGET_DOES_NOT_EXIST

        try:
            view, args, kwargs = resolve(target_splitted.path)
        except Resolver404:
            return TARGET_DOES_NOT_EXIST

        try:
            entry = Entry.published.get(
                slug=kwargs['slug'],
                publication_date__year=kwargs['year'],
                publication_date__month=kwargs['month'],
                publication_date__day=kwargs['day'])
            if not entry.pingbacks_are_open:
                return TARGET_IS_NOT_PINGABLE
        except (KeyError, Entry.DoesNotExist):
            return TARGET_IS_NOT_PINGABLE

        soup = BeautifulSoup(document, 'html.parser')
        title = str(soup.find('title'))
        title = title and strip_tags(title) or _('No title')
        description = generate_pingback_content(soup, target,
                                                PINGBACK_CONTENT_LENGTH)

        pingback_klass = comments.get_model()
        pingback_datas = {
            'content_type': ContentType.objects.get_for_model(Entry),
            'object_pk': entry.pk,
            'site': site,
            'user_url': source,
            'user_name': title,
            'comment': description
        }
        pingback = pingback_klass(**pingback_datas)
        if check_is_spam(pingback, entry, FakeRequest()):
            return PINGBACK_IS_SPAM

        pingback_defaults = {'comment': pingback_datas.pop('comment'),
                             'user_name': pingback_datas.pop('user_name')}
        pingback, created = pingback_klass.objects.get_or_create(
            defaults=pingback_defaults,
            **pingback_datas)
        if created:
            pingback.flags.create(user=get_user_flagger(), flag=PINGBACK)
            pingback_was_posted.send(pingback.__class__,
                                     pingback=pingback,
                                     entry=entry)
            return 'Pingback from %s to %s registered.' % (source, target)
        return PINGBACK_ALREADY_REGISTERED
    except Exception:
        return UNDEFINED_ERROR
Ejemplo n.º 12
0
def pingback_ping(source, target):
    """
    pingback.ping(sourceURI, targetURI) => 'Pingback message'

    Notifies the server that a link has been added to sourceURI,
    pointing to targetURI.

    See: http://hixie.ch/specs/pingback/pingback-1.0
    """
    try:
        if source == target:
            return UNDEFINED_ERROR

        site = Site.objects.get_current()
        try:
            document = ''.join(map(
                lambda byte_line: byte_line.decode('utf-8'),
                urlopen(source).readlines()))
        except (HTTPError, URLError):
            return SOURCE_DOES_NOT_EXIST

        if target not in document:
            return SOURCE_DOES_NOT_LINK

        target_splitted = urlsplit(target)
        if target_splitted.netloc != site.domain:
            return TARGET_DOES_NOT_EXIST

        try:
            view, args, kwargs = resolve(target_splitted.path)
        except Resolver404:
            return TARGET_DOES_NOT_EXIST

        try:
            entry = Entry.published.get(
                slug=kwargs['slug'],
                publication_date__year=kwargs['year'],
                publication_date__month=kwargs['month'],
                publication_date__day=kwargs['day'])
            if not entry.pingbacks_are_open:
                return TARGET_IS_NOT_PINGABLE
        except (KeyError, Entry.DoesNotExist):
            return TARGET_IS_NOT_PINGABLE

        soup = BeautifulSoup(document, 'html.parser')
        title = six.text_type(soup.find('title'))
        title = title and strip_tags(title) or _('No title')
        description = generate_pingback_content(soup, target,
                                                PINGBACK_CONTENT_LENGTH)

        pingback_klass = comments.get_model()
        pingback_datas = {
            'content_type': ContentType.objects.get_for_model(Entry),
            'object_pk': entry.pk,
            'site': site,
            'user_url': source,
            'user_name': title,
            'comment': description
        }
        pingback = pingback_klass(**pingback_datas)
        if check_is_spam(pingback, entry, FakeRequest()):
            return PINGBACK_IS_SPAM

        pingback_defaults = {'comment': pingback_datas.pop('comment'),
                             'user_name': pingback_datas.pop('user_name')}
        pingback, created = pingback_klass.objects.get_or_create(
            defaults=pingback_defaults,
            **pingback_datas)
        if created:
            pingback.flags.create(user=get_user_flagger(), flag=PINGBACK)
            pingback_was_posted.send(pingback.__class__,
                                     pingback=pingback,
                                     entry=entry)
            return 'Pingback from %s to %s registered.' % (source, target)
        return PINGBACK_ALREADY_REGISTERED
    except Exception:
        return UNDEFINED_ERROR