Tenhal/landing_page/middleware.py
2025-12-29 14:08:24 +03:00

27 lines
889 B
Python

# middleware.py
import logging
from django_q.tasks import async_task
logger = logging.getLogger(__name__)
class AnalyticsMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# ip = request.META.get('REMOTE_ADDR')
# 1. Get the real IP
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
# Get the first IP in the list (the original client)
ip = x_forwarded_for.split(',')[0].strip()
else:
ip = request.META.get('REMOTE_ADDR')
# LOG 1: Check if Middleware is even being triggered
print(f"DEBUG: Middleware triggered for IP: {ip}")
# Hand off to the queue
async_task('landing_page.tasks.log_visitor_location', ip)
return self.get_response(request)