From 086547e4fa46da21dbdb61c77c5453ca8107fcb3 Mon Sep 17 00:00:00 2001 From: "Camilo Carlos (Estagiario)" Date: Fri, 11 Jun 2010 15:35:52 +0000 Subject: [PATCH] Reorganizando e corrigindo filtros personalizados --- sigi/admin/filterspecs.py | 65 ++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/sigi/admin/filterspecs.py b/sigi/admin/filterspecs.py index 74a67d7..57608ae 100644 --- a/sigi/admin/filterspecs.py +++ b/sigi/admin/filterspecs.py @@ -3,6 +3,7 @@ from django.utils.encoding import smart_unicode from django.utils.safestring import mark_safe from django.utils.translation import ugettext as _ from sigi.apps.contatos.models import UnidadeFederativa +from abc import ABCMeta class AlphabeticFilterSpec(ChoicesFilterSpec): """ @@ -39,7 +40,28 @@ class AlphabeticFilterSpec(ChoicesFilterSpec): FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'alphabetic_filter', False), AlphabeticFilterSpec)) -class MunicipioUFFilterSpec(ChoicesFilterSpec): +class UFFilterSpec(ChoicesFilterSpec): + """ + This is an abstract class and customs filters by 'Uf' have to extend this class. + """ + __metaclass__ = ABCMeta + def __init__(self, f, request, params, model, model_admin): + super(UFFilterSpec, self).__init__(f, request, params, model, + model_admin) + + def choices(self, cl): + yield {'selected': self.lookup_val is None, + 'query_string': cl.get_query_string({}, [self.lookup_kwarg]), + 'display': _('All')} + for val in self.lookup_choices: + yield {'selected': smart_unicode(val.codigo_ibge) == self.lookup_val, + 'query_string': cl.get_query_string({self.lookup_kwarg: val.codigo_ibge}), + 'display': val.nome} + def title(self): + return _('UF') % \ + {'field_name': self.field.verbose_name} + +class MunicipioUFFilterSpec(UFFilterSpec): """ Usage: @@ -58,57 +80,30 @@ class MunicipioUFFilterSpec(ChoicesFilterSpec): self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_choices = UnidadeFederativa.objects.all().order_by('nome') - def choices(self, cl): - yield {'selected': self.lookup_val is None, - 'query_string': cl.get_query_string({}, [self.lookup_kwarg]), - 'display': _('All')} - for val in self.lookup_choices: - yield {'selected': smart_unicode(val.codigo_ibge) == self.lookup_val, - 'query_string': cl.get_query_string({self.lookup_kwarg: val.codigo_ibge}), - 'display': val.nome} - def title(self): - return _('UF') % \ - {'field_name': self.field.verbose_name} # registering the filter FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'uf_filter', False), - MunicipioUFFilterSpec)) + MunicipioUFFilterSpec)) -class ConvenioUFFilterSpec(ChoicesFilterSpec): +class ConvenioUFFilterSpec(UFFilterSpec): """ Usage: - municipio.uf_filter = True + my_casa_legislativa_field.convenio_uf_filter = True On Django 1.3 you will can specify a lookup on admin filters. Example: - list_filter = ('municipio__uf',) + list_filter = ('casa_legislativa__municipio__uf',) """ - def __init__(self, f, request, params, model, model_admin): - super(ConvenioUFFilterSpec, self).__init__(f, request, params, model, - model_admin) + super(ConvenioUFFilterSpec, self).__init__(f, request, params, model, model_admin) self.lookup_kwarg = '%s__municipio__uf__codigo_ibge__exact' % f.name self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_choices = UnidadeFederativa.objects.all().order_by('nome') - def choices(self, cl): - yield {'selected': self.lookup_val is None, - 'query_string': cl.get_query_string({}, [self.lookup_kwarg]), - 'display': _('All')} - for val in self.lookup_choices: - yield {'selected': smart_unicode(val.codigo_ibge) == self.lookup_val, - 'query_string': cl.get_query_string({self.lookup_kwarg: val.codigo_ibge}), - 'display': val.nome} - def title(self): - return _('UF') % \ - {'field_name': self.field.verbose_name} - -# registering the filter -FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'uf_filter', False), - ConvenioUFFilterSpec)) - +FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'convenio_uf_filter', False), +ConvenioUFFilterSpec)) class RangeValuesFilterSpec(FilterSpec): """