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,7 +40,28 @@ 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):
"""
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: Usage:
@ -58,57 +80,30 @@ class MunicipioUFFilterSpec(ChoicesFilterSpec):
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),
MunicipioUFFilterSpec)) MunicipioUFFilterSpec))
class ConvenioUFFilterSpec(ChoicesFilterSpec): class ConvenioUFFilterSpec(UFFilterSpec):
""" """
Usage: 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: 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): def __init__(self, f, request, params, model, model_admin):
super(ConvenioUFFilterSpec, self).__init__(f, request, params, model, super(ConvenioUFFilterSpec, 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__municipio__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): FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'convenio_uf_filter', False),
yield {'selected': self.lookup_val is None, ConvenioUFFilterSpec))
'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))
class RangeValuesFilterSpec(FilterSpec): class RangeValuesFilterSpec(FilterSpec):
""" """

Loading…
Cancel
Save