From 786045acc661d35989ea6f5a873b595edb183e81 Mon Sep 17 00:00:00 2001 From: Breno Teixeira Date: Fri, 7 Feb 2014 15:45:18 -0200 Subject: [PATCH] =?UTF-8?q?Adi=C3=A7=C3=A3o=20dos=20filters=20da=20app=20c?= =?UTF-8?q?ontato?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/contatos/filters.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 sigi/apps/contatos/filters.py diff --git a/sigi/apps/contatos/filters.py b/sigi/apps/contatos/filters.py new file mode 100644 index 0000000..a2c674b --- /dev/null +++ b/sigi/apps/contatos/filters.py @@ -0,0 +1,45 @@ +# coding: utf-8 +from django.contrib import admin + + +class PopulationFilter(admin.SimpleListFilter): + # Human-readable title which will be displayed in the + # right admin sidebar just above the filter options. + title = 'População' + + # Parameter for the filter that will be used in the URL query. + parameter_name = 'faixa' + + def lookups(self, request, model_admin): + """ + Returns a list of tuples. The first element in each + tuple is the coded value for the option that will + appear in the URL query. The second element is the + human-readable name for the option that will appear + in the right sidebar. + """ + return ( + ( '1', '< 100 Mil'), + ( '2', '100 Mil a 1 Milhão'), + ( '3', '1 Milhão a 100 Milhões'), + ( '4', '> 100 Milhões'), + ) + + def queryset(self, request, queryset): + """ + Returns the filtered queryset based on the value + provided in the query string and retrievable via + `self.value()`. + """ + # Compare the requested value (either '1', '2', '3' or '4') + # to decide how to filter the queryset. + if self.value() == '1': + return queryset.filter(populacao__lt=100000) + elif self.value() == '2': + return queryset.filter(populacao__gte=100000, populacao__lt=1000000) + elif self.value() == '3': + return queryset.filter(populacao__gte=1000000, populacao__lt=10000000) + elif self.value() == '4': + return queryset.filter(populacao__gt=100000000) + else: + return queryset