Skip to content

Commit bce477d

Browse files
xrmxavelis
authored andcommitted
Sort view filters values
So that they are usable. We can remove the empty string path as we already have it in the template. While at it move all the calculation to the database for filtering and flattening the data.
1 parent a47f13c commit bce477d

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

project/tests/test_view_requests.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def test_path(self):
1414
paths = RequestsView()._get_paths()
1515
for r in requests:
1616
self.assertIn(r.path, paths)
17-
self.assertIn('', paths)
1817

1918
def test_show(self):
2019
self.assertIn(RequestsView.default_show, RequestsView.show)
@@ -35,8 +34,8 @@ def test_default(self):
3534
'options_show': RequestsView.show,
3635
'options_order_by': RequestsView().options_order_by,
3736
'options_order_dir': RequestsView().options_order_dir,
38-
'options_paths': RequestsView()._get_paths()
3937
}, context)
38+
self.assertQuerysetEqual(context['options_paths'], RequestsView()._get_paths())
4039
self.assertNotIn('path', context)
4140
self.assertIn('results', context)
4241

@@ -57,8 +56,8 @@ def test_get(self):
5756
'options_show': RequestsView.show,
5857
'options_order_by': RequestsView().options_order_by,
5958
'options_order_dir': RequestsView().options_order_dir,
60-
'options_paths': RequestsView()._get_paths()
6159
}, context)
60+
self.assertQuerysetEqual(context['options_paths'], RequestsView()._get_paths())
6261
self.assertIn('results', context)
6362

6463

silk/views/requests.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,38 @@ def options_order_dir(self):
6060
return [{'value': x, 'label': self.order_dir[x]['label']} for x in self.order_dir.keys()]
6161

6262
def _get_paths(self):
63-
return [''] + [x['path'] for x in Request.objects.values('path').distinct()]
63+
return Request.objects.values_list(
64+
'path',
65+
flat=True
66+
).order_by(
67+
'path'
68+
).distinct()
69+
70+
def _get_views(self):
71+
return Request.objects.values_list(
72+
'view_name',
73+
flat=True
74+
).exclude(
75+
view_name=''
76+
).order_by(
77+
'view_name'
78+
).distinct()
6479

6580
def _get_status_codes(self):
66-
return [x['status_code'] for x in Response.objects.values('status_code').distinct()]
81+
return Response.objects.values_list(
82+
'status_code',
83+
flat=True
84+
).order_by(
85+
'status_code'
86+
).distinct()
6787

6888
def _get_methods(self):
69-
return [x['method'] for x in Request.objects.values('method').distinct()]
89+
return Request.objects.values_list(
90+
'method',
91+
flat=True
92+
).order_by(
93+
'method'
94+
).distinct()
7095

7196
def _get_objects(self, show=None, order_by=None, order_dir=None, path=None, filters=None):
7297
if not filters:
@@ -110,7 +135,7 @@ def _create_context(self, request):
110135
'options_paths': self._get_paths(),
111136
'options_status_codes': self._get_status_codes(),
112137
'options_methods': self._get_methods(),
113-
'view_names': [x[0] for x in Request.objects.values_list('view_name').distinct() if x[0]],
138+
'view_names': self._get_views(),
114139
'filters': raw_filters
115140
}
116141
context.update(csrf(request))

0 commit comments

Comments
 (0)