Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added logging request city and request country using geoip2 #38

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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 @@ -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
32 changes: 31 additions & 1 deletion rest_framework_tracking/base_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +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 @@ -71,10 +78,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 @@ -88,10 +102,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 @@ -173,6 +190,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
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),
),
]
16 changes: 16 additions & 0 deletions tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,19 @@ def test_custom_log_handler(self):
self.client.get('/custom-log-handler')
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)