Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ tox = "*"
mock = "*"
django = ">=3.0.7"
six = ">=1.13.0"
geoip2 = ""
maxminddb = "*"

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

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ drf-api-tracking provides a Django model and DRF view mixin that work together t
`view` | Target VIEW of the request, e.g., `"views.api.ApiView"` | CharField
`view_method` | Target METHOD of the VIEW of the request, e.g., `"get"` | CharField
`remote_addr` | IP address where the request originated (X_FORWARDED_FOR if available, REMOTE_ADDR if not), e.g., `"127.0.0.1"` | GenericIPAddressField
`request_city` | City where the reuqest originated | CharField
`request_country` | Country where the reuqest originated | CharField
`host` | Originating host of the request, e.g., `"example.com"` | URLField
`method` | HTTP method, e.g., `"GET"` | CharField
`query_params` | Dictionary of request query parameters, as text | TextField
Expand Down
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
2 changes: 1 addition & 1 deletion rest_framework_tracking/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class APIRequestLogAdmin(admin.ModelAdmin):
if app_settings.ADMIN_LOG_READONLY:
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')

def changelist_view(self, request, extra_context=None):
Expand Down
35 changes: 32 additions & 3 deletions rest_framework_tracking/base_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

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


from .app_settings import app_settings

logger = logging.getLogger(__name__)

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


class BaseLoggingMixin(object):
"""Mixin to log requests"""
Expand Down Expand Up @@ -74,10 +80,17 @@ 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)

# update request city and country
request_location_dict = {}
if geo_location:
request_location_dict = self._get_request_location(ip_address)

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": self._get_path(request),
Expand All @@ -91,10 +104,13 @@ def finalize_response(self, request, response, *args, **kwargs):
"response_ms": self._get_response_ms(),
"response": self._clean_data(rendered_content),
"status_code": response.status_code,
**request_location_dict
}
)
if self._clean_data(request.query_params.dict()) == {}:
self.log.update({"query_params": self.log["data"]})


try:
self.handle_log()
except Exception:
Expand Down Expand Up @@ -154,7 +170,7 @@ def _get_view_name(self, request):
def _get_view_method(self, request):
"""Get view method."""
if hasattr(self, "action"):
return self.action if self.action else None
return self.action or None
return request.method.lower()

def _get_user(self, request):
Expand All @@ -173,6 +189,19 @@ def _get_response_ms(self):
response_ms = int(response_timedelta.total_seconds() * 1000)
return max(response_ms, 0)

def _get_request_location(self, ip_address):
try:
geo_location_data = geo_location.city(ip_address)
request_city = geo_location_data["city"]
request_country = geo_location_data["country_name"]

return {
"request_city": request_city,
"request_country": request_country,
}
except Exception:
return {}

def should_log(self, request, response):
"""
Method that should return a value that evaluated to True if the request should be logged.
Expand Down Expand Up @@ -221,7 +250,7 @@ def _clean_data(self, data):
value = ast.literal_eval(value)
except (ValueError, SyntaxError):
pass
if isinstance(value, list) or isinstance(value, dict):
if isinstance(value, (list, dict)):
data[key] = self._clean_data(value)
if key.lower() in SENSITIVE_FIELDS:
data[key] = self.CLEANED_SUBSTITUTE
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),
),
]
15 changes: 15 additions & 0 deletions tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,21 @@ def test_custom_log_handler(self):
self.client.post('/custom-log-handler')
self.assertEqual(APIRequestLog.objects.all().count(), 1)

def test_log_request_city(self):
request = APIRequestFactory().get('/logging')
request.META['REMOTE_ADDR'] = '127.0.0.9'

MockLoggingView.as_view()(request).render()
log = APIRequestLog.objects.first()
self.assertEqual(log.request_city, None)

def test_log_request_country(self):
request = APIRequestFactory().get('/logging')
request.META['REMOTE_ADDR'] = '127.0.0.9'

MockLoggingView.as_view()(request).render()
log = APIRequestLog.objects.first()
self.assertEqual(log.request_country, None)
@override_settings(DATA_UPLOAD_MAX_MEMORY_SIZE=1)
def test_decode_request_body_setting(self):
content_type = "multipart/form-data; boundary=_"
Expand Down