Skip to content

Commit

Permalink
Implemented auto-increment nr_times_visited
Browse files Browse the repository at this point in the history
  • Loading branch information
alextreme committed Nov 10, 2021
1 parent bb1fd3e commit b97f69d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 8 additions & 0 deletions regex_redirects/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def __init__(self, *args, **kwargs):
)
super(RedirectFallbackMiddleware, self).__init__(*args, **kwargs)

def increment_redirect(self, pk):
redirect = Redirect.objects.get(pk=pk)
redirect.nr_times_visited += 1
redirect.save()

def process_response(self, request, response):
if response.status_code != 404:
return response # No need to check for a redirect for non-404 responses.
Expand All @@ -55,6 +60,7 @@ def process_response(self, request, response):
for redirect in redirects:
# Attempt a regular match
if redirect['old_path'] == full_path:
self.increment_redirect(redirect['id'])
if redirect['new_path'].startswith('http'):
return http.HttpResponsePermanentRedirect(redirect['new_path'])
else:
Expand All @@ -66,6 +72,7 @@ def process_response(self, request, response):
slashed_full_path = full_path[:path_len] + '/' + full_path[path_len:]

if redirect['old_path'] == slashed_full_path:
self.increment_redirect(redirect['id'])
if redirect['new_path'].startswith('http'):
return http.HttpResponsePermanentRedirect(redirect['new_path'])
else:
Expand All @@ -85,6 +92,7 @@ def process_response(self, request, response):
continue

if re.match(redirect['old_path'], full_path):
self.increment_redirect(redirect['id'])
# Convert $1 into \1 (otherwise users would have to enter \1 via the admin
# which would have to be escaped)
new_path = redirect['new_path'].replace('$', '\\')
Expand Down
14 changes: 11 additions & 3 deletions regex_redirects/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@ def test_model(self):
self.assertEqual(six.text_type(r1), "/initial ---> /new_target")

def test_redirect(self):
Redirect.objects.create(
redirect = Redirect.objects.create(
old_path='/initial', new_path='/new_target')
self.assertEqual(redirect.nr_times_visited, 0)
response = self.client.get('/initial')
self.assertRedirects(response,
'/new_target', status_code=301, target_status_code=404)
redirect.refresh_from_db()
self.assertEqual(redirect.nr_times_visited, 1)

@override_settings(APPEND_SLASH=True)
def test_redirect_with_append_slash(self):
Redirect.objects.create(
redirect = Redirect.objects.create(
old_path='/initial/', new_path='/new_target/')
self.assertEqual(redirect.nr_times_visited, 0)
response = self.client.get('/initial')
self.assertRedirects(response,
'/new_target/', status_code=301, target_status_code=404)
redirect.refresh_from_db()
self.assertEqual(redirect.nr_times_visited, 1)

@override_settings(APPEND_SLASH=True)
def test_redirect_with_append_slash_and_query_string(self):
Expand All @@ -47,7 +53,7 @@ def test_redirect_with_append_slash_and_query_string(self):
'/new_target/', status_code=301, target_status_code=404)

def test_regular_expression(self):
Redirect.objects.create(
redirect = Redirect.objects.create(
old_path='/news/index/(\d+)/(.*)/',
new_path='/my/news/$2/',
regular_expression=True)
Expand All @@ -56,6 +62,8 @@ def test_regular_expression(self):
'/my/news/foobar/',
status_code=301, target_status_code=404)
redirect = Redirect.objects.get(regular_expression=True)
redirect.refresh_from_db()
self.assertEqual(redirect.nr_times_visited, 1)

def test_fallback_redirects(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='django-regex-redirects',
version='0.2.0',
version='0.3.1',
author=u'Alex de Landgraaf',
author_email='[email protected]',
packages=find_packages(),
Expand Down

0 comments on commit b97f69d

Please sign in to comment.