diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index e8acf2db..21e55530 100644 Binary files a/osinaweb/db.sqlite3 and b/osinaweb/db.sqlite3 differ diff --git a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc index 0641f80b..06d0baea 100644 Binary files a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc and b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc differ diff --git a/osinaweb/osichat/__pycache__/models.cpython-310.pyc b/osinaweb/osichat/__pycache__/models.cpython-310.pyc index 1d729d5c..941f5ce0 100644 Binary files a/osinaweb/osichat/__pycache__/models.cpython-310.pyc and b/osinaweb/osichat/__pycache__/models.cpython-310.pyc differ diff --git a/osinaweb/osichat/consumers.py b/osinaweb/osichat/consumers.py index 07b7edf2..a8c18020 100644 --- a/osinaweb/osichat/consumers.py +++ b/osinaweb/osichat/consumers.py @@ -4,11 +4,17 @@ import json from django.template.loader import render_to_string from asgiref.sync import async_to_sync from django.shortcuts import get_object_or_404 +import requests class OsitcomVisitor(WebsocketConsumer): def connect(self): self.accept() + self.visitor_ip = self.scope.get('client', [''])[0] + if self.visitor_ip: + response = requests.get(f'http://ipinfo.io/{self.visitor_ip}/json') + data = response.json() + self.visitor_country = data.get('country', 'Unknown') async_to_sync(self.channel_layer.group_add)( 'ositcom_visitors', self.channel_name ) @@ -28,9 +34,9 @@ class OsitcomVisitor(WebsocketConsumer): event = { 'type': 'new_visitor_handler', 'session_id': text_data_json.get('session_id'), - 'ip_address': text_data_json.get('ip_address'), - 'country': text_data_json.get('country'), + 'referrer': text_data_json.get('referrer'), 'url': text_data_json.get('url'), + } async_to_sync(self.channel_layer.group_send)( 'ositcom_visitors', event @@ -39,8 +45,9 @@ class OsitcomVisitor(WebsocketConsumer): def new_visitor_handler(self, event): visitor = Visitor.objects.create( session_id = event['session_id'], - ip_address = event['ip_address'], - country = event['country'], + ip_address = self.visitor_ip, + country = self.visitor_country, + referrer = event['referrer'], url = event['url'], visit_date = datetime.now(), ) diff --git a/osinaweb/osichat/migrations/0013_rename_reference_visitor_referrer.py b/osinaweb/osichat/migrations/0013_rename_reference_visitor_referrer.py new file mode 100644 index 00000000..e2e6ae48 --- /dev/null +++ b/osinaweb/osichat/migrations/0013_rename_reference_visitor_referrer.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.5 on 2024-07-21 19:58 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('osichat', '0012_alter_visitor_reference'), + ] + + operations = [ + migrations.RenameField( + model_name='visitor', + old_name='reference', + new_name='referrer', + ), + ] diff --git a/osinaweb/osichat/migrations/__pycache__/0013_rename_reference_visitor_referrer.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0013_rename_reference_visitor_referrer.cpython-310.pyc new file mode 100644 index 00000000..0413ec8b Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0013_rename_reference_visitor_referrer.cpython-310.pyc differ diff --git a/osinaweb/osichat/models.py b/osinaweb/osichat/models.py index b06dc644..f9dfd90a 100644 --- a/osinaweb/osichat/models.py +++ b/osinaweb/osichat/models.py @@ -7,7 +7,7 @@ class Visitor(models.Model): ip_address = models.CharField(max_length=300) country = models.CharField(max_length=15, null=True) url = models.URLField() - reference = models.URLField(null=True, blank=True) + referrer = models.URLField(null=True, blank=True) visit_date = models.DateTimeField(null=True) left_date = models.DateTimeField(null=True) diff --git a/osinaweb/static/js/osichat/visitors.js b/osinaweb/static/js/osichat/visitors.js index eb4dc199..0b2c2732 100644 --- a/osinaweb/static/js/osichat/visitors.js +++ b/osinaweb/static/js/osichat/visitors.js @@ -1,10 +1,9 @@ const visitors_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws"; -const my_domain = "192.168.1.111:8000"; +const my_domain = "osina.ositcom.com"; const session_id = document.getElementById('session_id').textContent.trim(); -const client_ip = document.getElementById('client_ip').textContent.trim(); -const client_country = document.getElementById('client_country').textContent.trim(); const current_url = document.getElementById('current_url').textContent.trim(); +const referrer = document.referrer; const visitorsSocketUrl = `${chat_ws_scheme}://${domain}/ws/osichat/visitors/`; const visitorsSocket = new WebSocket(visitorsSocketUrl); @@ -14,9 +13,8 @@ visitorsSocket.onopen = () => { const event_message = { 'event_type': 'new_visitor', 'session_id': session_id, - 'ip_address': client_ip, - 'country': client_country, - 'url': current_url + 'referrer': referrer, + 'url': current_url, }; visitorsSocket.send(JSON.stringify(event_message)); };