Repository: interlegis/sapl

Issue:
Possibilitar desvincular matéria legislativa de protocolo



Discussion:

Resolving patch:
diff --git a/sapl/protocoloadm/forms.py b/sapl/protocoloadm/forms.py
--- a/sapl/protocoloadm/forms.py
+++ b/sapl/protocoloadm/forms.py
@@ -758,6 +758,7 @@ def __init__(self, *args, **kwargs):
         super(DocumentoAdministrativoForm, self).__init__(
             *args, **kwargs)
 
+
 class DesvincularDocumentoForm(ModelForm):
 
     numero = forms.CharField(required=True,
@@ -816,4 +817,58 @@ def __init__(self, *args, **kwargs):
                      )
         )
         super(DesvincularDocumentoForm, self).__init__(
-            *args, **kwargs)
\ No newline at end of file
+            *args, **kwargs)
+
+
+class DesvincularMateriaForm(forms.Form):
+
+    numero = forms.CharField(required=True,
+                             label=_('Número da Matéria'))
+    ano = forms.ChoiceField(required=True,
+                            label=_('Ano da Matéria'),
+                            choices=RANGE_ANOS,
+                            widget=forms.Select(attrs={'class': 'selector'}))
+    tipo = forms.ModelChoiceField(label=_('Tipo de Matéria'),
+                                  required=True,
+                                  queryset=TipoMateriaLegislativa.objects.all(),
+                                  empty_label='------')
+
+    def clean(self):
+        super(DesvincularMateriaForm, self).clean()
+
+        cleaned_data = self.cleaned_data
+
+        if not self.is_valid():
+            return cleaned_data
+
+        numero = cleaned_data['numero']
+        ano = cleaned_data['ano']
+        tipo = cleaned_data['tipo']
+
+        try:
+            materia = MateriaLegislativa.objects.get(numero=numero, ano=ano, tipo=tipo)
+            if not materia.numero_protocolo:
+                raise forms.ValidationError(
+                    _("%s %s/%s não se encontra vinculada a nenhum protocolo" % (tipo, numero, ano)))
+        except ObjectDoesNotExist:
+            raise forms.ValidationError(
+                _("%s %s/%s não existe" % (tipo, numero, ano)))
+
+        return cleaned_data
+
+    def __init__(self, *args, **kwargs):
+        super(DesvincularMateriaForm, self).__init__(*args, **kwargs)
+
+        row1 = to_row(
+            [('numero', 4),
+             ('ano', 4),
+             ('tipo', 4)])
+
+        self.helper = FormHelper()
+        self.helper.layout = Layout(
+            Fieldset(_('Identificação da Matéria'),
+                     row1,
+                     HTML("&nbsp;"),
+                     form_actions(label='Desvincular')
+                     )
+        )
diff --git a/sapl/protocoloadm/urls.py b/sapl/protocoloadm/urls.py
--- a/sapl/protocoloadm/urls.py
+++ b/sapl/protocoloadm/urls.py
@@ -16,7 +16,8 @@
                                      TramitacaoAdmCrud,
                                      atualizar_numero_documento,
                                      doc_texto_integral,
-                                     DesvincularDocumentoView)
+                                     DesvincularDocumentoView,
+                                     DesvincularMateriaView)
 
 from .apps import AppConfig
 
@@ -64,6 +65,8 @@
         AnularProtocoloAdmView.as_view(), name='anular_protocolo'),
     url(r'^protocoloadm/desvincular-documento',
         DesvincularDocumentoView.as_view(), name='desvincular_documento'),
+    url(r'^protocoloadm/desvincular-materia',
+        DesvincularMateriaView.as_view(), name='desvincular_materia'),
     url(r'^protocoloadm/protocolar-mat',
         ProtocoloMateriaView.as_view(), name='protocolar_mat'),
 
diff --git a/sapl/protocoloadm/views.py b/sapl/protocoloadm/views.py
--- a/sapl/protocoloadm/views.py
+++ b/sapl/protocoloadm/views.py
@@ -13,6 +13,7 @@
 from django.utils.translation import ugettext_lazy as _
 from django.views.generic import CreateView, ListView
 from django.views.generic.base import RedirectView, TemplateView
+from django.views.generic.edit import FormView
 from django_filters.views import FilterView
 
 import sapl
@@ -30,7 +31,7 @@
                     DocumentoAdministrativoFilterSet,
                     DocumentoAdministrativoForm, ProtocoloDocumentForm,
                     ProtocoloFilterSet, ProtocoloMateriaForm,
-                    TramitacaoAdmEditForm, TramitacaoAdmForm, DesvincularDocumentoForm)
+                    TramitacaoAdmEditForm, TramitacaoAdmForm, DesvincularDocumentoForm, DesvincularMateriaForm)
 from .models import (DocumentoAcessorioAdministrativo, DocumentoAdministrativo,
                      StatusTramitacaoAdministrativo,
                      TipoDocumentoAdministrativo, TramitacaoAdministrativo)
@@ -722,6 +723,7 @@ def atualizar_numero_documento(request):
 
     return response
 
+
 class DesvincularDocumentoView(PermissionRequiredMixin, CreateView):
     template_name = 'protocoloadm/anular_protocoloadm.html'
     form_class = DesvincularDocumentoForm
@@ -738,3 +740,21 @@ def form_valid(self, form):
         documento.protocolo = None
         documento.save()
         return redirect(self.get_success_url())
+
+
+class DesvincularMateriaView(PermissionRequiredMixin, FormView):
+    template_name = 'protocoloadm/anular_protocoloadm.html'
+    form_class = DesvincularMateriaForm
+    form_valid_message = _('Matéria desvinculado com sucesso!')
+    permission_required = ('protocoloadm.action_anular_protocolo', )
+
+    def get_success_url(self):
+        return reverse('sapl.protocoloadm:protocolo')
+
+    def form_valid(self, form):
+        materia = MateriaLegislativa.objects.get(numero=form.cleaned_data['numero'],
+                                                        ano=form.cleaned_data['ano'],
+                                                        tipo=form.cleaned_data['tipo'])
+        materia.numero_protocolo = None
+        materia.save()
+        return redirect(self.get_success_url())
\ No newline at end of file


Test patch: