Skip to content

Commit

Permalink
added method filter to filter requests by http verb (jazzband#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbradburn authored and moagstar committed Dec 8, 2017
1 parent 6cf10ac commit 566ed79
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
13 changes: 12 additions & 1 deletion project/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
TimeSpentOnQueriesFilter,
OverallTimeFilter,
StatusCodeFilter,
MethodFilter,
)
from .test_lib.mock_suite import MockSuite
from .util import delete_all_models
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions silk/request_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
16 changes: 16 additions & 0 deletions silk/templates/silk/requests.html
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,22 @@ <h4>View</h4>
</label>
</div>

<div>
<input form="filter-form2"
class="typ"
type="hidden"
value="MethodFilter"
name="filter-method-typ">
<label>Method:
<select form="filter-form2" name="filter-method-value">
<option value="" {% if not filters.method.value %}selected{% endif %}></option>
{% for method in options_methods %}
<option {% if method == filters.method.value %}selected{% endif %}>{{ method }}</option>
{% endfor %}
</select>
</label>
</div>


</div>
{% endblock %}
Expand Down
4 changes: 4 additions & 0 deletions silk/views/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 566ed79

Please sign in to comment.