Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tox = "*"
mock = "*"
django = ">=3.0.7"
six = ">=1.13.0"
geoip2 = ""

[requires]
python_version = ">=3.7"
302 changes: 240 additions & 62 deletions Pipfile.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Django>=1.11
djangorestframework>=3.5
six>=1.14.0
geoip2>=4.1.0

# Test requirements
pytest-django
Expand Down
4 changes: 2 additions & 2 deletions rest_framework_tracking/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class APIRequestLogAdmin(admin.ModelAdmin):
date_hierarchy = 'requested_at'
list_display = ('id', 'requested_at', 'response_ms', 'status_code',
list_display = ('requested_at', 'response_ms', 'status_code',
'user', 'method',
'path', 'remote_addr', 'host',
'query_params')
Expand All @@ -16,7 +16,7 @@ class APIRequestLogAdmin(admin.ModelAdmin):
if getattr(settings, 'DRF_TRACKING_ADMIN_LOG_READONLY', False):
readonly_fields = ('user', 'username_persistent', 'requested_at',
'response_ms', 'path', 'view', 'view_method',
'remote_addr', 'host', 'method', 'query_params',
'remote_addr', 'request_city', 'request_country', 'host', 'method', 'query_params',
'data', 'response', 'errors', 'status_code')


Expand Down
21 changes: 20 additions & 1 deletion rest_framework_tracking/base_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@

from django.db import connection
from django.utils.timezone import now
from django.contrib.gis.geoip2 import GeoIP2
from django.conf import settings


logger = logging.getLogger(__name__)

# create an instance of GeoIP2 if GEOIP_PATH was set in settings.py
if hasattr(settings, 'GEOIP_PATH'):
geo_location = GeoIP2()
else:
geo_location = None


class BaseLoggingMixin(object):
"""Mixin to log requests"""
Expand Down Expand Up @@ -70,10 +78,12 @@ def finalize_response(self, request, response, *args, **kwargs):
rendered_content = response.rendered_content
else:
rendered_content = response.getvalue()

ip_address = self._get_ip_address(request)

self.log.update(
{
"remote_addr": self._get_ip_address(request),
"remote_addr": ip_address,
"view": self._get_view_name(request),
"view_method": self._get_view_method(request),
"path": request.path,
Expand All @@ -91,6 +101,15 @@ def finalize_response(self, request, response, *args, **kwargs):
)
if self._clean_data(request.query_params.dict()) == {}:
self.log.update({"query_params": self.log["data"]})

# update request city and country
if geo_location:
try:
geo_location_data = geo_location.city(ip_address)
self.log.update({"request_city": geo_location_data["city"], "request_country": geo_location_data["country_name"]})
except Exception:
pass

try:
self.handle_log()
except Exception:
Expand Down
2 changes: 2 additions & 0 deletions rest_framework_tracking/base_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class BaseAPIRequestLog(models.Model):
db_index=True,
)
remote_addr = models.GenericIPAddressField()
request_city = models.CharField(max_length=255, null=True, blank=True)
request_country = models.CharField(max_length=255, null=True, blank=True)
host = models.URLField()
method = models.CharField(max_length=10)
query_params = models.TextField(null=True, blank=True)
Expand Down
23 changes: 23 additions & 0 deletions rest_framework_tracking/migrations/0011_auto_20201123_1321.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.1.3 on 2020-11-23 13:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('rest_framework_tracking', '0010_auto_20200609_1404'),
]

operations = [
migrations.AddField(
model_name='apirequestlog',
name='request_city',
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AddField(
model_name='apirequestlog',
name='request_country',
field=models.CharField(blank=True, max_length=255, null=True),
),
]