From 176947cb4e355051f2e0f0e4b3f1b1e78986d213 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Wed, 23 Nov 2016 15:06:32 -0200 Subject: [PATCH] Cria form para relacionamento de normas --- sapl/norma/forms.py | 49 ++++++++++++++++++- .../norma/migrations/0025_normarelacionada.py | 29 +++++++++++ .../migrations/0026_auto_20161123_1450.py | 21 ++++++++ sapl/norma/models.py | 19 +++++++ 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 sapl/norma/migrations/0025_normarelacionada.py create mode 100644 sapl/norma/migrations/0026_auto_20161123_1450.py diff --git a/sapl/norma/forms.py b/sapl/norma/forms.py index 7ae6b85ba..4c01178e3 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -14,7 +14,8 @@ from sapl.materia.models import MateriaLegislativa, TipoMateriaLegislativa from sapl.settings import MAX_DOC_UPLOAD_SIZE from sapl.utils import RANGE_ANOS, RangeWidgetOverride -from .models import AssuntoNorma, NormaJuridica +from .models import (AssuntoNorma, NormaJuridica, NormaRelacionada, + TipoNormaJuridica) def get_esferas(): @@ -146,3 +147,49 @@ class NormaJuridicaForm(ModelForm): norma.materia = self.cleaned_data['materia'] norma = super(NormaJuridicaForm, self).save(commit=True) return norma + + +class NormaRelacionadaForm(ModelForm): + + tipo = forms.ModelChoiceField( + label='Tipo', + required=True, + queryset=TipoNormaJuridica.objects.all(), + empty_label='----------', + ) + numero = forms.CharField(label='Número', required=True) + ano = forms.CharField(label='Ano', required=True) + ementa = forms.CharField(widget=forms.Textarea) + + 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) + + def clean(self): + if self.errors: + return self.errors + cleaned_data = self.cleaned_data + + try: + norma_relacionada = MateriaLegislativa.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.') + raise ValidationError(msg) + else: + cleaned_data['norma_relacionada'] = norma_relacionada + + return cleaned_data + + def save(self, commit=False): + relacionada = super(NormaRelacionadaForm, self).save(commit) + relacionada.norma_relacionada = self.cleaned_data['norma_relacionada'] + relacionada.save() + return relacionada diff --git a/sapl/norma/migrations/0025_normarelacionada.py b/sapl/norma/migrations/0025_normarelacionada.py new file mode 100644 index 000000000..1cfafedb2 --- /dev/null +++ b/sapl/norma/migrations/0025_normarelacionada.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.7 on 2016-11-23 14:44 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('norma', '0024_auto_20161123_1430'), + ] + + operations = [ + migrations.CreateModel( + name='NormaRelacionada', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('norma_principal', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='norma_principal', to='norma.NormaJuridica')), + ('norma_relacionada', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='norma_relacionada', to='norma.NormaJuridica')), + ('tipo_vinculo', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='norma.VinculoNormaJuridica')), + ], + options={ + 'verbose_name': 'Norma Relacionada', + 'verbose_name_plural': 'Normas Relacionadas', + }, + ), + ] diff --git a/sapl/norma/migrations/0026_auto_20161123_1450.py b/sapl/norma/migrations/0026_auto_20161123_1450.py new file mode 100644 index 000000000..6fda19247 --- /dev/null +++ b/sapl/norma/migrations/0026_auto_20161123_1450.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.7 on 2016-11-23 14:50 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('norma', '0025_normarelacionada'), + ] + + operations = [ + migrations.AlterField( + model_name='normarelacionada', + name='tipo_vinculo', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='norma.VinculoNormaJuridica', verbose_name='Tipo de Vínculo'), + ), + ] diff --git a/sapl/norma/models.py b/sapl/norma/models.py index 484ccd99d..517209009 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -182,3 +182,22 @@ class VinculoNormaJuridica(models.Model): def __str__(self): return self.descricao + + +class NormaRelacionada(models.Model): + norma_principal = models.ForeignKey( + NormaJuridica, related_name='norma_principal') + norma_relacionada = models.ForeignKey( + NormaJuridica, related_name='norma_relacionada') + tipo_vinculo = models.ForeignKey( + VinculoNormaJuridica, verbose_name='Tipo de Vínculo') + + class Meta: + verbose_name = _('Norma Relacionada') + verbose_name_plural = _('Normas Relacionadas') + + def __str__(self): + return _('Principal: %(norma_principal)s' + ' - Relacionada: %(norma_relacionada)s') % { + 'norma_principal': self.norma_principal, + 'norma_relacionada': self.norma_relacionada}