diff --git a/comissoes/views.py b/comissoes/views.py index 88ca097f0..09bdd137d 100644 --- a/comissoes/views.py +++ b/comissoes/views.py @@ -21,7 +21,7 @@ class ComissaoCrud(Crud): model = Comissao help_path = 'modulo_comissoes' - class BaseMixin(crud.base.BaseMixin): + class BaseMixin(crud.base.CrudBaseMixin): list_field_names = ['nome', 'sigla', 'tipo', 'data_criacao'] diff --git a/crud/base.py b/crud/base.py index c3d0ec006..cfe2c370d 100644 --- a/crud/base.py +++ b/crud/base.py @@ -56,7 +56,7 @@ def make_pagination(index, num_pages): return head + [None] + tail -class BaseMixin(CrispyLayoutFormMixin): +class CrudBaseMixin(CrispyLayoutFormMixin): @classmethod def url_name(cls, suffix): @@ -88,7 +88,7 @@ class BaseMixin(CrispyLayoutFormMixin): return self.resolve_url(DELETE, args=(self.object.id,)) def get_template_names(self): - names = super(BaseMixin, self).get_template_names() + names = super(CrudBaseMixin, self).get_template_names() names.append("crud/%s.html" % self.template_name_suffix.lstrip('_')) return names @@ -120,7 +120,8 @@ class CrudListView(ListView): def _as_row(self, obj): return [ - (get_field_display(obj, name)[1], obj.pk if i == 0 else None) + (get_field_display(obj, name)[1], + self.resolve_url(DETAIL, args=(obj.id,)) if i == 0 else None) for i, name in enumerate(self.list_field_names)] def get_context_data(self, **kwargs): @@ -205,7 +206,7 @@ class CrudDeleteView(FormMessagesMixin, DeleteView): class Crud: - BaseMixin = BaseMixin + BaseMixin = CrudBaseMixin ListView = CrudListView CreateView = CrudCreateView DetailView = CrudDetailView @@ -220,6 +221,7 @@ class Crud: class CrudViewWithBase(cls.BaseMixin, view): model = cls.model help_path = cls.help_path + crud = cls CrudViewWithBase.__name__ = view.__name__ return CrudViewWithBase @@ -240,6 +242,7 @@ class Crud: @classonlymethod def build(cls, _model, _help_path): + class ModelCrud(cls): model = _model help_path = _help_path diff --git a/crud/masterdetail.py b/crud/masterdetail.py index 458f74fd8..602d4be9b 100644 --- a/crud/masterdetail.py +++ b/crud/masterdetail.py @@ -1,15 +1,33 @@ -from .base import (Crud, CrudCreateView, CrudDeleteView, CrudDetailView, - CrudListView, CrudUpdateView) +from django.utils.decorators import classonlymethod + +from .base import (CREATE, LIST, Crud, CrudBaseMixin, CrudCreateView, + CrudDeleteView, CrudDetailView, CrudListView, + CrudUpdateView) class MasterDetailCrud(Crud): + class BaseMixin(CrudBaseMixin): + + @property + def list_url(self): + return self.resolve_url(LIST, args=(self.kwargs['pk'],)) + + @property + def create_url(self): + return self.resolve_url(CREATE, args=(self.kwargs['pk'],)) + class ListView(CrudListView): @classmethod def get_url_regex(cls): return r'^(?P\d+)/%s$' % cls.model._meta.model_name + def get_queryset(self): + qs = super(MasterDetailCrud.ListView, self).get_queryset() + kwargs = {self.crud.parent_field: self.kwargs['pk']} + return qs.filter(**kwargs) + class CreateView(CrudCreateView): @classmethod @@ -33,3 +51,9 @@ class MasterDetailCrud(Crud): @classmethod def get_url_regex(cls): return r'^%s/(?P\d+)/delete$' % cls.model._meta.model_name + + @classonlymethod + def build(cls, model, parent_field, help_path): + crud = super(MasterDetailCrud, cls).build(model, help_path) + crud.parent_field = parent_field + return crud diff --git a/materia/views.py b/materia/views.py index 97efe04a7..b47589a85 100644 --- a/materia/views.py +++ b/materia/views.py @@ -62,7 +62,7 @@ class MateriaLegislativaCrud(Crud): model = MateriaLegislativa help_path = 'materia_legislativa' - class BaseMixin(crud.base.BaseMixin): + class BaseMixin(crud.base.CrudBaseMixin): list_field_names = ['tipo', 'numero', 'ano', 'data_apresentacao'] diff --git a/norma/views.py b/norma/views.py index e454ac7cc..838e7a62b 100644 --- a/norma/views.py +++ b/norma/views.py @@ -43,7 +43,7 @@ class NormaCrud(Crud): def layout_key(self): return 'NormaJuridicaCreate' - class BaseMixin(crud.base.BaseMixin): + class BaseMixin(crud.base.CrudBaseMixin): list_field_names = ['tipo', 'numero', 'ano', 'ementa'] diff --git a/parlamentares/forms.py b/parlamentares/forms.py index 3df9ae69e..d708ce4cd 100644 --- a/parlamentares/forms.py +++ b/parlamentares/forms.py @@ -9,7 +9,7 @@ import crispy_layout_mixin import sapl from crispy_layout_mixin import form_actions -from .models import Dependente, Filiacao, Legislatura, Mandato, Parlamentar +from .models import Filiacao, Legislatura, Mandato, Parlamentar class ParlamentarForm(ModelForm): @@ -106,56 +106,6 @@ class MandatoEditForm(MandatoForm): css_class='btn btn-primary')]) -class DependenteForm(ModelForm): - - class Meta: - model = Dependente - fields = ['nome', - 'data_nascimento', - 'tipo', - 'sexo', - 'cpf', - 'rg', - 'titulo_eleitor'] - - def __init__(self, *args, **kwargs): - - row1 = crispy_layout_mixin.to_row( - [('nome', 12)]) - - row2 = crispy_layout_mixin.to_row( - [('tipo', 4), - ('sexo', 4), - ('data_nascimento', 4)]) - - row3 = crispy_layout_mixin.to_row( - [('cpf', 4), - ('rg', 4), - ('titulo_eleitor', 4)]) - - self.helper = FormHelper() - self.helper.layout = Layout( - Fieldset(_('Cadastro de Dependentes'), - row1, row2, row3, - form_actions()) - - ) - super(DependenteForm, self).__init__( - *args, **kwargs) - - -class DependenteEditForm(DependenteForm): - - def __init__(self, *args, **kwargs): - super(DependenteEditForm, self).__init__( - *args, **kwargs) - - self.helper.layout[0][-1:] = form_actions(more=[ - HTML(' '), - Submit('excluir', 'Excluir', - css_class='btn btn-primary')]) - - class FiliacaoForm(ModelForm): class Meta: diff --git a/parlamentares/urls.py b/parlamentares/urls.py index 6ef8bdf59..5015c3170 100644 --- a/parlamentares/urls.py +++ b/parlamentares/urls.py @@ -1,12 +1,10 @@ from django.conf.urls import include, url -from parlamentares.views import (CargoMesaCrud, ColigacaoCrud, +from parlamentares.views import (CargoMesaCrud, ColigacaoCrud, DependenteCrud, FiliacaoEditView, FiliacaoView, LegislaturaCrud, MandatoEditView, MandatoView, MesaDiretoraView, NivelInstrucaoCrud, - ParlamentarCrud, - ParlamentaresDependentesEditView, - ParlamentaresDependentesView, PartidoCrud, + ParlamentarCrud, PartidoCrud, SessaoLegislativaCrud, TipoAfastamentoCrud, TipoDependenteCrud, TipoMilitarCrud) @@ -15,7 +13,9 @@ from .apps import AppConfig app_name = AppConfig.name urlpatterns = [ - url(r'^parlamentar/', include(ParlamentarCrud.get_urls())), + url(r'^parlamentar/', include( + ParlamentarCrud.get_urls() + DependenteCrud.get_urls() + )), url(r'^sistema/parlamentar/legislatura/', include(LegislaturaCrud.get_urls())), @@ -36,12 +36,6 @@ urlpatterns = [ url(r'^sistema/mesa-diretora/cargo-mesa/', include(CargoMesaCrud.get_urls())), - url(r'^parlamentar/(?P\d+)/dependente$', - ParlamentaresDependentesView.as_view(), - name='parlamentar_dependente'), - url(r'^parlamentar/(?P\d+)/dependente/(?P\d+)$', - ParlamentaresDependentesEditView.as_view(), - name='parlamentar_dependente_edit'), url(r'^parlamentar/(?P\d+)/filiacao$', FiliacaoView.as_view(), name='parlamentar_filiacao'), diff --git a/parlamentares/views.py b/parlamentares/views.py index c0bb5d859..7ef91e9dc 100644 --- a/parlamentares/views.py +++ b/parlamentares/views.py @@ -8,10 +8,10 @@ from django.views.generic import CreateView, FormView, UpdateView import crud.base from crud.base import Crud +from crud.masterdetail import MasterDetailCrud -from .forms import (DependenteEditForm, DependenteForm, FiliacaoEditForm, - FiliacaoForm, MandatoEditForm, MandatoForm, - ParlamentarCreateForm, ParlamentarForm) +from .forms import (FiliacaoEditForm, FiliacaoForm, MandatoEditForm, + MandatoForm, ParlamentarCreateForm, ParlamentarForm) from .models import (CargoMesa, Coligacao, ComposicaoMesa, Dependente, Filiacao, Legislatura, Mandato, NivelInstrucao, Parlamentar, Partido, SessaoLegislativa, SituacaoMilitar, @@ -21,7 +21,6 @@ CargoMesaCrud = Crud.build(CargoMesa, 'cargo_mesa') LegislaturaCrud = Crud.build(Legislatura, 'tabelas_auxiliares#legislatura') ColigacaoCrud = Crud.build(Coligacao, 'coligacao') PartidoCrud = Crud.build(Partido, 'partidos') -DependenteCrud = Crud.build(Dependente, '') SessaoLegislativaCrud = Crud.build(SessaoLegislativa, 'sessao_legislativa') FiliacaoCrud = Crud.build(Filiacao, '') MandatoCrud = Crud.build(Mandato, '') @@ -30,6 +29,8 @@ NivelInstrucaoCrud = Crud.build(NivelInstrucao, 'nivel_instrucao') TipoAfastamentoCrud = Crud.build(TipoAfastamento, 'tipo_afastamento') TipoMilitarCrud = Crud.build(SituacaoMilitar, 'tipo_situa_militar') +DependenteCrud = MasterDetailCrud.build(Dependente, 'parlamentar', '') + class ParlamentarCrud(Crud): model = Parlamentar @@ -160,74 +161,6 @@ def validate(form, parlamentar, filiacao, request): return True -class ParlamentaresDependentesView(CreateView): - template_name = "parlamentares/parlamentar_dependente.html" - form_class = DependenteForm - model = Dependente - - def get_success_url(self): - pk = self.kwargs['pk'] - return reverse('parlamentares:parlamentar_dependente', - kwargs={'pk': pk}) - - def get_context_data(self, **kwargs): - context = super(ParlamentaresDependentesView, self).\ - get_context_data(**kwargs) - pk = self.kwargs['pk'] - parlamentar = Parlamentar.objects.get(pk=pk) - dependentes = Dependente.objects.filter( - parlamentar=parlamentar).order_by('nome', 'tipo') - - if len(parlamentar.mandato_set.all()) == 0: - legislatura_id = 0 - else: - legislatura_id = parlamentar.mandato_set.last().legislatura.id - - context.update({'object': parlamentar, - 'dependentes': dependentes, - # precisa de legislatura_id??? - 'legislatura_id': legislatura_id}) - return context - - def form_valid(self, form): - parlamentar_id = self.kwargs['pk'] - dependente = form.save(commit=False) - parlamentar = Parlamentar.objects.get(id=parlamentar_id) - dependente.parlamentar = parlamentar - dependente.save() - return HttpResponseRedirect(self.get_success_url()) - - -class ParlamentaresDependentesEditView(UpdateView): - template_name = "parlamentares/parlamentar_dependente_edit.html" - form_class = DependenteEditForm - model = Dependente - pk_url_kwarg = 'dk' - - def get_success_url(self): - pk = self.kwargs['pk'] - return reverse('parlamentares:parlamentar_dependente', - kwargs={'pk': pk}) - - def get_context_data(self, **kwargs): - context = super(ParlamentaresDependentesEditView, self).\ - get_context_data(**kwargs) - parlamentar = Parlamentar.objects.get(id=self.kwargs['pk']) - context.update({ - 'object': parlamentar, - 'legislatura_id': parlamentar.mandato_set.last( - ).legislatura_id}) - return context - - def form_valid(self, form): - if 'salvar' in self.request.POST: - form.save() - elif 'excluir' in self.request.POST: - dependente = form.instance - dependente.delete() - return HttpResponseRedirect(self.get_success_url()) - - class MesaDiretoraView(FormView): template_name = "mesa_diretora/mesa_diretora.html" success_url = reverse_lazy('parlamentares:mesa_diretora') diff --git a/sessao/views.py b/sessao/views.py index a8fbbb002..442136151 100644 --- a/sessao/views.py +++ b/sessao/views.py @@ -42,13 +42,13 @@ class SessaoCrud(Crud): model = SessaoPlenaria help_path = 'sessao_plenaria' - class BaseMixin(crud.base.BaseMixin): + class BaseMixin(crud.base.CrudBaseMixin): list_field_names = ['numero', 'tipo', 'legislatura', 'sessao_legislativa', 'data_inicio', 'hora_inicio'] # FIXME!!!! corrigir referencias no codigo e remover isso!!!!! # fazer com #230 - class CrudDetailView(crud.base.BaseMixin, crud.base.DetailView): + class CrudDetailView(crud.base.CrudDetailView): model = SessaoPlenaria help_path = 'sessao_plenaria' diff --git a/templates/parlamentares/parlamentar_dependente.html b/templates/parlamentares/parlamentar_dependente.html deleted file mode 100644 index 01683a8d8..000000000 --- a/templates/parlamentares/parlamentar_dependente.html +++ /dev/null @@ -1,32 +0,0 @@ -{% extends "crud/detail.html" %} -{% load i18n %} -{% load crispy_forms_tags %} -{% block actions %}{% endblock %} -{% block detail_content %} - -

Parlamentar: {{ object.nome_parlamentar }}

- -
- Dependentes - - - - - - - - - - {% for d in dependentes %} - - - - - - {% endfor %} -
Nome DependenteTipoData de Nascimento
{{d.nome}}{{d.tipo }}{{d.data_nascimento|default_if_none:""}}
-
- - {% crispy form %} - -{% endblock detail_content %} diff --git a/templates/parlamentares/parlamentar_dependente_edit.html b/templates/parlamentares/parlamentar_dependente_edit.html deleted file mode 100644 index 9682c56d5..000000000 --- a/templates/parlamentares/parlamentar_dependente_edit.html +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "crud/detail.html" %} -{% load i18n %} -{% load crispy_forms_tags %} -{% block actions %}{% endblock %} -{% block detail_content %} - {% crispy form %} -{% endblock detail_content %} diff --git a/templates/parlamentares/subnav.yaml b/templates/parlamentares/subnav.yaml index 35eae1ffc..64a43f3fe 100644 --- a/templates/parlamentares/subnav.yaml +++ b/templates/parlamentares/subnav.yaml @@ -5,4 +5,4 @@ - title: Filiações Partidárias url: parlamentar_filiacao - title: Dependentes - url: parlamentar_dependente + url: dependente_list