Browse Source

Cria form para relacionamento de normas

pull/814/head
Eduardo Edson Batista Cordeiro Alves 8 years ago
parent
commit
176947cb4e
  1. 49
      sapl/norma/forms.py
  2. 29
      sapl/norma/migrations/0025_normarelacionada.py
  3. 21
      sapl/norma/migrations/0026_auto_20161123_1450.py
  4. 19
      sapl/norma/models.py

49
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

29
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',
},
),
]

21
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'),
),
]

19
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}

Loading…
Cancel
Save