Browse Source

Reorganizando e corrigindo filtros personalizados

0.3
Camilo Carlos (Estagiario) 15 years ago
parent
commit
086547e4fa
  1. 65
      sigi/admin/filterspecs.py

65
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.safestring import mark_safe
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from sigi.apps.contatos.models import UnidadeFederativa from sigi.apps.contatos.models import UnidadeFederativa
from abc import ABCMeta
class AlphabeticFilterSpec(ChoicesFilterSpec): class AlphabeticFilterSpec(ChoicesFilterSpec):
""" """
@ -39,24 +40,14 @@ class AlphabeticFilterSpec(ChoicesFilterSpec):
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'alphabetic_filter', False), FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'alphabetic_filter', False),
AlphabeticFilterSpec)) AlphabeticFilterSpec))
class MunicipioUFFilterSpec(ChoicesFilterSpec): class UFFilterSpec(ChoicesFilterSpec):
""" """
Usage: This is an abstract class and customs filters by 'Uf' have to extend this class.
my_municipio_field.uf_filter = True
On Django 1.3 you will can specify a lookup on admin filters. Example:
list_filter = ('municipio__uf',)
""" """
__metaclass__ = ABCMeta
def __init__(self, f, request, params, model, model_admin): def __init__(self, f, request, params, model, model_admin):
super(MunicipioUFFilterSpec, self).__init__(f, request, params, model, super(UFFilterSpec, self).__init__(f, request, params, model,
model_admin) model_admin)
self.lookup_kwarg = '%s__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): def choices(self, cl):
yield {'selected': self.lookup_val is None, yield {'selected': self.lookup_val is None,
@ -70,15 +61,11 @@ class MunicipioUFFilterSpec(ChoicesFilterSpec):
return _('UF') % \ return _('UF') % \
{'field_name': self.field.verbose_name} {'field_name': self.field.verbose_name}
# registering the filter class MunicipioUFFilterSpec(UFFilterSpec):
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'uf_filter', False),
MunicipioUFFilterSpec))
class ConvenioUFFilterSpec(ChoicesFilterSpec):
""" """
Usage: Usage:
municipio.uf_filter = True my_municipio_field.uf_filter = True
On Django 1.3 you will can specify a lookup on admin filters. Example: On Django 1.3 you will can specify a lookup on admin filters. Example:
@ -87,28 +74,36 @@ class ConvenioUFFilterSpec(ChoicesFilterSpec):
""" """
def __init__(self, f, request, params, model, model_admin): def __init__(self, f, request, params, model, model_admin):
super(ConvenioUFFilterSpec, self).__init__(f, request, params, model, super(MunicipioUFFilterSpec, self).__init__(f, request, params, model,
model_admin) model_admin)
self.lookup_kwarg = '%s__municipio__uf__codigo_ibge__exact' % f.name self.lookup_kwarg = '%s__uf__codigo_ibge__exact' % f.name
self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val = request.GET.get(self.lookup_kwarg, None)
self.lookup_choices = UnidadeFederativa.objects.all().order_by('nome') 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 # registering the filter
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'uf_filter', False), FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'uf_filter', False),
ConvenioUFFilterSpec)) MunicipioUFFilterSpec))
class ConvenioUFFilterSpec(UFFilterSpec):
"""
Usage:
my_casa_legislativa_field.convenio_uf_filter = True
On Django 1.3 you will can specify a lookup on admin filters. Example:
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)
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')
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'convenio_uf_filter', False),
ConvenioUFFilterSpec))
class RangeValuesFilterSpec(FilterSpec): class RangeValuesFilterSpec(FilterSpec):
""" """

Loading…
Cancel
Save