Fixed the user visit logging to work with IP behind Apache in the Mia core application.

This commit is contained in:
依瑪貓 2020-08-11 11:25:04 +08:00
parent 8c7ffc9b76
commit 96e1bd2374

View File

@ -93,7 +93,7 @@ def _log_visit(request):
if "visit_logged" in request.session:
return
user = request.user
ip = request.META["REMOTE_ADDR"]
ip = _get_remote_ip(request)
User.objects.filter(pk=user.pk).update(
visits=F("visits") + 1,
visited_at=Now(),
@ -104,6 +104,13 @@ def _log_visit(request):
request.session["visit_logged"] = True
def _get_remote_ip(request):
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
if x_forwarded_for:
return x_forwarded_for.split(",")[0]
return request.META.get('REMOTE_ADDR')
def _get_host(ip):
"""Look-up the host name by its IP.