From 566ed79df5a27137ae7a0d1835c1b33251c97a53 Mon Sep 17 00:00:00 2001 From: danielbradburn Date: Mon, 17 Jul 2017 05:46:43 +0200 Subject: [PATCH] added method filter to filter requests by http verb (#194) --- project/tests/test_filters.py | 13 ++++++++++++- silk/request_filters.py | 4 ++++ silk/templates/silk/requests.html | 16 ++++++++++++++++ silk/views/requests.py | 4 ++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/project/tests/test_filters.py b/project/tests/test_filters.py index 29dd56fb..883dc16d 100644 --- a/project/tests/test_filters.py +++ b/project/tests/test_filters.py @@ -21,6 +21,7 @@ TimeSpentOnQueriesFilter, OverallTimeFilter, StatusCodeFilter, + MethodFilter, ) from .test_lib.mock_suite import MockSuite from .util import delete_all_models @@ -99,7 +100,7 @@ def test_time_spent_filter(self): self.assertGreaterEqual(f.time_taken, c) def test_status_code_filter(self): - requests = [mock_suite.mock_request() for _ in range(0, 10)] + requests = [mock_suite.mock_request() for _ in range(0, 50)] requests = sorted(requests, key=lambda x: x.response.status_code) by_status_code = groupby(requests, key=lambda x: x.response.status_code) for status_code, expected in by_status_code: @@ -109,6 +110,16 @@ def test_status_code_filter(self): filtered = query_set.filter(status_code_filter) self.assertEqual(len(list(expected)), filtered.count()) + def test_method_filter(self): + requests = [mock_suite.mock_request() for _ in range(0, 50)] + requests = sorted(requests, key=lambda x: x.method) + by_method = groupby(requests, key=lambda x: x.method) + for method, expected in by_method: + method_filter = MethodFilter(method) + query_set = models.Request.objects.all() + query_set = method_filter.contribute_to_query_set(query_set) + filtered = query_set.filter(method_filter) + self.assertEqual(len(list(expected)), filtered.count()) class TestRequestAfterDateFilter(TestCase): def assertFilter(self, dt, f): diff --git a/silk/request_filters.py b/silk/request_filters.py index 31885944..0aec072f 100644 --- a/silk/request_filters.py +++ b/silk/request_filters.py @@ -201,6 +201,10 @@ def __init__(self, n): super(StatusCodeFilter, self).__init__(value, response__status_code=n) +class MethodFilter(BaseFilter): + def __init__(self, value): + super(MethodFilter, self).__init__(value, method=value) + def filters_from_request(request): raw_filters = {} diff --git a/silk/templates/silk/requests.html b/silk/templates/silk/requests.html index aa019f36..a7d806d4 100644 --- a/silk/templates/silk/requests.html +++ b/silk/templates/silk/requests.html @@ -220,6 +220,22 @@

View

+
+ + +
+ {% endblock %} diff --git a/silk/views/requests.py b/silk/views/requests.py index 1b43a16e..1a63baca 100644 --- a/silk/views/requests.py +++ b/silk/views/requests.py @@ -70,6 +70,9 @@ def _get_paths(self): def _get_status_codes(self): return [x['status_code'] for x in Response.objects.values('status_code').distinct()] + def _get_methods(selfs): + return [x['method'] for x in Request.objects.values('method').distinct()] + def _get_objects(self, show=None, order_by=None, order_dir=None, path=None, filters=None): if not filters: filters = [] @@ -111,6 +114,7 @@ def _create_context(self, request): 'options_order_dir': self.options_order_dir, 'options_paths': self._get_paths(), 'options_status_codes': self._get_status_codes(), + 'options_methods': self._get_methods(), 'view_names': [x[0] for x in Request.objects.values_list('view_name').distinct() if x[0]], 'filters': raw_filters }