diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py index 5dc0d0920..468426a13 100644 --- a/sapl/materia/forms.py +++ b/sapl/materia/forms.py @@ -8,8 +8,7 @@ from crispy_forms.layout import (HTML, Button, Column, Div, Field, Fieldset, Layout, Submit) from django import forms from django.contrib.contenttypes.models import ContentType -from django.core.exceptions import ObjectDoesNotExist, ValidationError,\ - NON_FIELD_ERRORS +from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.files.base import File from django.core.urlresolvers import reverse from django.db import models, transaction @@ -820,8 +819,12 @@ class TipoProposicaoForm(ModelForm): ) % (cd['tipo_conteudo_related'], content_type)) unique_value = self._meta.model.objects.filter( - content_type=content_type, object_id=cd['tipo_conteudo_related'] - ).first() + content_type=content_type, object_id=cd['tipo_conteudo_related']) + + if self.instance.pk: + unique_value = unique_value.exclude(pk=self.instance.pk) + + unique_value = unique_value.first() if unique_value: raise ValidationError( @@ -831,7 +834,7 @@ class TipoProposicaoForm(ModelForm): content_type, unique_value.tipo_conteudo_related)) - return self.cleaned_data + return super().clean() @transaction.atomic def save(self, commit=False): diff --git a/sapl/materia/migrations/0007_auto_20170620_1252.py b/sapl/materia/migrations/0007_auto_20170620_1252.py new file mode 100644 index 000000000..6d19ee4da --- /dev/null +++ b/sapl/materia/migrations/0007_auto_20170620_1252.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.13 on 2017-06-20 12:52 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('materia', '0006_merge'), + ] + + operations = [ + migrations.AlterField( + model_name='tipoproposicao', + name='descricao', + field=models.CharField(error_messages={'unique': 'Já existe um Tipo de Proposição com esta descrição.'}, max_length=50, unique=True, verbose_name='Descrição'), + ), + ] diff --git a/sapl/materia/models.py b/sapl/materia/models.py index 6b02f2f14..f6d9ab619 100644 --- a/sapl/materia/models.py +++ b/sapl/materia/models.py @@ -34,7 +34,13 @@ def grupo_autor(): @reversion.register() class TipoProposicao(models.Model): - descricao = models.CharField(max_length=50, verbose_name=_('Descrição')) + descricao = models.CharField( + max_length=50, + verbose_name=_('Descrição'), + unique=True, + error_messages={ + 'unique': _('Já existe um Tipo de Proposição com esta descrição.') + }) # FIXME - para a rotina de migração - estes campos mudaram # retire o comentário quando resolver