diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py index 1c6ad78b1..f31324ceb 100644 --- a/sapl/materia/forms.py +++ b/sapl/materia/forms.py @@ -14,20 +14,20 @@ from django.core.urlresolvers import reverse from django.db import models, transaction from django.db.models import Max from django.forms import ModelForm, ModelChoiceField, widgets -from django.forms.fields import BooleanField from django.forms.forms import Form -from django.forms.widgets import Select, HiddenInput +from django.forms.models import ModelMultipleChoiceField +from django.forms.widgets import Select, CheckboxSelectMultiple, HiddenInput from django.utils import six from django.utils.encoding import force_text from django.utils.html import format_html from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ from django_filters.filterset import STRICTNESS +from jedi.evaluate import instance import django_filters from sapl.base.models import Autor, TipoAutor from sapl.comissoes.models import Comissao -from sapl.compilacao.forms import error_messages from sapl.compilacao.models import (STATUS_TA_IMMUTABLE_PUBLIC, STATUS_TA_PRIVATE) from sapl.crispy_layout_mixin import (SaplFormLayout, form_actions, to_column, @@ -735,6 +735,55 @@ class AutoriaForm(ModelForm): return cd +class AutoriaMultiCreateForm(Form): + + tipo_autor = ModelChoiceField(label=_('Tipo Autor'), + required=False, + queryset=TipoAutor.objects.all(), + empty_label=_('Selecione'),) + + data_relativa = forms.DateField( + widget=forms.HiddenInput(), required=False) + + autor = ModelMultipleChoiceField( + queryset=Autor.objects.all(), + label=_('Possiveis Autores'), + required=False, + widget=CheckboxSelectMultiple) + + autores = ModelMultipleChoiceField( + queryset=Autor.objects.all(), + required=False, + widget=HiddenInput) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + row1 = to_row([('tipo_autor', 12), ]) + + row2 = to_row([('autor', 12), ]) + + self.helper = FormHelper() + self.helper.layout = Layout( + Fieldset( + _('Autorias'), row1, row2, 'data_relativa', 'autores', + form_actions(save_label='Incluir Autores Selecionados'))) + + self.fields['autor'].choices = [] + + def clean(self): + cd = super().clean() + + if 'autores' in self.errors: + del self.errors['autores'] + + if 'autor' not in cd or not cd['autor'].exists(): + raise ValidationError( + _('Ao menos um autor deve ser selecionado para inclusão')) + + return cd + + class AcessorioEmLoteFilterSet(django_filters.FilterSet): filter_overrides = {models.DateField: { diff --git a/sapl/materia/urls.py b/sapl/materia/urls.py index 600fadfc6..d588f1f20 100644 --- a/sapl/materia/urls.py +++ b/sapl/materia/urls.py @@ -2,8 +2,7 @@ from django.conf.urls import include, url from sapl.materia.views import (AcompanhamentoConfirmarView, AcompanhamentoExcluirView, - AcompanhamentoMateriaView, - AdicionarVariasAutorias, AnexadaCrud, + AcompanhamentoMateriaView, AnexadaCrud, AssuntoMateriaCrud, AutoriaCrud, ConfirmarProposicao, CriarProtocoloMateriaView, DespachoInicialCrud, DocumentoAcessorioCrud, @@ -21,11 +20,12 @@ from sapl.materia.views import (AcompanhamentoConfirmarView, TipoFimRelatoriaCrud, TipoMateriaCrud, TipoProposicaoCrud, TramitacaoCrud, TramitacaoEmLoteView, UnidadeTramitacaoCrud, - proposicao_texto, recuperar_materia) + proposicao_texto, recuperar_materia, + AutoriaMultiCreateView) +from . import receivers from .apps import AppConfig -from . import receivers app_name = AppConfig.name @@ -60,9 +60,9 @@ urlpatterns_materia = [ AcompanhamentoExcluirView.as_view(), name='acompanhar_excluir'), - url(r'^materia/(?P\d+)/adicionar-varias-autorias/', - AdicionarVariasAutorias.as_view(), - name='adicionar_varias_autorias'), + url(r'^materia/(?P\d+)/autoria/multicreate', + AutoriaMultiCreateView.as_view(), + name='autoria_multicreate'), url(r'^materia/acessorio-em-lote', DocumentoAcessorioEmLoteView.as_view(), name='acessorio_em_lote'), diff --git a/sapl/materia/views.py b/sapl/materia/views.py index bfa996b68..70184bc27 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -16,7 +16,7 @@ from django.db.models import Q from django.http import HttpResponse, JsonResponse from django.http.response import Http404, HttpResponseRedirect from django.shortcuts import get_object_or_404, redirect -from django.utils import formats +from django.utils import formats, deprecation from django.utils.translation import ugettext_lazy as _ from django.views.generic import CreateView, ListView, TemplateView, UpdateView from django.views.generic.base import RedirectView @@ -37,7 +37,7 @@ from sapl.crud.base import (ACTION_CREATE, ACTION_DELETE, ACTION_DETAIL, from sapl.materia.forms import (AnexadaForm, ConfirmarProposicaoForm, LegislacaoCitadaForm, AutoriaForm, ProposicaoForm, TipoProposicaoForm, TramitacaoForm, - TramitacaoUpdateForm) + TramitacaoUpdateForm, AutoriaMultiCreateForm) from sapl.materia.models import Autor from sapl.norma.models import LegislacaoCitada from sapl.parlamentares.models import Parlamentar @@ -1086,13 +1086,16 @@ class AutoriaCrud(MasterDetailCrud): parent_field = 'materia' help_path = '' public = [RP_LIST, RP_DETAIL] + list_field_names = ['autor', 'autor__tipo__descricao', 'primeiro_autor'] - class CreateView(MasterDetailCrud.CreateView): + class LocalBaseMixin(): form_class = AutoriaForm @property def layout_key(self): - return 'AutoriaCreate' + return None + + class CreateView(LocalBaseMixin, MasterDetailCrud.CreateView): def get_initial(self): initial = super().get_initial() @@ -1101,12 +1104,7 @@ class AutoriaCrud(MasterDetailCrud): initial['autor'] = [] return initial - class UpdateView(MasterDetailCrud.UpdateView): - form_class = AutoriaForm - - @property - def layout_key(self): - return 'AutoriaUpdate' + class UpdateView(LocalBaseMixin, MasterDetailCrud.UpdateView): def get_initial(self): initial = super().get_initial() @@ -1117,6 +1115,46 @@ class AutoriaCrud(MasterDetailCrud): return initial +class AutoriaMultiCreateView(FormView): + form_class = AutoriaMultiCreateForm + template_name = 'materia/autoria_multicreate_form.html' + + @classmethod + def get_url_regex(cls): + return r'^(?P\d+)/%s/multicreate' % cls.model._meta.model_name + + @property + def layout_key(self): + return None + + def get_initial(self): + initial = super().get_initial() + self.materia = MateriaLegislativa.objects.get(id=self.kwargs['pk']) + initial['data_relativa'] = self.materia.data_apresentacao + initial['autores'] = self.materia.autores.all() + return initial + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['title'] = '%s (%s)' % ( + _('Adicionar Várias Autorias'), self.materia) + return context + + def get_success_url(self): + messages.add_message( + self.request, messages.SUCCESS, + _('Autorias adicionadas com sucesso.')) + return reverse( + 'sapl.materia:autoria_list', kwargs={'pk': self.materia.pk}) + + def form_valid(self, form): + autores_selecionados = form.cleaned_data['autor'] + for autor in autores_selecionados: + Autoria.objects.create(materia=self.materia, autor=autor) + + return FormView.form_valid(self, form) + + class DespachoInicialCrud(MasterDetailCrud): model = DespachoInicial parent_field = 'materia' @@ -1694,10 +1732,10 @@ class TramitacaoEmLoteView(PrimeiraTramitacaoEmLoteView): context['primeira_tramitacao'] = False if ('tramitacao__status' in qr and - 'tramitacao__unidade_tramitacao_destino' in qr and - qr['tramitacao__status'] and - qr['tramitacao__unidade_tramitacao_destino'] - ): + 'tramitacao__unidade_tramitacao_destino' in qr and + qr['tramitacao__status'] and + qr['tramitacao__unidade_tramitacao_destino'] + ): lista = filtra_tramitacao_destino_and_status( qr['tramitacao__status'], qr['tramitacao__unidade_tramitacao_destino']) diff --git a/sapl/templates/materia/autoria_list.html b/sapl/templates/materia/autoria_list.html index 44b59c849..9574ed3b7 100644 --- a/sapl/templates/materia/autoria_list.html +++ b/sapl/templates/materia/autoria_list.html @@ -6,8 +6,8 @@ {% block more_buttons %} {% if perms|get_add_perm:view %} - - {% blocktrans with verbose_name=view.verbose_name %} Adicionar Várias Autorias {% endblocktrans %} + + {% trans "Adicionar Várias Autorias" %} {% endif %} diff --git a/sapl/templates/materia/autoria_multicreate_form.html b/sapl/templates/materia/autoria_multicreate_form.html new file mode 100644 index 000000000..59c3ece91 --- /dev/null +++ b/sapl/templates/materia/autoria_multicreate_form.html @@ -0,0 +1,51 @@ +{% extends "crud/form.html" %} +{% load i18n %} +{% load crispy_forms_tags %} +{% load common_tags %} + +{% block extra_js %} + +{% endblock %} diff --git a/sapl/templates/materia/layouts.yaml b/sapl/templates/materia/layouts.yaml index aac494432..4a86fe945 100644 --- a/sapl/templates/materia/layouts.yaml +++ b/sapl/templates/materia/layouts.yaml @@ -61,13 +61,6 @@ Autoria: {% trans 'Autoria' %}: - autor primeiro_autor -AutoriaCreate: - {% trans 'Autoria' %}: - - tipo_autor autor primeiro_autor - -AutoriaUpdate: - {% trans 'Autoria' %}: - - tipo_autor autor primeiro_autor DocumentoAcessorio: {% trans 'Documento Acessório' %}: