Skip to content

Commit

Permalink
Some clean up and better style
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunedan committed Jul 9, 2018
1 parent 8ba04a3 commit 9934f3c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
12 changes: 7 additions & 5 deletions README.rst
Expand Up @@ -166,13 +166,15 @@ locked. For example::

LOCKDOWN_REMOTE_ADDR_EXCEPTIONS
-------------------------------
An optional list addresses to be matched agains incomming
`requests.META['REMOTE_ADDR']`. If IP is in this list, it will not be locked. For example::

LOCKDOWN_REMOTE_ADDR = (
'192.168.0.1',
An optional list of IP-addresses to be matched against the requesting
IP-address (from `requests.META['REMOTE_ADDR']`). If the requesting IP-address
is in this list, it will not be locked. For example::

LOCKDOWN_REMOTE_ADDR_EXCEPTIONS = [
'127.0.0.1',
)
'::1',
]

LOCKDOWN_UNTIL
--------------
Expand Down
9 changes: 5 additions & 4 deletions lockdown/middleware.py
Expand Up @@ -82,15 +82,16 @@ def process_request(self, request):
if settings.ENABLED is False:
return None

# Don't lock down if the client REMOTE_ADDR matched and exception
# Don't lock down if the client REMOTE_ADDR matched and is part of the
# exception list.
if self.remote_addr_exceptions:
remote_addr_exceptions = self.remote_addr_exceptions
else:
remote_addr_exceptions = settings.REMOTE_ADDR_EXCEPTIONS

if remote_addr_exceptions:
if request.META.get('REMOTE_ADDR') in remote_addr_exceptions:
return None
if remote_addr_exceptions and \
request.META.get('REMOTE_ADDR') in remote_addr_exceptions:
return None

# Don't lock down if the URL matches an exception pattern.
if self.url_exceptions:
Expand Down
5 changes: 3 additions & 2 deletions lockdown/tests/tests.py
Expand Up @@ -47,7 +47,7 @@ def test_global_disable(self):
self.assertEqual(response.content, self.locked_contents)

@patch('lockdown.tests.tests.middleware.settings.URL_EXCEPTIONS',
[r'/view/$'])
(r'/view/$',))
def test_url_exceptions(self):
"""Test that a page isn't locked when its URL is in the exception list.
Expand All @@ -65,7 +65,8 @@ def test_remote_addr_exception_lock(self):
The excepted IP are determined by the
LOCKDOWN_REMOTE_ADDR_EXCEPTIONS setting
"""
response = self.client.get(self.locked_url, REMOTE_ADDR='192.168.0.100')
response = self.client.get(self.locked_url,
REMOTE_ADDR='192.168.0.100')
self.assertNotEqual(response.content, self.locked_contents)

@patch('lockdown.tests.tests.middleware.settings.REMOTE_ADDR_EXCEPTIONS',
Expand Down
3 changes: 2 additions & 1 deletion lockdown/tests/views.py
Expand Up @@ -32,8 +32,9 @@ def locked_view_with_exception(request):
return HttpResponse('A locked view.')


@lockdown(remote_addr_exceptions=('192.168.0.1',))
@lockdown(remote_addr_exceptions=['192.168.0.1'])
def locked_view_with_ip_exception(request):
"""View, locked except for the configured IP-address."""
return HttpResponse('A locked view.')


Expand Down

0 comments on commit 9934f3c

Please sign in to comment.