From 4161c8c69c5a72f05495f0a2f3b483809e8494bb Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Thu, 25 Feb 2016 08:56:53 -0300 Subject: [PATCH] Refatora model choices usando django-model-utils Fix #122 --- comissoes/models.py | 9 +++--- materia/models.py | 53 ++++++++++++++--------------------- norma/models.py | 11 ++++---- parlamentares/models.py | 9 +++--- protocoloadm/models.py | 9 +++--- requirements/requirements.txt | 1 + sapl/test_utils.py | 14 +-------- sapl/utils.py | 8 ------ sessao/models.py | 11 ++++---- 9 files changed, 49 insertions(+), 76 deletions(-) diff --git a/comissoes/models.py b/comissoes/models.py index 0787d6d9b..6876debc2 100644 --- a/comissoes/models.py +++ b/comissoes/models.py @@ -1,15 +1,14 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ +from model_utils import Choices from parlamentares.models import Parlamentar -from sapl.utils import YES_NO_CHOICES, make_choices +from sapl.utils import YES_NO_CHOICES class TipoComissao(models.Model): - NATUREZA_CHOICES, TEMPORARIA, PERMANENTE = make_choices( - 'T', _('Temporária'), - 'P', _('Permanente'), - ) + NATUREZA_CHOICES = Choices(('T', 'temporaria', _('Temporária')), + ('P', 'permanente', _('Permanente'))) nome = models.CharField(max_length=50, verbose_name=_('Nome')) natureza = models.CharField( max_length=1, verbose_name=_('Natureza'), choices=NATUREZA_CHOICES) diff --git a/materia/models.py b/materia/models.py index bb2fb078d..1e9c0544a 100644 --- a/materia/models.py +++ b/materia/models.py @@ -1,9 +1,10 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ +from model_utils import Choices from comissoes.models import Comissao from parlamentares.models import Parlamentar, Partido -from sapl.utils import YES_NO_CHOICES, make_choices, xstr +from sapl.utils import YES_NO_CHOICES, xstr class TipoMateriaLegislativa(models.Model): @@ -53,11 +54,11 @@ def texto_upload_path(instance, filename): return get_materia_media_path(instance, 'materia', filename) +TIPO_APRESENTACAO_CHOICES = Choices(('O', 'oral', _('Oral')), + ('E', 'escrita', _('Escrita'))) + + class MateriaLegislativa(models.Model): - TIPO_APRESENTACAO_CHOICES, ORAL, ESCRITA = make_choices( - 'O', _('Oral'), - 'E', _('Escrita'), - ) tipo = models.ForeignKey(TipoMateriaLegislativa, verbose_name=_('Tipo')) numero = models.PositiveIntegerField(verbose_name=_('Número')) @@ -375,16 +376,11 @@ class Relatoria(models.Model): class Parecer(models.Model): - APRESENTACAO_CHOICES, ORAL, ESCRITA = make_choices( - 'O', _('Oral'), - 'E', _('Escrita'), - ) - relatoria = models.ForeignKey(Relatoria) materia = models.ForeignKey(MateriaLegislativa) tipo_conclusao = models.CharField(max_length=3, blank=True) tipo_apresentacao = models.CharField( - max_length=1, choices=APRESENTACAO_CHOICES) + max_length=1, choices=TIPO_APRESENTACAO_CHOICES) parecer = models.TextField(blank=True) class Meta: @@ -398,10 +394,8 @@ class Parecer(models.Model): class TipoProposicao(models.Model): - MAT_OU_DOC_CHOICES, MATERIA, DOCUMENTO = make_choices( - 'M', _('Matéria'), - 'D', _('Documento'), - ) + MAT_OU_DOC_CHOICES = Choices(('M', 'materia', _('Matéria')), + ('D', 'documento', _('Documento'))) descricao = models.CharField(max_length=50, verbose_name=_('Descrição')) materia_ou_documento = models.CharField( @@ -469,10 +463,8 @@ class Proposicao(models.Model): class StatusTramitacao(models.Model): - INDICADOR_CHOICES, FIM, RETORNO = make_choices( - 'F', _('Fim'), - 'R', _('Retorno'), - ) + INDICADOR_CHOICES = Choices(('F', 'fim', _('Fim')), + ('R', 'retorno', _('Retorno'))) sigla = models.CharField(max_length=10, verbose_name=_('Sigla')) descricao = models.CharField(max_length=60, verbose_name=_('Descrição')) @@ -516,19 +508,16 @@ class UnidadeTramitacao(models.Model): class Tramitacao(models.Model): - TURNO_CHOICES, \ - PRIMEIRO, SEGUNDO, UNICO, SUPLEMENTAR, FINAL, \ - VOTACAO_UNICA, PRIMEIRA_VOTACAO, \ - SEGUNDA_TERCEIRA_VOTACAO = make_choices( - 'P', _('Primeiro'), - 'S', _('Segundo'), - 'Ú', _('Único'), - 'L', _('Suplementar'), - 'F', _('Final'), - 'A', _('Votação única em Regime de Urgência'), - 'B', _('1ª Votação'), - 'C', _('2ª e 3ª Votação'), - ) + TURNO_CHOICES = Choices( + ('P', 'primeiro', _('Primeiro')), + ('S', 'segundo', _('Segundo')), + ('Ú', 'unico', _('Único')), + ('L', 'suplementar', _('Suplementar')), + ('F', 'final', _('Final')), + ('A', 'votacao_unica', _('Votação única em Regime de Urgência')), + ('B', 'primeira_votacao', _('1ª Votação')), + ('C', 'segunda_terceira_votacao', _('2ª e 3ª Votação')), + ) status = models.ForeignKey( StatusTramitacao, blank=True, null=True, verbose_name=_('Status')) diff --git a/norma/models.py b/norma/models.py index 679607cfe..a8b95431a 100644 --- a/norma/models.py +++ b/norma/models.py @@ -1,9 +1,10 @@ from django.db import models from django.template import defaultfilters from django.utils.translation import ugettext_lazy as _ +from model_utils import Choices from materia.models import MateriaLegislativa -from sapl.utils import YES_NO_CHOICES, make_choices +from sapl.utils import YES_NO_CHOICES class AssuntoNorma(models.Model): @@ -61,10 +62,10 @@ def texto_upload_path(instance, filename): class NormaJuridica(models.Model): - ESFERA_FEDERACAO_CHOICES, ESTADUAL, FEDERAL, MUNICIPAL = make_choices( - 'E', _('Estadual'), - 'F', _('Federal'), - 'M', _('Municipal'), + ESFERA_FEDERACAO_CHOICES = Choices( + ('E', 'estadual', _('Estadual')), + ('F', 'federal', _('Federal')), + ('M', 'municipal', _('Municipal')), ) texto_integral = models.FileField( blank=True, diff --git a/parlamentares/models.py b/parlamentares/models.py index aeb682d5e..71aea7feb 100644 --- a/parlamentares/models.py +++ b/parlamentares/models.py @@ -2,8 +2,9 @@ import datetime from django.db import models from django.utils.translation import ugettext_lazy as _ +from model_utils import Choices -from sapl.utils import YES_NO_CHOICES, make_choices +from sapl.utils import YES_NO_CHOICES class Legislatura(models.Model): @@ -32,9 +33,9 @@ class Legislatura(models.Model): class SessaoLegislativa(models.Model): - TIPO_SESSAO_CHOICES, ORDINARIA, EXTRAORDINARIA = make_choices( - 'O', _('Ordinária'), - 'E', _('Extraordinária'), + TIPO_SESSAO_CHOICES = Choices( + ('O', 'ordinaria', _('Ordinária')), + ('E', 'extraordinaria', _('Extraordinária')), ) legislatura = models.ForeignKey(Legislatura) diff --git a/protocoloadm/models.py b/protocoloadm/models.py index ed5834159..6b04db2b6 100644 --- a/protocoloadm/models.py +++ b/protocoloadm/models.py @@ -2,9 +2,10 @@ from uuid import uuid4 from django.db import models from django.utils.translation import ugettext_lazy as _ +from model_utils import Choices from materia.models import Autor, TipoMateriaLegislativa, UnidadeTramitacao -from sapl.utils import YES_NO_CHOICES, make_choices +from sapl.utils import YES_NO_CHOICES class TipoDocumentoAdministrativo(models.Model): @@ -126,9 +127,9 @@ class Protocolo(models.Model): class StatusTramitacaoAdministrativo(models.Model): - INDICADOR_CHOICES, FIM, RETORNO = make_choices( - 'F', _('Fim'), - 'R', _('Retorno'), + INDICADOR_CHOICES = Choices( + ('F', 'fim', _('Fim')), + ('R', 'retorno', _('Retorno')), ) sigla = models.CharField(max_length=10, verbose_name=_('Sigla')) diff --git a/requirements/requirements.txt b/requirements/requirements.txt index baf54c00b..7d80a904d 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -5,6 +5,7 @@ django-braces django-compressor django-crispy-forms django-extra-views +django-model-utils django-sass-processor django-vanilla-views django<1.9 diff --git a/sapl/test_utils.py b/sapl/test_utils.py index d76d19f9d..925b0ce8a 100644 --- a/sapl/test_utils.py +++ b/sapl/test_utils.py @@ -1,16 +1,4 @@ -from pytest import mark - -from .utils import listify, make_choices - - -@mark.parametrize("choice_pairs, result", [ - ( - ['A', 'aaa', 'B', 'bbb'], - [[('A', 'aaa'), ('B', 'bbb')], 'A', 'B'] - ), -]) -def test_make_choices(choice_pairs, result): - assert list(make_choices(*choice_pairs)) == result +from .utils import listify def test_listify(): diff --git a/sapl/utils.py b/sapl/utils.py index d86c59616..f57c90935 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -38,14 +38,6 @@ def create_barcode(value): return data.decode('utf-8') -def make_choices(*choice_pairs): - assert len(choice_pairs) % 2 == 0 - ipairs = iter(choice_pairs) - choices = list(zip(ipairs, ipairs)) - yield choices - for key, value in choices: - yield key - YES_NO_CHOICES = [(True, _('Sim')), (False, _('Não'))] diff --git a/sessao/models.py b/sessao/models.py index 15157c2aa..b1e24fc43 100644 --- a/sessao/models.py +++ b/sessao/models.py @@ -1,10 +1,11 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ +from model_utils import Choices from materia.models import MateriaLegislativa from parlamentares.models import (CargoMesa, Legislatura, Parlamentar, SessaoLegislativa) -from sapl.utils import YES_NO_CHOICES, make_choices +from sapl.utils import YES_NO_CHOICES class TipoSessaoPlenaria(models.Model): @@ -92,10 +93,10 @@ class SessaoPlenaria(models.Model): class AbstractOrdemDia(models.Model): - TIPO_VOTACAO_CHOICES, SIMBOLICA, NOMINAL, SECRETA = make_choices( - 1, _('Simbólica'), - 2, _('Nominal'), - 3, _('Secreta'), + TIPO_VOTACAO_CHOICES = Choices( + (1, 'simbolica', (('Simbólica'))), + (2, 'nominal', (('Nominal'))), + (3, 'secreta', (('Secreta'))), ) sessao_plenaria = models.ForeignKey(SessaoPlenaria)