From 6edbedecae47f338b886823cf2efa0866b5ec4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Cantu=C3=A1ria?= Date: Mon, 29 Jun 2020 12:52:03 -0300 Subject: [PATCH] 3177 - Adiciona Pesquisa de Autor (#3196) * Adiciona pesquisa de autor * Corrige nome do campo para pesquisa --- sapl/base/forms.py | 20 ++++++++ sapl/base/urls.py | 4 +- sapl/base/views.py | 54 ++++++++++++++++++++- sapl/templates/base/autor_detail.html | 14 ++++++ sapl/templates/base/autor_filter.html | 51 +++++++++++++++++++ sapl/templates/menu_tabelas_auxiliares.yaml | 7 ++- 6 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 sapl/templates/base/autor_detail.html create mode 100644 sapl/templates/base/autor_filter.html diff --git a/sapl/base/forms.py b/sapl/base/forms.py index b1ce0f9e8..c6f4a3ce8 100644 --- a/sapl/base/forms.py +++ b/sapl/base/forms.py @@ -770,6 +770,26 @@ class AutorForm(ModelForm): return autor +class AutorFilterSet(django_filters.FilterSet): + nome = django_filters.CharFilter(label=_('Nome do Autor'), lookup_expr='icontains') + + class Meta: + model = Autor + fields = ['nome'] + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + row0 = to_row([('nome', 12)]) + + self.form.helper = SaplFormHelper() + self.form.helper.form_method = 'GET' + self.form.helper.layout = Layout( + Fieldset(_('Pesquisa de Autor'), + row0, + form_actions(label='Pesquisar'))) + + class AutorFormForAdmin(AutorForm): status_user = forms.ChoiceField( label=_('Bloqueio do Usuário Existente'), diff --git a/sapl/base/urls.py b/sapl/base/urls.py index e8bcc587b..2630f5114 100644 --- a/sapl/base/urls.py +++ b/sapl/base/urls.py @@ -8,7 +8,8 @@ from django.contrib.auth.views import (password_reset, password_reset_complete, password_reset_done) from django.views.generic.base import RedirectView, TemplateView -from sapl.base.views import AutorCrud, ConfirmarEmailView, TipoAutorCrud, get_estatistica, DetailUsuarioView +from sapl.base.views import AutorCrud, ConfirmarEmailView, TipoAutorCrud, get_estatistica, DetailUsuarioView, \ + PesquisarAutorView from sapl.settings import EMAIL_SEND_USER, MEDIA_URL from .apps import AppConfig @@ -90,6 +91,7 @@ recuperar_senha = [ urlpatterns = [ url(r'^sistema/autor/tipo/', include(TipoAutorCrud.get_urls())), url(r'^sistema/autor/', include(AutorCrud.get_urls())), + url(r'^sistema/autor/pesquisar-autor/', PesquisarAutorView.as_view(), name='pesquisar_autor'), url(r'^sistema/ajuda/(?P\w+)$', HelpTopicView.as_view(), name='help_topic'), diff --git a/sapl/base/views.py b/sapl/base/views.py index 260253527..56e8879e1 100644 --- a/sapl/base/views.py +++ b/sapl/base/views.py @@ -41,7 +41,7 @@ from sapl.relatorios.views import (relatorio_materia_em_tramitacao, relatorio_ma from sapl import settings from sapl.audiencia.models import AudienciaPublica, TipoAudienciaPublica from sapl.base.models import Autor, TipoAutor -from sapl.base.forms import AutorForm, AutorFormForAdmin, TipoAutorForm +from sapl.base.forms import AutorForm, AutorFormForAdmin, TipoAutorForm, AutorFilterSet from sapl.comissoes.models import Comissao, Reuniao from sapl.crud.base import CrudAux, make_pagination from sapl.materia.models import (Anexada, Autoria, DocumentoAcessorio, @@ -295,6 +295,58 @@ class AutorCrud(CrudAux): return url_reverse +class PesquisarAutorView(FilterView): + model = Autor + filterset_class = AutorFilterSet + paginate_by = 10 + + def get_filterset_kwargs(self, filterset_class): + super().get_filterset_kwargs(filterset_class) + + kwargs = {'data': self.request.GET or None} + + qs = self.get_queryset().order_by('nome').distinct() + + kwargs.update({ + 'queryset': qs, + }) + return kwargs + + def get_context_data(self, **kwargs): + context = super().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 Autor encontrado!' + + context['title'] = _('Autores') + + return context + + def get(self, request, *args, **kwargs): + super().get(request) + + data = self.filterset.data + url = '' + if data: + url = "&" + str(self.request.META['QUERY_STRING']) + if url.startswith("&page"): + ponto_comeco = url.find('nome=') - 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 RelatoriosListView(TemplateView): template_name='base/relatorios_list.html' diff --git a/sapl/templates/base/autor_detail.html b/sapl/templates/base/autor_detail.html new file mode 100644 index 000000000..c9938e372 --- /dev/null +++ b/sapl/templates/base/autor_detail.html @@ -0,0 +1,14 @@ +{% extends "crud/detail.html" %} +{% load i18n %} +{% load crispy_forms_tags staticfiles %} + +{% block sub_actions %} + +{% endblock sub_actions %} diff --git a/sapl/templates/base/autor_filter.html b/sapl/templates/base/autor_filter.html new file mode 100644 index 000000000..5e1a05542 --- /dev/null +++ b/sapl/templates/base/autor_filter.html @@ -0,0 +1,51 @@ +{% extends "crud/list.html" %} +{% load i18n %} +{% load crispy_forms_tags staticfiles %} + +{% block base_content %} + {% if not show_results %} + {% crispy filter.form %} + {% else %} +
+ {% trans 'Fazer nova pesquisa' %} + {% if not request.user.is_anonymous %} + Cadastrar Autor + {% endif %} +
+
+ {% if numero_res > 0 %} + {% if numero_res == 1 %} +

Foi encontrado {{ numero_res }} resultado

+ {% else %} +

Foram encontrados {{ numero_res }} resultados

+ {% endif %} + + + + + + + + + + {% for autor in page_obj %} + + + + + + {% endfor %} + +
Tipo do AutorNome do AutorUsuário
{{ autor.tipo }} + + {% if autor.nome %} {{ autor.nome }} {% else %} - {% endif %} + + {% if autor.user %} {{ autor.user }} {% else %} - {% endif %}
+ {% else %} +

{{ NO_ENTRIES_MSG }}

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


+{% endblock base_content %} diff --git a/sapl/templates/menu_tabelas_auxiliares.yaml b/sapl/templates/menu_tabelas_auxiliares.yaml index 84dbde48a..8a83ac44f 100644 --- a/sapl/templates/menu_tabelas_auxiliares.yaml +++ b/sapl/templates/menu_tabelas_auxiliares.yaml @@ -8,8 +8,11 @@ - title: {% trans 'Configurações da Aplicação' %} url: sapl.base:appconfig_list css_class: btn btn-link - - title: {% trans 'Autor' %} - url: sapl.base:autor_list + - title: {% trans 'Pesquisar Autor' %} + url: sapl.base:pesquisar_autor + css_class: btn btn-link + - title: {% trans 'Adicionar Autor' %} + url: sapl.base:autor_create css_class: btn btn-link - title: {% trans 'Tipo de Autor' %} url: sapl.base:tipoautor_list