From b97f69d7f151ae05afc1214dc7899b08a7ef2c76 Mon Sep 17 00:00:00 2001 From: Alex de Landgraaf Date: Wed, 10 Nov 2021 12:55:17 +0100 Subject: [PATCH] Implemented auto-increment nr_times_visited --- regex_redirects/middleware.py | 8 ++++++++ regex_redirects/tests.py | 14 +++++++++++--- setup.py | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/regex_redirects/middleware.py b/regex_redirects/middleware.py index bb488d7..3aa66b4 100644 --- a/regex_redirects/middleware.py +++ b/regex_redirects/middleware.py @@ -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. @@ -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: @@ -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: @@ -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('$', '\\') diff --git a/regex_redirects/tests.py b/regex_redirects/tests.py index d9fc5a0..4157a12 100644 --- a/regex_redirects/tests.py +++ b/regex_redirects/tests.py @@ -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): @@ -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) @@ -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): """ diff --git a/setup.py b/setup.py index 7ce7a7f..509dc70 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name='django-regex-redirects', - version='0.2.0', + version='0.3.1', author=u'Alex de Landgraaf', author_email='alex@maykinmedia.nl', packages=find_packages(),