Skip to content
30 changes: 28 additions & 2 deletions forums/settings.py
Comment thread
ankitamk14 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -267,28 +267,54 @@
except ImportError:
pass


LOGGING = {
'version': 1,
'disable_existing_loggers': False,

'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},

'formatters': {
'verbose': {
'format': '[{asctime}] {levelname} {name}: {message}',
'style': '{',
},
},

'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},

# 🔹 New handler for spam detection
Comment thread
Bhavishya-Chaturvedi marked this conversation as resolved.
Outdated
'spam_file': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': BASE_DIR / 'logs/spam_detection.log', # create logs/ dir
Comment thread
Bhavishya-Chaturvedi marked this conversation as resolved.
Outdated
'maxBytes': 1024 * 1024 * 5, # 5 MB
'backupCount': 5, # keep 5 old logs
'formatter': 'verbose',
},
},

'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},

# 🔹 New dedicated logger
Comment thread
Bhavishya-Chaturvedi marked this conversation as resolved.
Outdated
'spam_detection': {
'handlers': ['spam_file'],
'level': 'INFO',
'propagate': False,
},
}
}

Expand Down
18 changes: 11 additions & 7 deletions website/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
sw = stopwords.words('english')

# Configure logging for spam detection
logging.basicConfig(level=logging.INFO)
import logging
spam_logger = logging.getLogger('spam_detection')


Expand Down Expand Up @@ -113,7 +113,7 @@ def __init__(self):
def extract_urls(self, text: str):
return re.findall(r'https?://[^\s)<>"]+', text)

def detect_spam(self, title: str, content: str, category: str = "", tutorial: str = "") -> dict:
def detect_spam(self,user,question, title: str, content: str, category: str = "", tutorial: str = "") -> dict:
combined_text = " ".join(filter(None, [title, content, category, tutorial])).lower()
spam_score = 0
matches = []
Expand Down Expand Up @@ -160,8 +160,10 @@ def detect_spam(self, title: str, content: str, category: str = "", tutorial: st
}

# debug log
spam_logger.info(f"SpamDetect result: score={spam_score} action={action} matches={len(matches)}")

spam_logger.info(
"SpamDetect result: question_id=%s user_id=%s score=%s action=%s matches=%s",
question.id, user.id, spam_score, action, len(matches)
)
return result


Expand All @@ -175,6 +177,8 @@ def handle_spam(question, user, delete_on_high=True, save_question_metadata_befo
"""
detector = SpamQuestionDetector()
result = detector.detect_spam(
user=user,
question=question,
title=getattr(question, 'title', '') or '',
content=getattr(question, 'body', '') or '',
category=getattr(question, 'category', '') or '',
Expand All @@ -188,7 +192,7 @@ def handle_spam(question, user, delete_on_high=True, save_question_metadata_befo

# prepare log payload
log_payload = {
#'question_id': question.id,
'question_id': question.id,
'user_id': user.id if user else None,
'category': getattr(question, 'category', '') or '',
'title': getattr(question, 'title', '') or '',
Expand All @@ -206,8 +210,8 @@ def handle_spam(question, user, delete_on_high=True, save_question_metadata_befo

if delete_on_high:
# delete after logging
spam_logger.info(f"AUTO_DELETE: Question {question.id} by user {user.id} score={spam_score}")
question.delete()
spam_logger.info(f"MARK_INACTIVE: Question {question.id} by user {user.id} score={spam_score}")
question.status = 0
Comment thread
ankitamk14 marked this conversation as resolved.
Outdated
return 'AUTO_DELETE'
else:
# hide instead of delete
Expand Down