From cf973caf5a930be3750dbdc088fcb86d80300ffa Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 3 Mar 2021 13:45:16 +0000 Subject: [PATCH] added geo support to track_user view --- stats/tests.py | 25 +++++++++++++++++++++++-- stats/views.py | 10 +++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/stats/tests.py b/stats/tests.py index bd78892..3bfe3ce 100644 --- a/stats/tests.py +++ b/stats/tests.py @@ -65,7 +65,7 @@ class TrackUserViewTest(APITestCase): self.assertEqual(response.status_code, status.HTTP_201_CREATED) def test_anon_user_can_register_product_action(self): - """Not logged-in user cannot modify existing instance + """Not logged-in user can register product action """ # Create instance product = ProductFactory() @@ -85,7 +85,7 @@ class TrackUserViewTest(APITestCase): self.assertEqual(response.status_code, status.HTTP_201_CREATED) def test_anon_user_can_register_company_action(self): - """Not logged-in user cannot modify existing instance + """Not logged-in user can register company action """ # Create instance company = CompanyFactory() @@ -104,6 +104,27 @@ class TrackUserViewTest(APITestCase): # Assert forbidden code self.assertEqual(response.status_code, status.HTTP_201_CREATED) + def test_anon_user_can_register_company_with_coordinates(self): + """Not logged-in user can register action with coordinates + """ + # Create instance + company = CompanyFactory() + + data = { + 'action': 'VIEW', + 'action_object': { + 'model': 'company', + 'id': company.id, + }, + 'geo': (12.2, -0.545) + } + + # Query endpoint + response = self.client.post(self.endpoint, data=data, format='json') + + # Assert forbidden code + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + # authenticated user def test_auth_user_can_only_post(self): """Regular logged-in user can list instance diff --git a/stats/views.py b/stats/views.py index d5b5e45..778d62e 100644 --- a/stats/views.py +++ b/stats/views.py @@ -8,8 +8,10 @@ from rest_framework.response import Response from rest_framework import status from rest_framework.permissions import AllowAny -from ipware import get_client_ip from django.contrib.gis.geoip2 import GeoIP2 +from django.contrib.gis.geos import Point + +from ipware import get_client_ip from back_latienda.permissions import IsStaff @@ -44,7 +46,7 @@ def track_user(request): id: 1, }, 'ip': '2134.234.234.2134', - 'geo': (latitude: 324.32., longitud: 32423.23) + 'geo': (324.32, 32423.23) } """ try: @@ -56,7 +58,7 @@ def track_user(request): 'user': None if request.user.is_anonymous else request.user, 'anonymous': request.user.is_anonymous, 'ip_address': data.get('ip'), - 'geo': data.get('geo'), + 'geo': Point(data.get('geo')), } if data['action_object'].get('model') == 'product': @@ -73,7 +75,5 @@ def track_user(request): new_stat = StatsLog.objects.create(**instance_data) return Response(status=status.HTTP_201_CREATED) except Exception as e: - import ipdb; ipdb.set_trace() - logging.error(f"Stats could not be created: {str(e)}") return Response(f"Process could not be registered: {str(type(e))}", status=status.HTTP_406_NOT_ACCEPTABLE)