Ejemplo n.º 1
0
def is_spam(text, request):
    """Generic spam checker interface."""
    if not text:
        return False
    akismet = get_akismet()
    if akismet is not None:
        from akismet import AkismetServerError, SpamStatus

        user_ip = get_ip_address(request)
        user_agent = get_user_agent_raw(request)

        try:
            result = akismet.check(
                user_ip=user_ip,
                user_agent=user_agent,
                comment_content=text,
                comment_type="comment",
            )
            if result:
                try:
                    raise Exception(
                        f"Akismet reported spam: {user_ip} / {user_agent} / {text!r}"
                    )
                except Exception:
                    report_error(cause="Akismet reported spam")
            return result == SpamStatus.DefiniteSpam
        except (OSError, AkismetServerError):
            report_error()
            return True
    return False
Ejemplo n.º 2
0
    def add(self, unit, target, request, vote=False):
        """Create new suggestion for this unit."""
        from weblate.auth.models import get_anonymous

        user = request.user if request else get_anonymous()

        if unit.translated and unit.target == target:
            return False

        same_suggestions = self.filter(target=target, unit=unit)
        # Do not rely on the SQL as MySQL compares strings case insensitive
        for same in same_suggestions:
            if same.target == target:
                if same.user == user or not vote:
                    return False
                same.add_vote(request, Vote.POSITIVE)
                return False

        # Create the suggestion
        suggestion = self.create(
            target=target,
            unit=unit,
            user=user,
            userdetails={
                "address": get_ip_address(request),
                "agent": get_user_agent_raw(request),
            },
        )

        # Record in change
        Change.objects.create(
            unit=unit,
            suggestion=suggestion,
            action=Change.ACTION_SUGGESTION,
            user=user,
            target=target,
            author=user,
        )

        # Add unit vote
        if vote:
            suggestion.add_vote(request, Vote.POSITIVE)

        # Update suggestion stats
        if user is not None:
            user.profile.suggested += 1
            user.profile.save()

        return suggestion
Ejemplo n.º 3
0
def is_spam(text, request):
    """Generic spam checker interface."""
    if settings.AKISMET_API_KEY:
        from akismet import Akismet

        akismet = Akismet(settings.AKISMET_API_KEY, get_site_url())
        try:
            return akismet.comment_check(
                get_ip_address(request),
                get_user_agent_raw(request),
                comment_content=text,
                comment_type="comment",
            )
        except OSError:
            report_error()
            return True
    return False
Ejemplo n.º 4
0
 def add(self, unit, request, text):
     """Add comment to this unit."""
     user = request.user
     new_comment = self.create(
         user=user,
         unit=unit,
         comment=text,
         userdetails={
             "address": get_ip_address(request),
             "agent": get_user_agent_raw(request),
         },
     )
     Change.objects.create(
         unit=unit,
         comment=new_comment,
         action=Change.ACTION_COMMENT,
         user=user,
         author=user,
         details={"comment": text},
     )