Skip to content

Commit

Permalink
Sort view filters values
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xrmx authored and avelis committed Feb 15, 2019
1 parent a47f13c commit bce477d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
5 changes: 2 additions & 3 deletions project/tests/test_view_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def test_path(self):
paths = RequestsView()._get_paths()
for r in requests:
self.assertIn(r.path, paths)
self.assertIn('', paths)

def test_show(self):
self.assertIn(RequestsView.default_show, RequestsView.show)
Expand All @@ -35,8 +34,8 @@ def test_default(self):
'options_show': RequestsView.show,
'options_order_by': RequestsView().options_order_by,
'options_order_dir': RequestsView().options_order_dir,
'options_paths': RequestsView()._get_paths()
}, context)
self.assertQuerysetEqual(context['options_paths'], RequestsView()._get_paths())
self.assertNotIn('path', context)
self.assertIn('results', context)

Expand All @@ -57,8 +56,8 @@ def test_get(self):
'options_show': RequestsView.show,
'options_order_by': RequestsView().options_order_by,
'options_order_dir': RequestsView().options_order_dir,
'options_paths': RequestsView()._get_paths()
}, context)
self.assertQuerysetEqual(context['options_paths'], RequestsView()._get_paths())
self.assertIn('results', context)


Expand Down
33 changes: 29 additions & 4 deletions silk/views/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,38 @@ def options_order_dir(self):
return [{'value': x, 'label': self.order_dir[x]['label']} for x in self.order_dir.keys()]

def _get_paths(self):
return [''] + [x['path'] for x in Request.objects.values('path').distinct()]
return Request.objects.values_list(
'path',
flat=True
).order_by(
'path'
).distinct()

def _get_views(self):
return Request.objects.values_list(
'view_name',
flat=True
).exclude(
view_name=''
).order_by(
'view_name'
).distinct()

def _get_status_codes(self):
return [x['status_code'] for x in Response.objects.values('status_code').distinct()]
return Response.objects.values_list(
'status_code',
flat=True
).order_by(
'status_code'
).distinct()

def _get_methods(self):
return [x['method'] for x in Request.objects.values('method').distinct()]
return Request.objects.values_list(
'method',
flat=True
).order_by(
'method'
).distinct()

def _get_objects(self, show=None, order_by=None, order_dir=None, path=None, filters=None):
if not filters:
Expand Down Expand Up @@ -110,7 +135,7 @@ def _create_context(self, request):
'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]],
'view_names': self._get_views(),
'filters': raw_filters
}
context.update(csrf(request))
Expand Down

0 comments on commit bce477d

Please sign in to comment.