From 36d1829fc4744752267d0c9a2cd745190c85d901 Mon Sep 17 00:00:00 2001 From: eduardocalil Date: Thu, 19 May 2016 08:08:19 -0300 Subject: [PATCH] Fix #363 coligacao (#472) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Permite a adicao de partidos a composicao e a listagem deles * Permite a adicao de partidos a composicao e a listagem deles * Modifica o verbose name do partido de ComposicaoColigacao no models * Ordena as coligacoes * Impede que adicione um partido já existente na coligacao --- base/tests/test_login.py | 10 ++-- parlamentares/forms.py | 23 +++++++++- parlamentares/layouts.yaml | 4 ++ .../migrations/0019_auto_20160518_1136.py | 21 +++++++++ .../migrations/0020_auto_20160518_1201.py | 21 +++++++++ parlamentares/models.py | 3 +- parlamentares/urls.py | 8 ++-- parlamentares/views.py | 46 +++++++++++++++---- templates/parlamentares/coligacao_detail.html | 10 ++++ templates/parlamentares/subnav.yaml | 1 + 10 files changed, 129 insertions(+), 18 deletions(-) create mode 100644 parlamentares/migrations/0019_auto_20160518_1136.py create mode 100644 parlamentares/migrations/0020_auto_20160518_1201.py create mode 100644 templates/parlamentares/coligacao_detail.html diff --git a/base/tests/test_login.py b/base/tests/test_login.py index 3fe9301dd..819d027a4 100755 --- a/base/tests/test_login.py +++ b/base/tests/test_login.py @@ -12,15 +12,15 @@ def user(): def test_login_aparece_na_barra_para_usuario_nao_logado(client): response = client.get('/') - assert 'Login' in str(response.content) + assert 'Login' in str(response.content) def test_username_do_usuario_logado_aparece_na_barra(client, user): assert client.login(username='jfirmino', password='123') response = client.get('/') - assert 'Login' not in str(response.content) + assert 'Login' not in str(response.content) assert 'jfirmino' in str(response.content) - assert 'Sair' in str(response.content) + assert 'Sair' in str(response.content) def test_nome_completo_do_usuario_logado_aparece_na_barra(client, user): @@ -30,9 +30,9 @@ def test_nome_completo_do_usuario_logado_aparece_na_barra(client, user): user.save() assert client.login(username='jfirmino', password='123') response = client.get('/') - assert 'Login' not in str(response.content) + assert 'Login' not in str(response.content) assert 'Joao Firmino' in str(response.content) - assert 'Sair' in str(response.content) + assert 'Sair' in str(response.content) @pytest.mark.urls('base.tests.teststub_urls') diff --git a/parlamentares/forms.py b/parlamentares/forms.py index 42af57529..c19b3cb48 100644 --- a/parlamentares/forms.py +++ b/parlamentares/forms.py @@ -10,7 +10,8 @@ from floppyforms.widgets import ClearableFileInput import sapl from sapl.utils import intervalos_tem_intersecao -from .models import Filiacao, Legislatura, Mandato, Parlamentar +from .models import (ComposicaoColigacao, Filiacao, Legislatura, + Mandato, Parlamentar) class ImageThumbnailFileInput(ClearableFileInput): @@ -154,3 +155,23 @@ class FiliacaoForm(ModelForm): raise ValidationError(validacao[1]) return self.cleaned_data + + +class ComposicaoColigacaoForm(ModelForm): + + class Meta: + model = ComposicaoColigacao + fields = ['partido'] + + def clean(self): + cleaned_data = self.cleaned_data + pk = self.initial['coligacao_id'] + if (ComposicaoColigacao.objects.filter( + coligacao_id=pk, + partido=cleaned_data.get('partido')).exists()): + msg = _('Esse partido já foi cadastrado nesta coligação.') + raise ValidationError(msg) + else: + if self.errors: + return self.errors + return self.cleaned_data diff --git a/parlamentares/layouts.yaml b/parlamentares/layouts.yaml index be274551b..866a791f9 100644 --- a/parlamentares/layouts.yaml +++ b/parlamentares/layouts.yaml @@ -88,3 +88,7 @@ TipoAfastamento: SituacaoMilitar: Tipo Situação Militar: - descricao + +ComposicaoColigacao: + 'Nome do Partido': + - partido diff --git a/parlamentares/migrations/0019_auto_20160518_1136.py b/parlamentares/migrations/0019_auto_20160518_1136.py new file mode 100644 index 000000000..f391c8be8 --- /dev/null +++ b/parlamentares/migrations/0019_auto_20160518_1136.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.5 on 2016-05-18 14:36 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('parlamentares', '0018_auto_20160510_0943'), + ] + + operations = [ + migrations.AlterField( + model_name='composicaocoligacao', + name='partido', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='parlamentares.Partido', verbose_name='Partido'), + ), + ] diff --git a/parlamentares/migrations/0020_auto_20160518_1201.py b/parlamentares/migrations/0020_auto_20160518_1201.py new file mode 100644 index 000000000..2fb59f220 --- /dev/null +++ b/parlamentares/migrations/0020_auto_20160518_1201.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.5 on 2016-05-18 15:01 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('parlamentares', '0019_auto_20160518_1136'), + ] + + operations = [ + migrations.AlterField( + model_name='composicaocoligacao', + name='partido', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='parlamentares.Partido', verbose_name='Partidos da Coligação'), + ), + ] diff --git a/parlamentares/models.py b/parlamentares/models.py index b591e5fcb..e19670871 100644 --- a/parlamentares/models.py +++ b/parlamentares/models.py @@ -95,7 +95,8 @@ class Partido(models.Model): class ComposicaoColigacao(models.Model): # TODO M2M - partido = models.ForeignKey(Partido) + partido = models.ForeignKey(Partido, + verbose_name=_('Partidos da Coligação')) coligacao = models.ForeignKey(Coligacao) class Meta: diff --git a/parlamentares/urls.py b/parlamentares/urls.py index a6137d613..de3525339 100644 --- a/parlamentares/urls.py +++ b/parlamentares/urls.py @@ -1,6 +1,7 @@ from django.conf.urls import include, url -from parlamentares.views import (CargoMesaCrud, ColigacaoCrud, DependenteCrud, +from parlamentares.views import (CargoMesaCrud, ColigacaoCrud, + ComposicaoColigacaoCrud, DependenteCrud, FiliacaoCrud, LegislaturaCrud, MandatoCrud, MesaDiretoraView, NivelInstrucaoCrud, ParlamentarCrud, PartidoCrud, @@ -16,6 +17,9 @@ urlpatterns = [ ParlamentarCrud.get_urls() + DependenteCrud.get_urls() + FiliacaoCrud.get_urls() + MandatoCrud.get_urls() )), + url(r'^coligacao/', + include(ColigacaoCrud.get_urls() + + ComposicaoColigacaoCrud.get_urls())), url(r'^sistema/parlamentar/legislatura/', include(LegislaturaCrud.get_urls())), @@ -23,8 +27,6 @@ urlpatterns = [ include(TipoDependenteCrud.get_urls())), url(r'^sistema/parlamentar/nivel-instrucao/', include(NivelInstrucaoCrud.get_urls())), - url(r'^sistema/parlamentar/coligacao/', - include(ColigacaoCrud.get_urls())), url(r'^sistema/parlamentar/tipo-afastamento/', include(TipoAfastamentoCrud.get_urls())), url(r'^sistema/parlamentar/tipo-militar/', diff --git a/parlamentares/views.py b/parlamentares/views.py index 75f61426e..371103328 100644 --- a/parlamentares/views.py +++ b/parlamentares/views.py @@ -10,16 +10,15 @@ import crud.masterdetail from crud.base import Crud from crud.masterdetail import MasterDetailCrud -from .forms import (FiliacaoForm, LegislaturaForm, ParlamentarCreateForm, - ParlamentarForm) -from .models import (CargoMesa, Coligacao, ComposicaoMesa, Dependente, - Filiacao, Legislatura, Mandato, NivelInstrucao, - Parlamentar, Partido, SessaoLegislativa, SituacaoMilitar, - TipoAfastamento, TipoDependente) +from .forms import (ComposicaoColigacaoForm, FiliacaoForm, LegislaturaForm, + ParlamentarCreateForm, ParlamentarForm) + +from .models import (CargoMesa, Coligacao, ComposicaoColigacao, ComposicaoMesa, + Dependente, Filiacao, Legislatura, Mandato, + NivelInstrucao, Parlamentar, Partido, SessaoLegislativa, + SituacaoMilitar, TipoAfastamento, TipoDependente) CargoMesaCrud = Crud.build(CargoMesa, 'cargo_mesa') -# LegislaturaCrud = Crud.build(Legislatura, 'tabelas_auxiliares#legislatura') -ColigacaoCrud = Crud.build(Coligacao, 'coligacao') PartidoCrud = Crud.build(Partido, 'partidos') SessaoLegislativaCrud = Crud.build(SessaoLegislativa, 'sessao_legislativa') TipoDependenteCrud = Crud.build(TipoDependente, 'tipo_dependente') @@ -39,6 +38,37 @@ class MandatoCrud(MasterDetailCrud): ordering = ('-legislatura__data_inicio') +class ColigacaoCrud(Crud): + model = Coligacao + help_path = 'tabelas_auxiliares#coligacao' + + class ListView(crud.base.CrudListView): + ordering = ('-legislatura__data_inicio', 'nome') + + +class ComposicaoColigacaoCrud(MasterDetailCrud): + model = ComposicaoColigacao + parent_field = 'coligacao' + help_path = '' + + class CreateView(MasterDetailCrud.CreateView): + form_class = ComposicaoColigacaoForm + + def get_initial(self): + id = self.kwargs['pk'] + return {'coligacao_id': id} + + class UpdateView(MasterDetailCrud.UpdateView): + form_class = ComposicaoColigacaoForm + + def get_initial(self): + id = self.kwargs['pk'] + return {'coligacao_id': id} + + class ListView(MasterDetailCrud.ListView): + ordering = '-partido__sigla' + + class LegislaturaCrud(Crud): model = Legislatura help_path = 'tabelas_auxiliares#legislatura' diff --git a/templates/parlamentares/coligacao_detail.html b/templates/parlamentares/coligacao_detail.html new file mode 100644 index 000000000..9b9be3a54 --- /dev/null +++ b/templates/parlamentares/coligacao_detail.html @@ -0,0 +1,10 @@ +{% extends "crud/detail.html" %} +{% load i18n %} + +{% block actions %} +
+ {% trans 'Ver Composição' %} + {% trans 'Editar' %} + {% trans 'Excluir' %} +
+{% endblock actions %} \ No newline at end of file diff --git a/templates/parlamentares/subnav.yaml b/templates/parlamentares/subnav.yaml index 1287cb308..fc171ea84 100644 --- a/templates/parlamentares/subnav.yaml +++ b/templates/parlamentares/subnav.yaml @@ -6,3 +6,4 @@ url: filiacao_list - title: Dependentes url: dependente_list +