From d87f2bc7b67c89bf556dfbadf1fb48aa0dc2ca6c Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 8 Apr 2016 10:37:23 -0400 Subject: [PATCH] OrderingFilter adjustements (#3983) * Made sure the OrderingFilter relies on Field.verbose_name. * Marked OrderingFilter's order labels for translation. --- rest_framework/filters.py | 7 +++---- tests/test_filters.py | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/rest_framework/filters.py b/rest_framework/filters.py index a4542df10..2a25378a0 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -240,8 +240,7 @@ class OrderingFilter(BaseFilterBackend): elif valid_fields == '__all__': # View explicitly allows filtering on any model field valid_fields = [ - (field.name, getattr(field, 'label', field.name.title())) - for field in queryset.model._meta.fields + (field.name, field.verbose_name) for field in queryset.model._meta.fields ] valid_fields += [ (key, key.title().split('__')) @@ -272,8 +271,8 @@ class OrderingFilter(BaseFilterBackend): current = None if current is None else current[0] options = [] for key, label in self.get_valid_fields(queryset, view): - options.append((key, '%s - ascending' % label)) - options.append(('-' + key, '%s - descending' % label)) + options.append((key, '%s - %s' % (label, _('ascending')))) + options.append(('-' + key, '%s - %s' % (label, _('descending')))) return { 'request': request, 'current': current, diff --git a/tests/test_filters.py b/tests/test_filters.py index 729a7b75b..b72d95691 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -499,7 +499,7 @@ class SearchFilterM2MTests(TestCase): class OrderingFilterModel(models.Model): - title = models.CharField(max_length=20) + title = models.CharField(max_length=20, verbose_name='verbose title') text = models.CharField(max_length=100) @@ -741,6 +741,19 @@ class OrderingFilterTests(TestCase): reload_module(filters) + def test_get_template_context(self): + class OrderingListView(generics.ListAPIView): + ordering_fields = '__all__' + serializer_class = OrderingFilterSerializer + queryset = OrderingFilterModel.objects.all() + filter_backends = (filters.OrderingFilter,) + + request = factory.get('/', {'ordering': 'title'}, HTTP_ACCEPT='text/html') + view = OrderingListView.as_view() + response = view(request) + + self.assertContains(response, 'verbose title') + class SensitiveOrderingFilterModel(models.Model): username = models.CharField(max_length=20)