diff --git a/sapl/base/urls.py b/sapl/base/urls.py index 967c06ee1..05aed1b2b 100644 --- a/sapl/base/urls.py +++ b/sapl/base/urls.py @@ -40,7 +40,8 @@ from .views import (AlterarSenha, AppConfigCrud, CasaLegislativaCrud, ListarParlFiliacoesIntersecaoView, ListarAutoresDuplicadosView, ListarBancadaComissaoAutorExternoView, - ListarLegislaturaInfindavelView) + ListarLegislaturaInfindavelView, + pesquisa_textual) app_name = AppConfig.name @@ -193,6 +194,9 @@ urlpatterns = [ url(r'^sistema/inconsistencias/legislatura_infindavel$', ListarLegislaturaInfindavelView.as_view(), name='lista_legislatura_infindavel'), + url(r'^sistema/pesquisa-textual', + pesquisa_textual, + name='pesquisa_textual'), url(r'^sistema/estatisticas', get_estatistica), diff --git a/sapl/base/views.py b/sapl/base/views.py index cb0a9ff41..1ea862633 100644 --- a/sapl/base/views.py +++ b/sapl/base/views.py @@ -30,6 +30,7 @@ from django.views.generic import (CreateView, DeleteView, FormView, ListView, from django.views.generic.base import RedirectView, TemplateView from django_filters.views import FilterView from haystack.views import SearchView +from haystack.query import SearchQuerySet from sapl import settings from sapl.audiencia.models import AudienciaPublica, TipoAudienciaPublica @@ -46,8 +47,7 @@ from sapl.sessao.models import (PresencaOrdemDia, SessaoPlenaria, SessaoPlenariaPresenca, Bancada) from sapl.utils import (parlamentares_ativos, gerar_hash_arquivo, SEPARADOR_HASH_PROPOSICAO, show_results_filter_set, mail_service_configured, - intervalos_tem_intersecao,) - + intervalos_tem_intersecao, remover_acentos) from .forms import (AlterarSenhaForm, CasaLegislativaForm, ConfiguracoesAppForm, RelatorioAtasFilterSet, RelatorioAudienciaFilterSet, @@ -1778,3 +1778,57 @@ class LogotipoView(RedirectView): casa = get_casalegislativa() logo = casa and casa.logotipo and casa.logotipo.name return os.path.join(settings.MEDIA_URL, logo) if logo else STATIC_LOGO + +def filtro_campos(dicionario): + + chaves_desejadas = ['ementa', + 'ano', + 'numero', + 'em_tramitacao', + 'data_apresentacao', + 'apelido', + 'indexacao', + 'data_publicacao', + 'data', + 'data_vigencia'] + del_list = [] + for key in dicionario.keys(): + if key not in chaves_desejadas: + del_list = del_list + [key] + + for key in del_list: + del dicionario[key] + + return dicionario + +def pesquisa_textual(request): + + if 'q' not in request.GET: + return JsonResponse({'total': 0, + 'resultados': []}) + + results = SearchQuerySet().filter(content=request.GET['q']) + json_dict = { + 'total': results.count(), + 'parametros': request.GET['q'], + 'resultados': [], + } + + for e in results: + + sec_dict = {} + try: + sec_dict['pk'] = e.object.pk + except: + # Index and db are out of sync. Object has been deleted from database + continue + dici = filtro_campos(e.object.__dict__) + sec_dict['objeto'] = str(dici) + sec_dict['text'] = str(e.object.ementa) + + sec_dict['model'] = str(type(e.object)) + + json_dict['resultados'].append(sec_dict) + + + return JsonResponse(json_dict) \ No newline at end of file