diff --git a/sapl/norma/forms.py b/sapl/norma/forms.py index 0ba92b737..a2e6be9f0 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -1,5 +1,6 @@ import logging +import re from sapl.crispy_layout_mixin import SaplFormHelper from crispy_forms.layout import Fieldset, Layout @@ -142,12 +143,12 @@ class NormaJuridicaForm(FileFieldCheckMixin, ModelForm): def clean(self): - cleaned_data = super(NormaJuridicaForm, self).clean() + cleaned_data = super().clean() + cleaned_data['numero_pesquisa'] = list(map(lambda x: str(int(x)) if x.isnumeric() else x, re.split('\W+', cleaned_data['numero']))) if not self.is_valid(): return cleaned_data - import re has_digits = re.sub('[^0-9]', '', cleaned_data['numero']) if not has_digits: self.logger.error("Número de norma ({}) não pode conter somente letras.".format( @@ -213,7 +214,8 @@ class NormaJuridicaForm(FileFieldCheckMixin, ModelForm): norma = self.instance norma.timestamp = timezone.now() norma.materia = self.cleaned_data['materia'] - norma = super(NormaJuridicaForm, self).save(commit=True) + norma.numero_pesquisa = self.cleaned_data['numero_pesquisa'] + norma = super().save(commit=True) return norma diff --git a/sapl/norma/migrations/0025_normajuridica_numero_pesquisa.py b/sapl/norma/migrations/0025_normajuridica_numero_pesquisa.py new file mode 100644 index 000000000..06244a000 --- /dev/null +++ b/sapl/norma/migrations/0025_normajuridica_numero_pesquisa.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-05-08 14:40 +from __future__ import unicode_literals + +import re + +import django.contrib.postgres.fields +from django.db import migrations, models + + +def cria_numero_pesquisa(apps, schema_editor): + NormaJuridica = apps.get_model('norma', 'NormaJuridica') + for norma in NormaJuridica.objects.all(): + norma.numero_pesquisa = list(map(lambda x: str(int(x)) if x.isnumeric() else x, re.split('\W+', norma.numero))) + norma.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('norma', '0024_auto_20190425_0917'), + ] + + operations = [ + migrations.AddField( + model_name='normajuridica', + name='numero_pesquisa', + field=django.contrib.postgres.fields.ArrayField( + base_field=models.CharField(max_length=8, verbose_name='Número para Pesquisa'), default=[], size=8), + ), + + migrations.RunPython(cria_numero_pesquisa), + + migrations.AlterField( + model_name='normajuridica', + name='numero_pesquisa', + field=django.contrib.postgres.fields.ArrayField( + base_field=models.CharField(max_length=8, verbose_name='Número para Pesquisa'), size=8), + ), + ] diff --git a/sapl/norma/models.py b/sapl/norma/models.py index 52e797165..84f85f191 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -1,4 +1,5 @@ from django.contrib.contenttypes.fields import GenericRelation +from django.contrib.postgres.fields import ArrayField from django.db import models from django.template import defaultfilters from django.utils.translation import ugettext_lazy as _ @@ -91,6 +92,13 @@ class NormaJuridica(models.Model): numero = models.CharField( max_length=8, verbose_name=_('Número')) + numero_pesquisa = ArrayField( + models.CharField( + max_length=8, + verbose_name=_('Número para Pesquisa') + ), + size=8, + ) ano = models.PositiveSmallIntegerField(verbose_name=_('Ano'), choices=RANGE_ANOS) esfera_federacao = models.CharField( diff --git a/sapl/norma/tests/test_norma.py b/sapl/norma/tests/test_norma.py index 277ed47ed..e01f0ab59 100644 --- a/sapl/norma/tests/test_norma.py +++ b/sapl/norma/tests/test_norma.py @@ -22,6 +22,7 @@ def test_incluir_norma_submit(admin_client): response = admin_client.post(reverse('sapl.norma:normajuridica_create'), {'tipo': tipo.pk, 'numero': '1', + 'numero_pesquisa': ['1'], 'ano': '2016', 'data': '2016-03-22', 'esfera_federacao': 'E', @@ -47,6 +48,8 @@ def test_incluir_norma_errors(admin_client): [_('Este campo é obrigatório.')]) assert (response.context_data['form'].errors['numero'] == [_('Este campo é obrigatório.')]) + assert (response.context_data['form'].errors['numero_pesquisa'] == + [_('Este campo é obrigatório.')]) assert (response.context_data['form'].errors['ano'] == [_('Este campo é obrigatório.')]) assert (response.context_data['form'].errors['data'] == @@ -69,6 +72,7 @@ def test_norma_form_invalida(): assert errors['tipo'] == [_('Este campo é obrigatório.')] assert errors['numero'] == [_('Este campo é obrigatório.')] + assert errors['numero_pesquisa'] == [_('Este campo é obrigatório.')] assert errors['ano'] == [_('Este campo é obrigatório.')] assert errors['data'] == [_('Este campo é obrigatório.')] assert errors['esfera_federacao'] == [_('Este campo é obrigatório.')] @@ -92,6 +96,7 @@ def test_norma_juridica_materia_inexistente(): form = NormaJuridicaForm(data={'tipo': str(tipo.pk), 'numero': '1', + 'numero_pesquisa': ['1'], 'ano': '2017', 'data': '2017-12-12', 'esfera_federacao': 'F', @@ -118,6 +123,7 @@ def test_norma_juridica_materia_existente(): form = NormaJuridicaForm(data={'tipo': str(tipo.pk), 'numero': '1', + 'numero_pesquisa': ['1'], 'ano': '2017', 'data': '2017-12-12', 'esfera_federacao': 'F', diff --git a/sapl/templates/norma/normajuridica_filter.html b/sapl/templates/norma/normajuridica_filter.html index cad3c5f8f..8a636d76c 100644 --- a/sapl/templates/norma/normajuridica_filter.html +++ b/sapl/templates/norma/normajuridica_filter.html @@ -95,17 +95,3 @@ {% block table_content %} {% endblock table_content %} - -{% block extra_js %} - -{% endblock extra_js %} \ No newline at end of file diff --git a/sapl/templates/norma/normajuridica_form.html b/sapl/templates/norma/normajuridica_form.html index 91531c4d1..09ed3524d 100644 --- a/sapl/templates/norma/normajuridica_form.html +++ b/sapl/templates/norma/normajuridica_form.html @@ -86,7 +86,7 @@ let ano = $("select#id_ano.select").val(); let data_apresentacao = $("input#id_data.dateinput").val(); let ano_apresentacao = data_apresentacao.substr(data_apresentacao.length - 4); - + if(ano && ano_apresentacao && ano_apresentacao != ano){ $('#fundo_modal').fadeIn(); }