Browse Source

Reorganizando e corrigindo filtros personalizados

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

61
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,58 +80,31 @@ 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))
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),
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'convenio_uf_filter', False),
ConvenioUFFilterSpec))
class RangeValuesFilterSpec(FilterSpec):
"""
Author: Willie Gollino (wgollino@yahoo.com)

Loading…
Cancel
Save