From b1f4b7da13c8a6658fa30fdcc745bf83efcb2884 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Wed, 23 Nov 2016 15:39:07 -0200 Subject: [PATCH] Cria vinculo de normas --- sapl/norma/forms.py | 8 +-- .../migrations/0027_auto_20161123_1538.py | 26 +++++++++ sapl/norma/models.py | 10 ++-- sapl/norma/urls.py | 10 ++-- sapl/norma/views.py | 53 +++++++++++++++++-- sapl/templates/norma/layouts.yaml | 11 ++++ .../norma/normarelacionada_form.html | 27 ++++++++++ sapl/templates/norma/subnav.yaml | 2 + 8 files changed, 134 insertions(+), 13 deletions(-) create mode 100644 sapl/norma/migrations/0027_auto_20161123_1538.py create mode 100644 sapl/templates/norma/normarelacionada_form.html diff --git a/sapl/norma/forms.py b/sapl/norma/forms.py index 4c01178e3..071d959b3 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -159,12 +159,13 @@ class NormaRelacionadaForm(ModelForm): ) numero = forms.CharField(label='Número', required=True) ano = forms.CharField(label='Ano', required=True) - ementa = forms.CharField(widget=forms.Textarea) + ementa = forms.CharField( + required=False, + widget=forms.Textarea(attrs={'disabled': 'disabled'})) class Meta: model = NormaRelacionada fields = ['tipo', 'numero', 'ano', 'ementa', 'tipo_vinculo'] - widgets = {'ementa': forms.Select(attrs={'disabled': 'disabled'})} def __init__(self, *args, **kwargs): super(NormaRelacionadaForm, self).__init__(*args, **kwargs) @@ -175,10 +176,9 @@ class NormaRelacionadaForm(ModelForm): cleaned_data = self.cleaned_data try: - norma_relacionada = MateriaLegislativa.objects.get( + norma_relacionada = NormaJuridica.objects.get( numero=cleaned_data['numero'], ano=cleaned_data['ano'], - ementa=cleaned_data['ementa'], tipo=cleaned_data['tipo']) except ObjectDoesNotExist: msg = _('A norma a ser relacionada não existe.') diff --git a/sapl/norma/migrations/0027_auto_20161123_1538.py b/sapl/norma/migrations/0027_auto_20161123_1538.py new file mode 100644 index 000000000..f44348b0e --- /dev/null +++ b/sapl/norma/migrations/0027_auto_20161123_1538.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.7 on 2016-11-23 15:38 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('norma', '0026_auto_20161123_1450'), + ] + + operations = [ + migrations.AlterField( + model_name='normarelacionada', + name='norma_principal', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='norma_principal', to='norma.NormaJuridica', verbose_name='Norma Principal'), + ), + migrations.AlterField( + model_name='normarelacionada', + name='norma_relacionada', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='norma_relacionada', to='norma.NormaJuridica', verbose_name='Norma Relacionada'), + ), + ] diff --git a/sapl/norma/models.py b/sapl/norma/models.py index 517209009..49e85182c 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -186,11 +186,15 @@ class VinculoNormaJuridica(models.Model): class NormaRelacionada(models.Model): norma_principal = models.ForeignKey( - NormaJuridica, related_name='norma_principal') + NormaJuridica, + related_name='norma_principal', + verbose_name=_('Norma Principal')) norma_relacionada = models.ForeignKey( - NormaJuridica, related_name='norma_relacionada') + NormaJuridica, + related_name='norma_relacionada', + verbose_name=_('Norma Relacionada')) tipo_vinculo = models.ForeignKey( - VinculoNormaJuridica, verbose_name='Tipo de Vínculo') + VinculoNormaJuridica, verbose_name=_('Tipo de Vínculo')) class Meta: verbose_name = _('Norma Relacionada') diff --git a/sapl/norma/urls.py b/sapl/norma/urls.py index dfbc34f83..59d8f3473 100644 --- a/sapl/norma/urls.py +++ b/sapl/norma/urls.py @@ -1,8 +1,8 @@ from django.conf.urls import include, url from sapl.norma.views import (AssuntoNormaCrud, NormaCrud, NormaPesquisaView, - NormaTaView, TipoNormaCrud, - VinculoNormaJuridicaCrud) + NormaRelacionadaCrud, NormaTaView, TipoNormaCrud, + VinculoNormaJuridicaCrud, recuperar_norma) from .apps import AppConfig @@ -10,7 +10,8 @@ app_name = AppConfig.name urlpatterns = [ - url(r'^norma/', include(NormaCrud.get_urls())), + url(r'^norma/', include(NormaCrud.get_urls() + + NormaRelacionadaCrud.get_urls())), # Integração com Compilação url(r'^norma/(?P[0-9]+)/ta$', NormaTaView.as_view(), name='norma_ta'), @@ -22,4 +23,7 @@ urlpatterns = [ url(r'^norma/pesquisar$', NormaPesquisaView.as_view(), name='norma_pesquisa'), + + url(r'^norma/recuperar-norma/', recuperar_norma), + ] diff --git a/sapl/norma/views.py b/sapl/norma/views.py index 610e4ba64..8f90190e4 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -1,6 +1,7 @@ from datetime import datetime from django.core.urlresolvers import reverse +from django.http import JsonResponse from django.shortcuts import redirect from django.utils.translation import ugettext_lazy as _ from django.views.generic import FormView, ListView @@ -12,9 +13,9 @@ from sapl.compilacao.views import IntegracaoTaView from sapl.crud.base import (RP_DETAIL, RP_LIST, Crud, CrudAux, MasterDetailCrud, make_pagination) -from .forms import NormaFilterSet, NormaJuridicaForm -from .models import (AssuntoNorma, NormaJuridica, TipoNormaJuridica, - VinculoNormaJuridica) +from .forms import NormaFilterSet, NormaJuridicaForm, NormaRelacionadaForm +from .models import (AssuntoNorma, NormaJuridica, NormaRelacionada, + TipoNormaJuridica, VinculoNormaJuridica) # LegislacaoCitadaCrud = Crud.build(LegislacaoCitada, '') AssuntoNormaCrud = CrudAux.build(AssuntoNorma, 'assunto_norma_juridica', @@ -28,6 +29,35 @@ VinculoNormaJuridicaCrud = CrudAux.build( VinculoNormaJuridica, '', list_field_names=['sigla', 'descricao']) +class NormaRelacionadaCrud(MasterDetailCrud): + model = NormaRelacionada + parent_field = 'norma_principal' + help_path = '' + public = [RP_LIST, RP_DETAIL] + + class BaseMixin(MasterDetailCrud.BaseMixin): + list_field_names = ['norma_relacionada'] + + class CreateView(MasterDetailCrud.CreateView): + form_class = NormaRelacionadaForm + + class UpdateView(MasterDetailCrud.UpdateView): + form_class = NormaRelacionadaForm + + def get_initial(self): + self.initial['tipo'] = self.object.norma_relacionada.tipo.id + self.initial['numero'] = self.object.norma_relacionada.numero + self.initial['ano'] = self.object.norma_relacionada.ano + self.initial['ementa'] = self.object.norma_relacionada.ementa + return self.initial + + class DetailView(MasterDetailCrud.DetailView): + + @property + def layout_key(self): + return 'NormaRelacionadaDetail' + + class NormaPesquisaView(FilterView): model = NormaJuridica filterset_class = NormaFilterSet @@ -132,3 +162,20 @@ class NormaCrud(Crud): self.initial['ano_materia'] = norma.materia.ano self.initial['numero_materia'] = norma.materia.numero return self.initial.copy() + + +def recuperar_norma(request): + tipo = TipoNormaJuridica.objects.get(pk=request.GET['tipo']) + numero = request.GET['numero'] + ano = request.GET['ano'] + + try: + norma = NormaJuridica.objects.get(tipo=tipo, + ano=ano, + numero=numero) + response = JsonResponse({'ementa': norma.ementa, + 'id': norma.id}) + except ObjectDoesNotExist: + response = JsonResponse({'ementa': '', 'id': 0}) + + return response diff --git a/sapl/templates/norma/layouts.yaml b/sapl/templates/norma/layouts.yaml index a00f1d5d3..52d16d587 100644 --- a/sapl/templates/norma/layouts.yaml +++ b/sapl/templates/norma/layouts.yaml @@ -53,3 +53,14 @@ LegislacaoCitadaDetail: VinculoNormaJuridica: {% trans 'Tipo de Vínculo entre Normas Jurídicas' %}: - sigla:2 descricao + +NormaRelacionada: + {% trans 'Norma Relacionada' %}: + - tipo numero ano + - tipo_vinculo + - ementa + +NormaRelacionadaDetail: + {% trans 'Norma Relacionada' %}: + - norma_relacionada + - tipo_vinculo diff --git a/sapl/templates/norma/normarelacionada_form.html b/sapl/templates/norma/normarelacionada_form.html new file mode 100644 index 000000000..300f7e3b3 --- /dev/null +++ b/sapl/templates/norma/normarelacionada_form.html @@ -0,0 +1,27 @@ +{% extends "crud/form.html" %} +{% load i18n %} +{% load crispy_forms_tags %} +{% load common_tags %} + +{% block extra_js %} + +{% endblock %} diff --git a/sapl/templates/norma/subnav.yaml b/sapl/templates/norma/subnav.yaml index be3cf3c0a..0599efdfb 100644 --- a/sapl/templates/norma/subnav.yaml +++ b/sapl/templates/norma/subnav.yaml @@ -2,6 +2,8 @@ - title: {% trans 'Início' %} url: normajuridica_detail +- title: {% trans 'Normas Relacionadas' %} + url: normarelacionada_list # Opção adicionada para chamar o TextoArticulado da norma. # para integração foram necessárias apenas criar a url norma_ta em urls.py