Repository: interlegis/sapl

Issue:
Documento Acessório em Lote



Discussion:

Resolving patch:
diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py
--- a/sapl/materia/forms.py
+++ b/sapl/materia/forms.py
@@ -755,3 +755,34 @@ def save(self, commit=False):
         u.groups.add(grupo)
 
         return autor
+
+
+class AcessorioEmLoteFilterSet(django_filters.FilterSet):
+
+    filter_overrides = {models.DateField: {
+        'filter_class': django_filters.DateFromToRangeFilter,
+        'extra': lambda f: {
+            'label': '%s (%s)' % (f.verbose_name, _('Inicial - Final')),
+            'widget': RangeWidgetOverride}
+    }}
+
+    class Meta:
+        model = MateriaLegislativa
+        fields = ['tipo', 'data_apresentacao']
+
+    def __init__(self, *args, **kwargs):
+        super(AcessorioEmLoteFilterSet, self).__init__(*args, **kwargs)
+
+        self.filters['tipo'].label = 'Tipo de Matéria'
+        self.filters['data_apresentacao'].label = 'Data (Inicial - Final)'
+        self.form.fields['tipo'].required = True
+        self.form.fields['data_apresentacao'].required = True
+
+        row1 = to_row([('tipo', 12)])
+        row2 = to_row([('data_apresentacao', 12)])
+
+        self.form.helper = FormHelper()
+        self.form.helper.form_method = 'GET'
+        self.form.helper.layout = Layout(
+            Fieldset(_('Documentos Acessórios em Lote'),
+                     row1, row2, form_actions(save_label='Pesquisar')))
diff --git a/sapl/materia/urls.py b/sapl/materia/urls.py
--- a/sapl/materia/urls.py
+++ b/sapl/materia/urls.py
@@ -5,8 +5,9 @@
                                 AcompanhamentoMateriaView, AnexadaCrud,
                                 AutorCrud, AutoriaCrud, ConfirmarEmailView,
                                 ConfirmarProposicao, DespachoInicialCrud,
-                                DocumentoAcessorioCrud, LegislacaoCitadaCrud,
-                                MateriaLegislativaCrud,
+                                DocumentoAcessorioCrud,
+                                DocumentoAcessorioEmLoteView,
+                                LegislacaoCitadaCrud, MateriaLegislativaCrud,
                                 MateriaLegislativaPesquisaView, MateriaTaView,
                                 NumeracaoCrud, OrgaoCrud, OrigemCrud,
                                 ProposicaoCrud, ProposicaoDevolvida,
@@ -86,4 +87,7 @@
     url(r'^materia/(?P<pk>\d+)/acompanhar-excluir$',
         AcompanhamentoExcluirView.as_view(),
         name='acompanhar_excluir'),
+
+    url(r'^acessorio-em-lote', DocumentoAcessorioEmLoteView.as_view(),
+        name='acessorio_em_lote'),
 ]
diff --git a/sapl/materia/views.py b/sapl/materia/views.py
--- a/sapl/materia/views.py
+++ b/sapl/materia/views.py
@@ -4,7 +4,6 @@
 
 from crispy_forms.helper import FormHelper
 from crispy_forms.layout import HTML, Button
-from django.db.models import Q
 from django.conf import settings
 from django.contrib import messages
 from django.contrib.auth.mixins import PermissionRequiredMixin
@@ -13,6 +12,7 @@
 from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
 from django.core.mail import send_mail
 from django.core.urlresolvers import reverse
+from django.db.models import Q
 from django.http.response import HttpResponseRedirect
 from django.shortcuts import redirect
 from django.template import Context, loader
@@ -34,8 +34,9 @@
                         get_base_url, permissao_tb_aux, permissoes_autor,
                         permissoes_materia, permissoes_protocoloadm)
 
-from .forms import (AcompanhamentoMateriaForm, AnexadaForm, AutorForm,
-                    AutoriaForm, ConfirmarProposicaoForm, DespachoInicialForm,
+from .forms import (AcessorioEmLoteFilterSet, AcompanhamentoMateriaForm,
+                    AnexadaForm, AutorForm, AutoriaForm,
+                    ConfirmarProposicaoForm, DespachoInicialForm,
                     DocumentoAcessorioForm, LegislacaoCitadaForm,
                     MateriaLegislativaFilterSet, NumeracaoForm, ProposicaoForm,
                     ReceberProposicaoForm, RelatoriaForm, TramitacaoForm,
@@ -1246,3 +1247,47 @@ def do_envia_email_tramitacao(request, materia):
 
     enviar_emails(sender, recipients, messages)
     return None
+
+
+class DocumentoAcessorioEmLoteView(PermissionRequiredMixin, FilterView):
+    filterset_class = AcessorioEmLoteFilterSet
+    template_name = 'materia/acessorio_lote.html'
+    permission_required = permissoes_materia()
+
+    def get_context_data(self, **kwargs):
+        context = super(DocumentoAcessorioEmLoteView,
+                        self).get_context_data(**kwargs)
+
+        context['title'] = _('Documentos Acessórios em Lote')
+        # Verifica se os campos foram preenchidos
+        if not self.filterset.form.is_valid():
+            return context
+
+        qr = self.request.GET.copy()
+        context['tipos_docs'] = TipoDocumento.objects.all()
+        context['filter_url'] = ('&' + qr.urlencode()) if len(qr) > 0 else ''
+        return context
+
+    def post(self, request, *args, **kwargs):
+        marcadas = request.POST.getlist('materia_id')
+
+        if len(marcadas) == 0:
+            msg = _('Nenhuma máteria foi selecionada.')
+            messages.add_message(request, messages.ERROR, msg)
+            return self.get(request, self.kwargs)
+
+        tipo = TipoDocumento.objects.get(descricao=request.POST['tipo'])
+
+        for materia_id in marcadas:
+            DocumentoAcessorio.objects.create(
+                materia_id=materia_id,
+                tipo=tipo,
+                arquivo=request.POST['arquivo'],
+                nome=request.POST['nome'],
+                data=datetime.strptime(request.POST['data'], "%d/%m/%Y"),
+                autor=Autor.objects.get(id=request.POST['autor']),
+                ementa=request.POST['ementa']
+            )
+        msg = _('Documento(s) criado(s).')
+        messages.add_message(request, messages.SUCCESS, msg)
+        return self.get(request, self.kwargs)


Test patch: