diff --git a/sapl/compilacao/forms.py b/sapl/compilacao/forms.py index c3405eb7f..f01940eac 100644 --- a/sapl/compilacao/forms.py +++ b/sapl/compilacao/forms.py @@ -1328,3 +1328,85 @@ class DispositivoRegistroRevogacaoForm(Form): super(DispositivoRegistroRevogacaoForm, self).__init__(*args, **kwargs) self.fields['dispositivo_revogado'].choices = [] + + +class DispositivoRegistroInclusaoForm(Form): + + dispositivo_base_para_inclusao = forms.ModelChoiceField( + label=_('Dispositivo Base para inclusão de novo dispositivo'), + required=False, + queryset=Dispositivo.objects.all()) + + dispositivo_search_form = forms.CharField(widget=forms.HiddenInput(), + required=False) + + def __init__(self, *args, **kwargs): + + layout = [] + kwargs.pop('instance') + kwargs['initial'].pop('editor_type') + + row_dispositivo = Field( + 'dispositivo_base_para_inclusao', + data_sapl_ta='DispositivoSearch', + data_field='dispositivo_base_para_inclusao', + data_type_selection='radio', + template="compilacao/layout/dispositivo_radio.html") + + layout.append(Fieldset(_('Registro de Inclusão - ' + 'Seleção do Dispositivo Base para inclusão ' + 'de novo dispositivo.'), + row_dispositivo, + css_class="col-md-6")) + layout.append(Field('dispositivo_search_form')) + layout.append(Div(css_class="allowed_inserts col-md-6")) + + more = [ + HTML('%s' % + _('Cancelar')), + ] + more.append(Submit('salvar', _('Salvar'), css_class='pull-right')) + + buttons = FormActions(*more, css_class='form-group') + + _fields = [Div(*layout, css_class="row-fluid")] + \ + [to_row([(buttons, 12)])] + + self.helper = FormHelper() + self.helper.layout = Layout(*_fields) + + super(DispositivoRegistroInclusaoForm, self).__init__(*args, **kwargs) + + self.fields['dispositivo_base_para_inclusao'].choices = [] + + +class AllowedInsertsFragmentForm(forms.Form): + + json_add_next = forms.ChoiceField( + label=_('Inserir Depois'), choices=[], + required=False) + + json_add_in = forms.ChoiceField( + label=_('Inserir Dentro'), choices=[], + required=False) + + def __init__(self, *args, **kwargs): + kwargs.pop('instance') + allowed_inserts = kwargs['initial'].pop('allowed_inserts') + + super(AllowedInsertsFragmentForm, self).__init__( + *args, **kwargs) + + self.fields['json_add_next'].widget = forms.RadioSelect() + self.fields['json_add_in'].widget = forms.RadioSelect() + + for opcoes in allowed_inserts: + self.fields[opcoes['action']].choices = [ + ('%s,%s' % (item['tipo_pk'], + item['variacao']), + item['provavel']) + for item in opcoes['itens']] + + self.helper = FormHelper() + self.helper.form_tag = False + self.helper.disable_csrf = True diff --git a/sapl/compilacao/views.py b/sapl/compilacao/views.py index 23d698465..4a3d4c7f3 100644 --- a/sapl/compilacao/views.py +++ b/sapl/compilacao/views.py @@ -1,6 +1,6 @@ +import sys from collections import OrderedDict from datetime import datetime, timedelta -import sys from braces.views import FormMessagesMixin from django import forms @@ -23,15 +23,17 @@ from django.views.generic.edit import (CreateView, DeleteView, FormView, UpdateView) from django.views.generic.list import ListView -from sapl.compilacao.forms import (DispositivoDefinidorVigenciaForm, +from sapl.compilacao.forms import (AllowedInsertsFragmentForm, + DispositivoDefinidorVigenciaForm, DispositivoEdicaoAlteracaoForm, DispositivoEdicaoBasicaForm, DispositivoEdicaoVigenciaForm, DispositivoRegistroAlteracaoForm, + DispositivoRegistroInclusaoForm, + DispositivoRegistroRevogacaoForm, DispositivoSearchModalForm, NotaForm, PublicacaoForm, TaForm, - TextNotificacoesForm, TipoTaForm, VideForm, - DispositivoRegistroRevogacaoForm) + TextNotificacoesForm, TipoTaForm, VideForm) from sapl.compilacao.models import (Dispositivo, Nota, PerfilEstruturalTextoArticulado, Publicacao, TextoArticulado, @@ -42,7 +44,6 @@ from sapl.compilacao.utils import (DISPOSITIVO_SELECT_RELATED, DISPOSITIVO_SELECT_RELATED_EDIT) from sapl.crud.base import Crud, CrudListView, make_pagination - TipoNotaCrud = Crud.build(TipoNota, 'tipo_nota') TipoVideCrud = Crud.build(TipoVide, 'tipo_vide') TipoPublicacaoCrud = Crud.build(TipoPublicacao, 'tipo_publicacao') @@ -1460,7 +1461,7 @@ class ActionDeleteDispositivoMixin(ActionsCommonsMixin): class ActionDispositivoCreateMixin(ActionsCommonsMixin): - def allowed_inserts(self): + def allowed_inserts(self, _base=None): request = self.request try: if request and 'perfil_estrutural' not in request.session: @@ -1468,7 +1469,8 @@ class ActionDispositivoCreateMixin(ActionsCommonsMixin): perfil_pk = request.session['perfil_estrutural'] - base = Dispositivo.objects.get(pk=self.kwargs['dispositivo_id']) + base = Dispositivo.objects.get( + pk=self.kwargs['dispositivo_id'] if not _base else _base) prox_possivel = Dispositivo.objects.filter( ordem__gt=base.ordem, @@ -2067,6 +2069,13 @@ class ActionsEditMixin(ActionDragAndMoveDispositivoAlteradoMixin, return perfis[0].pk return None + def registra_inclusao(self, bloco_alteracao, dispositivo_base_inclusao): + data = {} + data.update({'pk': bloco_alteracao.pk, + 'pai': [bloco_alteracao.pk, ]}) + + return data + def registra_revogacao(self, bloco_alteracao, dispositivo_a_revogar): return self.registra_alteracao( bloco_alteracao, @@ -2215,8 +2224,13 @@ class DispositivoDinamicEditView( if 'action' in self.request.GET: initial.update({'editor_type': self.request.GET['action']}) + if self.action.startswith('get_form_'): + if self.action.endswith('_radio_allowed_inserts'): + initial.update({'allowed_inserts': self.allowed_inserts()}) + initial.update({'dispositivo_search_form': reverse_lazy( 'sapl.compilacao:dispositivo_search_form')}) + return initial def get_form(self, form_class=None): @@ -2245,8 +2259,13 @@ class DispositivoDinamicEditView( self.form_class = DispositivoRegistroAlteracaoForm elif self.action.endswith('_revogacao'): self.form_class = DispositivoRegistroRevogacaoForm + elif self.action.endswith('_inclusao'): + self.form_class = DispositivoRegistroInclusaoForm + elif self.action.endswith('_radio_allowed_inserts'): + self.form_class = AllowedInsertsFragmentForm context = self.get_context_data() return self.render_to_response(context) + elif self.action.startswith('get_actions'): self.form_class = None self.template_name = 'compilacao/ajax_actions_dinamic_edit.html' @@ -2298,6 +2317,13 @@ class DispositivoDinamicEditView( data = self.registra_revogacao(d, dispositivo_a_revogar) + if formtype == 'get_form_inclusao': + + dispositivo_base_para_inclusao = Dispositivo.objects.get( + pk=request.POST['dispositivo_base_para_inclusao']) + + data = self.registra_inclusao(d, dispositivo_base_para_inclusao) + elif formtype == 'get_form_base': texto = request.POST['texto'].strip() texto_atualizador = request.POST['texto_atualizador'].strip() diff --git a/sapl/static/js/compilacao.js b/sapl/static/js/compilacao.js index 22093eed5..f18d56bc2 100644 --- a/sapl/static/js/compilacao.js +++ b/sapl/static/js/compilacao.js @@ -220,6 +220,10 @@ function DispostivoSearch(opts) { onChangeFieldSelects(); modal_ds.modal('hide'); + + if ('post_selected' in opts) + opts['post_selected']() + }); modal_ds.modal('show'); diff --git a/sapl/static/js/compilacao_edit.js b/sapl/static/js/compilacao_edit.js index b78ad897a..c34ccbc93 100644 --- a/sapl/static/js/compilacao_edit.js +++ b/sapl/static/js/compilacao_edit.js @@ -138,6 +138,29 @@ function DispositivoEdit() { }); } + instance.get_form_inclusao = function () { + var _this = $(this); + _this.off('get_form_inclusao'); + $('.dpt-actions, .dpt-actions-bottom').html(''); + + var dpt_form = _this.children().filter('.dpt-form').children().first(); + var url_search = dpt_form[0]['id_dispositivo_search_form'].value; + DispostivoSearch({ + 'url_form': url_search, + 'text_button': 'Selecionar', + 'post_selected': instance.update_radio_allowed_inserts + }); + + instance.scrollTo(_this); + dpt_form.submit(instance.onSubmitFormRegistraInclusao); + + var btn_fechar = _this.find('.btn-fechar'); + btn_fechar.on('click', function() { + instance.clearEditSelected(); + instance.triggerBtnDptEdit(_this.attr('pk')); + }); + } + instance.get_form_revogacao = function () { var _this = $(this); _this.off('get_form_revogacao'); @@ -160,6 +183,26 @@ function DispositivoEdit() { }); } + instance.update_radio_allowed_inserts = function(event) { + + dispositivo_base_para_inclusao = $("input[name='dispositivo_base_para_inclusao']") + if (dispositivo_base_para_inclusao.length == 0) + return + var pk = dispositivo_base_para_inclusao[0].value; + + var form_data = { + 'action' : 'get_form_radio_allowed_inserts' + }; + + var url = pk+'/refresh'; + instance.waitShow(); + + $.get(url, form_data).done(function(data) { + $(".allowed_inserts").html(data); + }).fail(instance.waitHide).always(instance.waitHide); + + } + instance.loadActionsEdit = function(dpt) { var pk = dpt.attr('pk'); var url = pk+'/refresh?action=get_actions'; @@ -282,6 +325,37 @@ function DispositivoEdit() { event.preventDefault(); } + instance.onSubmitFormRegistraInclusao = function(event) { + var _this = this; + + var form_data = { + 'csrfmiddlewaretoken' : this['csrfmiddlewaretoken'].value, + 'dispositivo_base_para_inclusao' : this['dispositivo_base_para_inclusao'].value, + 'formtype': 'get_form_inclusao', + }; + var url = $(this).closest('.dpt').attr( "pk" )+'/refresh'; + + instance.waitShow(); + + $.post(url, form_data) + .done(function(data) { + instance.clearEditSelected(); + + if (data.pk != null) { + instance.refreshScreenFocusPk(data); + instance.message(data); + } + else { + alert('Erro na resposta!'); + } + + }).always(function() { + instance.waitHide(); + }); + if (event != null) + event.preventDefault(); + } + instance.onSubmitFormRegistraRevogacao = function(event) { var _this = this; diff --git a/sapl/static/styles/compilacao.scss b/sapl/static/styles/compilacao.scss index 148244c0b..170ff9f16 100644 --- a/sapl/static/styles/compilacao.scss +++ b/sapl/static/styles/compilacao.scss @@ -1297,6 +1297,7 @@ a:link:after, a:visited:after { border-collapse:separate; border-bottom: 1px solid white; + width: 100%; &.ta_title { background-color: rgba(0, 0, 0, 0.15); diff --git a/sapl/templates/compilacao/ajax_actions_dinamic_edit.html b/sapl/templates/compilacao/ajax_actions_dinamic_edit.html index 2d746740e..c819723b7 100644 --- a/sapl/templates/compilacao/ajax_actions_dinamic_edit.html +++ b/sapl/templates/compilacao/ajax_actions_dinamic_edit.html @@ -61,7 +61,7 @@ {% endfor %} {%endif%} - {% if not object.ta_publicado and not object.dispositivo_subsequente %} + {% if not object.ta_publicado and not object.dispositivo_subsequente and not object.tipo_dispositivo.dispositivo_de_alteracao%}