added geo support to track_user view
This commit is contained in:
@@ -65,7 +65,7 @@ class TrackUserViewTest(APITestCase):
|
|||||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
|
|
||||||
def test_anon_user_can_register_product_action(self):
|
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
|
# Create instance
|
||||||
product = ProductFactory()
|
product = ProductFactory()
|
||||||
@@ -85,7 +85,7 @@ class TrackUserViewTest(APITestCase):
|
|||||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
|
|
||||||
def test_anon_user_can_register_company_action(self):
|
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
|
# Create instance
|
||||||
company = CompanyFactory()
|
company = CompanyFactory()
|
||||||
@@ -104,6 +104,27 @@ class TrackUserViewTest(APITestCase):
|
|||||||
# Assert forbidden code
|
# Assert forbidden code
|
||||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
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
|
# authenticated user
|
||||||
def test_auth_user_can_only_post(self):
|
def test_auth_user_can_only_post(self):
|
||||||
"""Regular logged-in user can list instance
|
"""Regular logged-in user can list instance
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ from rest_framework.response import Response
|
|||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.permissions import AllowAny
|
from rest_framework.permissions import AllowAny
|
||||||
|
|
||||||
from ipware import get_client_ip
|
|
||||||
from django.contrib.gis.geoip2 import GeoIP2
|
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
|
from back_latienda.permissions import IsStaff
|
||||||
|
|
||||||
@@ -44,7 +46,7 @@ def track_user(request):
|
|||||||
id: 1,
|
id: 1,
|
||||||
},
|
},
|
||||||
'ip': '2134.234.234.2134',
|
'ip': '2134.234.234.2134',
|
||||||
'geo': (latitude: 324.32., longitud: 32423.23)
|
'geo': (324.32, 32423.23)
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
@@ -56,7 +58,7 @@ def track_user(request):
|
|||||||
'user': None if request.user.is_anonymous else request.user,
|
'user': None if request.user.is_anonymous else request.user,
|
||||||
'anonymous': request.user.is_anonymous,
|
'anonymous': request.user.is_anonymous,
|
||||||
'ip_address': data.get('ip'),
|
'ip_address': data.get('ip'),
|
||||||
'geo': data.get('geo'),
|
'geo': Point(data.get('geo')),
|
||||||
}
|
}
|
||||||
|
|
||||||
if data['action_object'].get('model') == 'product':
|
if data['action_object'].get('model') == 'product':
|
||||||
@@ -73,7 +75,5 @@ def track_user(request):
|
|||||||
new_stat = StatsLog.objects.create(**instance_data)
|
new_stat = StatsLog.objects.create(**instance_data)
|
||||||
return Response(status=status.HTTP_201_CREATED)
|
return Response(status=status.HTTP_201_CREATED)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
logging.error(f"Stats could not be created: {str(e)}")
|
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)
|
return Response(f"Process could not be registered: {str(type(e))}", status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||||
|
|||||||
Reference in New Issue
Block a user