From b620f218c044f74d7c5f40ba7f9b4a777361d04b Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Wed, 26 Oct 2016 15:18:35 -0200 Subject: [PATCH 1/4] Torna assunto norma um MasterDetail --- sapl/norma/forms.py | 23 ++++++++++++++++++++++- sapl/norma/models.py | 7 +++++++ sapl/norma/urls.py | 9 +++++---- sapl/norma/views.py | 17 +++++++++++++++-- sapl/templates/norma/layouts.yaml | 5 +++++ sapl/templates/norma/subnav.yaml | 4 ++++ 6 files changed, 58 insertions(+), 7 deletions(-) diff --git a/sapl/norma/forms.py b/sapl/norma/forms.py index c542f12b6..70eaca1b5 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -8,7 +8,8 @@ from django.forms import ModelForm from django.utils.translation import ugettext_lazy as _ from sapl.crispy_layout_mixin import form_actions, to_row -from sapl.materia.models import MateriaLegislativa, TipoMateriaLegislativa +from sapl.materia.models import (AssuntoNormaRelationship, MateriaLegislativa, + TipoMateriaLegislativa) from sapl.settings import MAX_DOC_UPLOAD_SIZE from sapl.utils import RANGE_ANOS @@ -30,6 +31,26 @@ ORDENACAO_CHOICES = [('', '---------'), ('data,tipo,ano,numero', _('Data/Tipo/Ano/Número'))] +class AssuntoNormaRelationshipForm(ModelForm): + + class Meta: + model = AssuntoNormaRelationship + fields = ['assunto'] + + def clean(self): + cleaned_data = self.cleaned_data + import ipdb; ipdb.set_trace() + # try: + # comissao = Comissao.objects.get(id=self.initial['comissao']) + # except ObjectDoesNotExist: + # msg = _('A localização atual deve ser uma comissão.') + # raise ValidationError(msg) + # else: + # cleaned_data['comissao'] = comissao + + return cleaned_data + + # TODO termos, pesquisa textual, assunto(M2M) class NormaJuridicaPesquisaForm(ModelForm): diff --git a/sapl/norma/models.py b/sapl/norma/models.py index f094df55f..6a88668a1 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -143,6 +143,13 @@ class AssuntoNormaRelationship(models.Model): ('assunto', 'norma'), ) + class Meta: + verbose_name = _('Assunto') + verbose_name_plural = _('Assuntos') + + def __str__(self): + return self.assunto + class LegislacaoCitada(models.Model): materia = models.ForeignKey(MateriaLegislativa) diff --git a/sapl/norma/urls.py b/sapl/norma/urls.py index a042e0236..f911c917f 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, PesquisaNormaListView, - TipoNormaCrud) +from sapl.norma.views import (AssuntoNormaCrud, AssuntoNormaRelationshipCrud, + NormaCrud, NormaPesquisaView, NormaTaView, + PesquisaNormaListView, TipoNormaCrud) 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() + + AssuntoNormaRelationshipCrud.get_urls())), # Integração com Compilação url(r'^norma/(?P[0-9]+)/ta$', NormaTaView.as_view(), name='norma_ta'), diff --git a/sapl/norma/views.py b/sapl/norma/views.py index a8ca956ec..05cbaf092 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -7,11 +7,13 @@ from django.views.generic.base import RedirectView from sapl.base.models import AppConfig from sapl.compilacao.views import IntegracaoTaView -from sapl.crud.base import RP_DETAIL, RP_LIST, Crud, CrudAux, make_pagination +from sapl.crud.base import (RP_DETAIL, RP_LIST, Crud, CrudAux, + MasterDetailCrud, make_pagination) from sapl.norma.forms import NormaJuridicaForm from .forms import NormaJuridicaPesquisaForm -from .models import AssuntoNorma, NormaJuridica, TipoNormaJuridica +from .models import (AssuntoNorma, AssuntoNormaRelationship, NormaJuridica, + TipoNormaJuridica) # LegislacaoCitadaCrud = Crud.build(LegislacaoCitada, '') AssuntoNormaCrud = CrudAux.build(AssuntoNorma, 'assunto_norma_juridica', @@ -39,6 +41,17 @@ class NormaTaView(IntegracaoTaView): return self.get_redirect_deactivated() +class AssuntoNormaRelationshipCrud(MasterDetailCrud): + model = AssuntoNormaRelationship + parent_field = 'norma' + help_path = '' + public = [RP_LIST, RP_DETAIL] + + class BaseMixin(MasterDetailCrud.BaseMixin): + list_field_names = ['assunto'] + ordering = 'assunto' + + class NormaCrud(Crud): model = NormaJuridica help_path = 'norma_juridica' diff --git a/sapl/templates/norma/layouts.yaml b/sapl/templates/norma/layouts.yaml index e7eff947e..30ad24dc8 100644 --- a/sapl/templates/norma/layouts.yaml +++ b/sapl/templates/norma/layouts.yaml @@ -1,4 +1,9 @@ {% load i18n %} + +AssuntoNormaRelationship: + {% trans 'Assunto Norma Jurídica' %}: + - assunto + AssuntoNorma: {% trans 'Assunto Norma Jurídica' %}: - assunto descricao diff --git a/sapl/templates/norma/subnav.yaml b/sapl/templates/norma/subnav.yaml index d35607335..465585e41 100644 --- a/sapl/templates/norma/subnav.yaml +++ b/sapl/templates/norma/subnav.yaml @@ -1,11 +1,15 @@ {% load i18n common_tags%} + - title: {% trans 'Início' %} url: normajuridica_detail +- title: {% trans 'Assuntos' %} + url: assuntonormarelationship_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 # e a view NormaTaView(IntegracaoTaView) em views.py # Em nada mais a integração interfere em NormaJuridica + {% if 'texto_articulado_norma'|get_config_attr %} - title: {% trans 'Texto' %} url: norma_ta From 9d926bd7eb215268587553a8579089667c8379c6 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Thu, 27 Oct 2016 14:31:45 -0200 Subject: [PATCH 2/4] Valida assuntos repetidos --- sapl/norma/forms.py | 29 +++++++++---------- .../migrations/0016_auto_20161027_1419.py | 23 +++++++++++++++ sapl/norma/models.py | 2 +- sapl/norma/views.py | 8 ++++- 4 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 sapl/norma/migrations/0016_auto_20161027_1419.py diff --git a/sapl/norma/forms.py b/sapl/norma/forms.py index 70eaca1b5..cc9b0569c 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -8,12 +8,11 @@ from django.forms import ModelForm from django.utils.translation import ugettext_lazy as _ from sapl.crispy_layout_mixin import form_actions, to_row -from sapl.materia.models import (AssuntoNormaRelationship, MateriaLegislativa, - TipoMateriaLegislativa) +from sapl.materia.models import MateriaLegislativa, TipoMateriaLegislativa from sapl.settings import MAX_DOC_UPLOAD_SIZE from sapl.utils import RANGE_ANOS -from .models import NormaJuridica +from .models import AssuntoNormaRelationship, NormaJuridica def get_esferas(): @@ -37,18 +36,18 @@ class AssuntoNormaRelationshipForm(ModelForm): model = AssuntoNormaRelationship fields = ['assunto'] - def clean(self): - cleaned_data = self.cleaned_data - import ipdb; ipdb.set_trace() - # try: - # comissao = Comissao.objects.get(id=self.initial['comissao']) - # except ObjectDoesNotExist: - # msg = _('A localização atual deve ser uma comissão.') - # raise ValidationError(msg) - # else: - # cleaned_data['comissao'] = comissao - - return cleaned_data + def save(self, commit=False): + norma_assunto = super(AssuntoNormaRelationshipForm, self).save(commit) + try: + AssuntoNormaRelationship.objects.get( + norma=norma_assunto.norma, + assunto=norma_assunto.assunto) + except ObjectDoesNotExist: + norma_assunto.save() + else: + raise forms.ValidationError( + "Esse Assunto já está anexado nesta Norma Jurídica.") + return norma_assunto # TODO termos, pesquisa textual, assunto(M2M) diff --git a/sapl/norma/migrations/0016_auto_20161027_1419.py b/sapl/norma/migrations/0016_auto_20161027_1419.py new file mode 100644 index 000000000..b090e538a --- /dev/null +++ b/sapl/norma/migrations/0016_auto_20161027_1419.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.7 on 2016-10-27 14:19 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('norma', '0015_auto_20160929_1635'), + ] + + operations = [ + migrations.AlterModelOptions( + name='assuntonormarelationship', + options={'verbose_name': 'Assunto', 'verbose_name_plural': 'Assuntos'}, + ), + migrations.AlterUniqueTogether( + name='assuntonormarelationship', + unique_together=set([]), + ), + ] diff --git a/sapl/norma/models.py b/sapl/norma/models.py index 6a88668a1..7b8b035a6 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -148,7 +148,7 @@ class AssuntoNormaRelationship(models.Model): verbose_name_plural = _('Assuntos') def __str__(self): - return self.assunto + return self.assunto.assunto class LegislacaoCitada(models.Model): diff --git a/sapl/norma/views.py b/sapl/norma/views.py index 05cbaf092..7e6e195d0 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -11,7 +11,7 @@ from sapl.crud.base import (RP_DETAIL, RP_LIST, Crud, CrudAux, MasterDetailCrud, make_pagination) from sapl.norma.forms import NormaJuridicaForm -from .forms import NormaJuridicaPesquisaForm +from .forms import AssuntoNormaRelationshipForm, NormaJuridicaPesquisaForm from .models import (AssuntoNorma, AssuntoNormaRelationship, NormaJuridica, TipoNormaJuridica) @@ -51,6 +51,12 @@ class AssuntoNormaRelationshipCrud(MasterDetailCrud): list_field_names = ['assunto'] ordering = 'assunto' + class CreateView(MasterDetailCrud.CreateView): + form_class = AssuntoNormaRelationshipForm + + class UpdateView(MasterDetailCrud.UpdateView): + form_class = AssuntoNormaRelationshipForm + class NormaCrud(Crud): model = NormaJuridica From 268a666577304b1115f3f7204c99d136d1264c52 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Thu, 27 Oct 2016 14:33:15 -0200 Subject: [PATCH 3/4] Adiciona verbose name no campo assuntos --- .../migrations/0017_auto_20161027_1432.py | 20 +++++++++++++++++++ sapl/norma/models.py | 1 + sapl/templates/norma/layouts.yaml | 1 + 3 files changed, 22 insertions(+) create mode 100644 sapl/norma/migrations/0017_auto_20161027_1432.py diff --git a/sapl/norma/migrations/0017_auto_20161027_1432.py b/sapl/norma/migrations/0017_auto_20161027_1432.py new file mode 100644 index 000000000..93208eb69 --- /dev/null +++ b/sapl/norma/migrations/0017_auto_20161027_1432.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.7 on 2016-10-27 14:32 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('norma', '0016_auto_20161027_1419'), + ] + + operations = [ + migrations.AlterField( + model_name='normajuridica', + name='assuntos', + field=models.ManyToManyField(through='norma.AssuntoNormaRelationship', to='norma.AssuntoNorma', verbose_name='Assuntos'), + ), + ] diff --git a/sapl/norma/models.py b/sapl/norma/models.py index 7b8b035a6..7a3fdbc3e 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -98,6 +98,7 @@ class NormaJuridica(models.Model): # XXX was a CharField (attention on migrate) assuntos = models.ManyToManyField( AssuntoNorma, + verbose_name=_('Assuntos'), through='AssuntoNormaRelationship') data_vigencia = models.DateField(blank=True, null=True) timestamp = models.DateTimeField() diff --git a/sapl/templates/norma/layouts.yaml b/sapl/templates/norma/layouts.yaml index 30ad24dc8..f52d65102 100644 --- a/sapl/templates/norma/layouts.yaml +++ b/sapl/templates/norma/layouts.yaml @@ -22,6 +22,7 @@ NormaJuridica: - ementa - indexacao - observacao + - assuntos NormaJuridicaCreate: {% trans 'Identificação Básica' %}: From 189bb5803a698b26d7302ab89424fd3c0a697aad Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Thu, 27 Oct 2016 14:35:01 -0200 Subject: [PATCH 4/4] Adiciona verbose name nos campos de AssuntoNormaRelationship --- .../migrations/0018_auto_20161027_1434.py | 26 +++++++++++++++++++ sapl/norma/models.py | 4 +-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 sapl/norma/migrations/0018_auto_20161027_1434.py diff --git a/sapl/norma/migrations/0018_auto_20161027_1434.py b/sapl/norma/migrations/0018_auto_20161027_1434.py new file mode 100644 index 000000000..c70998631 --- /dev/null +++ b/sapl/norma/migrations/0018_auto_20161027_1434.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.7 on 2016-10-27 14:34 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('norma', '0017_auto_20161027_1432'), + ] + + operations = [ + migrations.AlterField( + model_name='assuntonormarelationship', + name='assunto', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='norma.AssuntoNorma', verbose_name='Assunto'), + ), + migrations.AlterField( + model_name='assuntonormarelationship', + name='norma', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='norma.NormaJuridica', verbose_name='Norma'), + ), + ] diff --git a/sapl/norma/models.py b/sapl/norma/models.py index 7a3fdbc3e..23ba2d761 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -136,8 +136,8 @@ class NormaJuridica(models.Model): class AssuntoNormaRelationship(models.Model): - assunto = models.ForeignKey(AssuntoNorma) - norma = models.ForeignKey(NormaJuridica) + assunto = models.ForeignKey(AssuntoNorma, verbose_name=_('Assunto')) + norma = models.ForeignKey(NormaJuridica, verbose_name=_('Norma')) class Meta: unique_together = (