diff --git a/sapl/materia/urls.py b/sapl/materia/urls.py index b6a87cd5f..44051342f 100644 --- a/sapl/materia/urls.py +++ b/sapl/materia/urls.py @@ -25,6 +25,8 @@ from sapl.materia.views import (AcompanhamentoConfirmarView, TramitacaoEmLoteView, UnidadeTramitacaoCrud, proposicao_texto, recuperar_materia) +from sapl.norma.views import NormaPesquisaView + from .apps import AppConfig app_name = AppConfig.name @@ -42,6 +44,9 @@ urlpatterns_impressos = [ url(r'^materia/impressos/ficha-seleciona/$', FichaSelecionaView.as_view(), name='impressos_ficha_seleciona'), + url(r'^materia/impressos/norma-pesquisa/$', + NormaPesquisaView.as_view(), + name='impressos_norma_pesquisa'), ] urlpatterns_materia = [ diff --git a/sapl/norma/forms.py b/sapl/norma/forms.py index fa849171a..c21650513 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -208,3 +208,61 @@ class NormaRelacionadaForm(ModelForm): relacionada.norma_relacionada = self.cleaned_data['norma_relacionada'] relacionada.save() return relacionada + +class NormaPesquisaForm(forms.Form): + tipo_norma = forms.ModelChoiceField( + label=TipoNormaJuridica._meta.verbose_name, + queryset=TipoNormaJuridica.objects.all(), + required=False, + empty_label='Selecione') + + data_inicial = forms.DateField( + label='Data Inicial', + required=False, + widget=forms.DateInput(format='%d/%m/%Y') + ) + + data_final = forms.DateField( + label='Data Final', + required=False, + widget=forms.DateInput(format='%d/%m/%Y') + ) + + def __init__(self, *args, **kwargs): + super(NormaPesquisaForm, self).__init__(*args, **kwargs) + + row1 = to_row( + [('tipo_norma', 6), + ('data_inicial', 3), + ('data_final', 3)]) + + self.helper = FormHelper() + self.helper.layout = Layout( + Fieldset( + ('Índice de Normas'), + row1, + form_actions(label='Pesquisar') + ) + ) + + def clean(self): + super(NormaPesquisaForm, self).clean() + cleaned_data = self.cleaned_data + + data_inicial = cleaned_data['data_inicial'] + data_final = cleaned_data['data_final'] + + if (data_inicial and data_final and + data_inicial > data_final): + raise ValidationError(_( + 'A Data Final não pode ser menor que a Data Inicial')) + else: + condicao1 = data_inicial and not data_final + condicao2 = not data_inicial and data_final + if condicao1 or condicao2: + raise ValidationError(_('Caso pesquise por data, os campos de Data Inicial e ' + + 'Data Final devem ser preenchidos obrigatoriamente')) + + + return cleaned_data + diff --git a/sapl/norma/views.py b/sapl/norma/views.py index f7ee34baf..8cf8469d2 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -6,6 +6,13 @@ from django.utils import timezone from django.utils.translation import ugettext_lazy as _ from django.views.generic.base import RedirectView from django_filters.views import FilterView +from django.contrib.auth.mixins import PermissionRequiredMixin + +from django.http import HttpResponse, JsonResponse +from django.views.generic.edit import FormView +from django.views.generic import CreateView, ListView, TemplateView, UpdateView +from django.template import RequestContext, loader +import weasyprint from sapl.base.models import AppConfig from sapl.compilacao.views import IntegracaoTaView @@ -13,7 +20,7 @@ from sapl.crud.base import (RP_DETAIL, RP_LIST, Crud, CrudAux, MasterDetailCrud, make_pagination) from sapl.utils import show_results_filter_set -from .forms import NormaFilterSet, NormaJuridicaForm, NormaRelacionadaForm +from .forms import NormaFilterSet, NormaJuridicaForm, NormaRelacionadaForm, NormaPesquisaForm from .models import (AssuntoNorma, NormaJuridica, NormaRelacionada, TipoNormaJuridica, TipoVinculoNormaJuridica) @@ -216,3 +223,51 @@ def recuperar_numero_norma(request): {'numero': 1, 'ano': ano}) return response + + +class ImpressosView(PermissionRequiredMixin, TemplateView): + template_name = 'materia/impressos/impressos.html' + permission_required = ('materia.can_access_impressos', ) + + +def gerar_pdf_impressos(request, context, template_name): + template = loader.get_template(template_name) + html = template.render(RequestContext(request, context)) + response = HttpResponse(content_type="application/pdf") + weasyprint.HTML( + string=html, + base_url=request.build_absolute_uri()).write_pdf( + response) + + return response + +class NormaPesquisaView(PermissionRequiredMixin, FormView): + form_class = NormaPesquisaForm + template_name = 'materia/impressos/norma.html' + permission_required = ('materia.can_access_impressos', ) + + + def form_valid(self, form): + context = {} + + normas = NormaJuridica.objects.all().order_by( + '-numero') + template_norma = 'materia/impressos/normas_pdf.html' + + if form.cleaned_data['tipo_norma']: + normas = normas.filter(tipo=form.cleaned_data['tipo_norma']) + + if form.cleaned_data['data_inicial']: + normas = normas.filter( + data__gte=form.cleaned_data['data_inicial'], + data__lte=form.cleaned_data['data_final']) + + + context['quantidade'] = len(normas) + + if context['quantidade'] > 2000: + normas = normas[:2000] + + context['normas'] = normas + + return gerar_pdf_impressos(self.request, context, template_norma) diff --git a/sapl/templates/materia/impressos/impressos.html b/sapl/templates/materia/impressos/impressos.html index beb92862c..71f9f6f6f 100644 --- a/sapl/templates/materia/impressos/impressos.html +++ b/sapl/templates/materia/impressos/impressos.html @@ -10,15 +10,23 @@

Impressos

+

Etiqueta

-

Ficha

+
+

Capa Processo

+
+

Índice de Normas Jurídicas

+ + {#

Guia de Remessa

#} {#