add comment logic not blocking explicit explicit text and content

The add comment logic of my blogging app is not blocking explicit texts and explicit contents.

Here is my add_comment view.

def add_comment(request, pk):
    post = get_object_or_404(Post, pk=pk)

    if request.method == 'POST':
        comment_form = CommentForm(request.POST, request.FILES)
        if comment_form.is_valid():
            comment = comment_form.save(commit=False)
            comment.author = request.user
            comment.post = post

            # Check for explicit text in the comment
            if is_text_explicit(comment.content):
                messages.error(request, "Explicit text detected in the comment. Comment rejected.")
                return render(request, 'CasBlog/post_detail.html', {'post': post, 'comment_form': comment_form})

            # Check for explicit content in attachments (images/videos)
            files = request.FILES.getlist('attachments')
            for file in files:
                # Save the file temporarily
                with tempfile.NamedTemporaryFile(delete=False) as temp_file:
                    for chunk in file.chunks():
                        temp_file.write(chunk)
                    temp_path = temp_file.name

                # Check for explicit content
                if is_content_explicit(temp_path):
                    os.unlink(temp_path)  # Delete the temporary file
                    messages.error(request, f"Explicit content detected in the attachment '{file.name}'. Comment rejected.")
                    return render(request, 'CasBlog/post_detail.html', {'post': post, 'comment_form': comment_form})

                os.unlink(temp_path)  # Delete the temporary file after checking

            # Save the comment
            comment.save()

            # Save the attachments to the database
            for file in files:
                CommentAttachment.objects.create(comment=comment, file=file)

            messages.success(request, "Comment added successfully!")
            return redirect('CasBlog:post_detail', slug=post.slug, pk=post.pk)

    else:
        comment_form = CommentForm()

    return render(request, 'CasBlog/post_detail.html', {'post': post, 'comment_form': comment_form})```

You have stated that this is not blocking explicit text. What is happening? Are you getting an error on the console? Is it just accepting everything?

What are your is_text_explicit and is_content_explicit functions?

There are no errors on the console.
Yes, it is accepting everything

Here are my is_text_explicit and is_content_explicit functions.

    params = {
        'api_user': SIGHTENGINE_API_USER,
        'api_secret': SIGHTENGINE_API_SECRET,
        'models': 'profanity',  # Use 'models' instead of 'categories'
        'text': text,
        'lang': 'en',
        'mode': 'standard',  # Add the 'mode' parameter
    }

    try:
        response = requests.get('https://api.sightengine.com/1.0/text/check.json', params=params)
        result = response.json()

        logger.info(f"🔍 Full API Response for text '{text}': {result}")

        if result['status'] == 'success':
            # Check if profanity matches are found
            if result.get('profanity', {}).get('matches'):
                logger.info(f"Explicit content detected in text: {text}")
                return True
    except Exception as e:
        logger.error(f"Error checking text with Sightengine API: {e}")

    # Check using custom word list
    EXPLICIT_WORDS = { }  # Add more explicit words as needed
    words = set(text.lower().split())
    if words.intersection(EXPLICIT_WORDS):
        logger.info(f"Explicit content detected in text (custom word list): {text}")
        return True

    logger.info(f"No explicit content detected in text: {text}")
    return False```


```def is_content_explicit(file_path_or_url, is_url=False):
    try:
        if is_url:
            params = {
                'models': 'nudity,offensive',
                'api_user': SIGHTENGINE_API_USER,
                'api_secret': SIGHTENGINE_API_SECRET,
                'url': file_path_or_url,
            }
            response = requests.post(API_URL, data=params)
        else:
            with open(file_path_or_url, 'rb') as file:
                files = {'media': file}
                params = {
                    'models': 'nudity,offensive',
                    'api_user': SIGHTENGINE_API_USER,
                    'api_secret': SIGHTENGINE_API_SECRET,
                }
                response = requests.post(API_URL, files=files, data=params)

        result = response.json()
        logger.info(f"📸 API Response for media '{file_path_or_url}': {result}")  # ✅ Log API response

        if result['status'] == 'success':
            nudity_score = result.get('nudity', {}).get('raw', 0)
            offensive_score = result.get('offensive', {}).get('prob', 0)

            if nudity_score > NUDITY_THRESHOLD or offensive_score > OFFENSIVE_THRESHOLD:
                logger.warning(f"🚨 Explicit content detected: Nudity {nudity_score}, Offensive {offensive_score}")
                return True  # ✅ Block content
        return False
    except Exception as e:
        logger.error(f"❌ Error checking media with Sightengine API: {e}")
        return True  # ✅ Fail-safe: Block content if API fails```

You have plenty of logging in your functions - what do your logs say when you submit explicit content?

I’m not familiar with this api, but from what I can see at https://sightengine.com/docs/text-moderation-ml-models, there isn’t a model named “profanity”. I see a category by that name for the rule-based filtering, but not a model.

Do you have a reference to the docs where this combination is allowed?

Thanks so much I will look at the documentation for the models. But other logics like the create_post, post_edit and comment_edit are blocking explicit texts and contents with the same functions except the add_comment logic

Here are the logs that I got after submitted an explicit text in the add_comment section ‘“POST /post/hello/ HTTP/1.1” 302 0
“GET /post/hello/ HTTP/1.1” 200 39376’

Looks like you need to configure logging so that you can see all the outputs of the logger.info() calls if you’re trying to debug what’s happening.