diff --git a/sapl/parlamentares/forms.py b/sapl/parlamentares/forms.py index c7c8c9f9d..c9d5d828d 100755 --- a/sapl/parlamentares/forms.py +++ b/sapl/parlamentares/forms.py @@ -20,6 +20,7 @@ from sapl.utils import FileFieldCheckMixin from sapl.base.models import Autor, TipoAutor from sapl.crispy_layout_mixin import form_actions, to_row from sapl.rules import SAPL_GROUP_VOTANTE +import django_filters from .models import (ComposicaoColigacao, Filiacao, Frente, Legislatura, Mandato, Parlamentar, Votante) @@ -210,6 +211,29 @@ class ParlamentarForm(FileFieldCheckMixin, ModelForm): attrs={'id': 'texto-rico'})} +class ParlamentarFilterSet(django_filters.FilterSet): + nome_parlamentar = django_filters.CharFilter( + label=_('Nome do Parlamentar'), + lookup_expr='icontains') + + class Meta: + model = Parlamentar + fields = ['nome_parlamentar'] + + def __init__(self, *args, **kwargs): + super(ParlamentarFilterSet, self).__init__(*args, **kwargs) + + row0 = to_row([('nome_parlamentar', 12)]) + + self.form.helper = SaplFormHelper() + self.form.helper.form_method = 'GET' + self.form.helper.layout = Layout( + Fieldset(_('Pesquisa de Parlamentar'), + row0, + form_actions(label='Pesquisar')) + ) + + class ParlamentarCreateForm(ParlamentarForm): legislatura = forms.ModelChoiceField( diff --git a/sapl/parlamentares/urls.py b/sapl/parlamentares/urls.py index e383421c4..013248af0 100644 --- a/sapl/parlamentares/urls.py +++ b/sapl/parlamentares/urls.py @@ -17,7 +17,8 @@ from sapl.parlamentares.views import (CargoMesaCrud, ColigacaoCrud, frente_atualiza_lista_parlamentares, insere_parlamentar_composicao, parlamentares_frente_selected, - remove_parlamentar_composicao) + remove_parlamentar_composicao, + PesquisarParlamentarView) from .apps import AppConfig @@ -33,6 +34,9 @@ urlpatterns = [ VotanteView.get_urls() )), + url(r'^parlamentar/pesquisar-parlamentar/', + PesquisarParlamentarView.as_view(), name='pesquisar_parlamentar'), + url(r'^parlamentar/(?P\d+)/materias$', ParlamentarMateriasView.as_view(), name='parlamentar_materias'), diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index 3068e3779..246957fbe 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -10,27 +10,28 @@ from django.db.models import F, Q from django.db.models.aggregates import Count from django.http import JsonResponse from django.http.response import HttpResponseRedirect -from django.templatetags.static import static from django.utils import timezone from django.utils.datastructures import MultiValueDictKeyError from django.utils.translation import ugettext_lazy as _ from django.views.decorators.clickjacking import xframe_options_exempt from django.views.generic import FormView from django.views.generic.edit import UpdateView +from django_filters.views import FilterView from image_cropping.utils import get_backend + from sapl.base.forms import SessaoLegislativaForm, PartidoForm from sapl.base.models import Autor from sapl.comissoes.models import Participacao from sapl.crud.base import (RP_CHANGE, RP_DETAIL, RP_LIST, Crud, CrudAux, CrudBaseForListAndDetailExternalAppView, - MasterDetailCrud) + MasterDetailCrud, make_pagination) from sapl.materia.models import Autoria, Proposicao, Relatoria from sapl.parlamentares.apps import AppConfig -from sapl.utils import parlamentares_ativos +from sapl.utils import (parlamentares_ativos, show_results_filter_set) from .forms import (FiliacaoForm, FrenteForm, LegislaturaForm, MandatoForm, - ParlamentarCreateForm, ParlamentarForm, VotanteForm) + ParlamentarCreateForm, ParlamentarForm, VotanteForm, ParlamentarFilterSet) from .models import (CargoMesa, Coligacao, ComposicaoColigacao, ComposicaoMesa, Dependente, Filiacao, Frente, Legislatura, Mandato, NivelInstrucao, Parlamentar, Partido, SessaoLegislativa, @@ -164,6 +165,63 @@ class ProposicaoParlamentarCrud(CrudBaseForListAndDetailExternalAppView): _('Texto Eletrônico')) +class PesquisarParlamentarView(FilterView): + model = Parlamentar + filterset_class = ParlamentarFilterSet + paginate_by = 10 + + def get_filterset_kwargs(self, filterset_class): + super(PesquisarParlamentarView, + self).get_filterset_kwargs(filterset_class) + + kwargs = {'data': self.request.GET or None} + + qs = self.get_queryset().order_by('nome_parlamentar').distinct() + + kwargs.update({ + 'queryset': qs, + }) + return kwargs + + def get_context_data(self, **kwargs): + context = super(PesquisarParlamentarView, + self).get_context_data(**kwargs) + + paginator = context['paginator'] + page_obj = context['page_obj'] + + context['page_range'] = make_pagination( + page_obj.number, paginator.num_pages) + + context['NO_ENTRIES_MSG'] = 'Nenhum parlamentar encontrado!' + + context['title'] = _('Parlamentares') + + return context + + def get(self, request, *args, **kwargs): + super(PesquisarParlamentarView, self).get(request) + + data = self.filterset.data + url = '' + if data: + url = "&" + str(self.request.environ['QUERY_STRING']) + if url.startswith("&page"): + ponto_comeco = url.find('nome_parlamentar=') - 1 + url = url[ponto_comeco:] + + context = self.get_context_data(filter=self.filterset, + object_list=self.object_list, + filter_url=url, + numero_res=len(self.object_list) + ) + + context['show_results'] = show_results_filter_set( + self.request.GET.copy()) + + return self.render_to_response(context) + + class ParticipacaoParlamentarCrud(CrudBaseForListAndDetailExternalAppView): model = Participacao parent_field = 'parlamentar' diff --git a/sapl/templates/parlamentares/parlamentar_filter.html b/sapl/templates/parlamentares/parlamentar_filter.html new file mode 100644 index 000000000..9a3ea54f7 --- /dev/null +++ b/sapl/templates/parlamentares/parlamentar_filter.html @@ -0,0 +1,49 @@ +{% extends "crud/list.html" %} +{% load i18n %} +{% load crispy_forms_tags staticfiles %} + +{% block base_content %} + {% if not show_results %} + {% crispy filter.form %} + {% endif %} + + {% if show_results %} +
+ {% trans 'Fazer nova pesquisa' %} +
+

+ {% if numero_res > 0 %} + {% if numero_res == 1 %} +

Foi encontrado {{ numero_res }} resultado

+ {% else %} +

Foram encontrados {{ numero_res }} resultados

+ {% endif %} + + + + + + + + + + {% for usuario in page_obj %} + + + + + + {% endfor %} + +
Nome do ParlamentarNome CompletoAtivo
+ {{usuario.nome_parlamentar}} + {{ usuario.nome_completo }}{% if usuario.ativo %} Sim {% else %} Não {% endif %}
+ {% else %} +

{{ NO_ENTRIES_MSG }}

+ {% endif %} + {% endif %} + +
+ {% include 'paginacao.html'%} +


+{% endblock base_content %} diff --git a/sapl/templates/parlamentares/parlamentares_list.html b/sapl/templates/parlamentares/parlamentares_list.html index 88ad4acfe..2a724a578 100644 --- a/sapl/templates/parlamentares/parlamentares_list.html +++ b/sapl/templates/parlamentares/parlamentares_list.html @@ -1,6 +1,12 @@ {% extends "crud/list.html" %} {% load i18n %} {% load crispy_forms_tags cropping%} +{% block actions %} + {{ block.super }} +
+ Pesquisar Parlamentares +
+{% endblock actions %} {% block extra_content %}
Selecione o Período