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

yrchen's work on remote address #15

Merged
merged 5 commits into from
Jan 4, 2016
Merged
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ drf-tracking provides a Django model and DRF view mixin that work together to lo
`requested_at` | Date-time that the request was made | DateTimeField
`response_ms` | Number of milliseconds spent in view code | PositiveIntegerField
`path` | Target URI of the request, e.g., `"/api/"` | CharField
`remote_addr` | IP address where the request originated, e.g., `"127.0.0.1"` | GenericIPAddressField
`remote_addr` | IP address where the request originated (X_FORWARDED_FOR if available, REMOTE_ADDR if not), e.g., `"127.0.0.1"` | GenericIPAddressField
`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
13 changes: 10 additions & 3 deletions rest_framework_tracking/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ def initial(self, request, *args, **kwargs):
except AttributeError: # if already a dict, can't dictify
data_dict = request.data

# save to log without user
# self.request is already present
# get IP
ipaddr = request.META.get("HTTP_X_FORWARDED_FOR", None)
if ipaddr:
# X_FORWARDED_FOR returns client1, proxy1, proxy2,...
ipaddr = ipaddr.split(", ")[0]
else:
ipaddr = request.META.get("REMOTE_ADDR", "")

# save to log
self.request.log = APIRequestLog.objects.create(
requested_at=now(),
path=request.path,
remote_addr=request.META['REMOTE_ADDR'],
remote_addr=ipaddr,
host=request.get_host(),
method=request.method,
query_params=request.query_params.dict(),
Expand Down
10 changes: 9 additions & 1 deletion tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ def test_log_path(self):
log = APIRequestLog.objects.first()
self.assertEqual(log.path, '/logging')

def test_log_ip(self):
def test_log_ip_remote(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.remote_addr, '127.0.0.9')

def test_log_ip_xforwarded(self):
request = APIRequestFactory().get('/logging')
request.META['HTTP_X_FORWARDED_FOR'] = '127.0.0.8'

MockLoggingView.as_view()(request).render()
log = APIRequestLog.objects.first()
self.assertEqual(log.remote_addr, '127.0.0.8')

def test_log_host(self):
self.client.get('/logging')
log = APIRequestLog.objects.first()
Expand Down