Browse Source

Adiciona listagem ao crud mestre detalhe

pull/341/head
Marcio Mazza 9 years ago
committed by LeandroRoberto
parent
commit
be7a4491ba
  1. 2
      comissoes/views.py
  2. 11
      crud/base.py
  3. 28
      crud/masterdetail.py
  4. 2
      materia/views.py
  5. 2
      norma/views.py
  6. 52
      parlamentares/forms.py
  7. 16
      parlamentares/urls.py
  8. 77
      parlamentares/views.py
  9. 4
      sessao/views.py
  10. 32
      templates/parlamentares/parlamentar_dependente.html
  11. 7
      templates/parlamentares/parlamentar_dependente_edit.html
  12. 2
      templates/parlamentares/subnav.yaml

2
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']

11
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

28
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<pk>\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<pk>\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

2
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']

2
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']

52
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('&nbsp;'),
Submit('excluir', 'Excluir',
css_class='btn btn-primary')])
class FiliacaoForm(ModelForm):
class Meta:

16
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<pk>\d+)/dependente$',
ParlamentaresDependentesView.as_view(),
name='parlamentar_dependente'),
url(r'^parlamentar/(?P<pk>\d+)/dependente/(?P<dk>\d+)$',
ParlamentaresDependentesEditView.as_view(),
name='parlamentar_dependente_edit'),
url(r'^parlamentar/(?P<pk>\d+)/filiacao$',
FiliacaoView.as_view(),
name='parlamentar_filiacao'),

77
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')

4
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'

32
templates/parlamentares/parlamentar_dependente.html

@ -1,32 +0,0 @@
{% extends "crud/detail.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block actions %}{% endblock %}
{% block detail_content %}
<h2><strong>Parlamentar: </strong>{{ object.nome_parlamentar }}</h2>
<fieldset>
<legend>Dependentes</legend>
<table class="table table-striped table-bordered">
<thead class="thead-default">
<tr>
<th><strong>Nome Dependente</strong></th>
<th><strong>Tipo</strong></th>
<th><strong>Data de Nascimento</strong></th>
</tr>
</thead>
{% for d in dependentes %}
<tr>
<td><a href="{% url 'parlamentares:parlamentar_dependente_edit' object.id d.id %}">{{d.nome}}</a></td>
<td>{{d.tipo }}</td>
<td>{{d.data_nascimento|default_if_none:""}}</td>
</tr>
{% endfor %}
</table>
</fieldset>
{% crispy form %}
{% endblock detail_content %}

7
templates/parlamentares/parlamentar_dependente_edit.html

@ -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 %}

2
templates/parlamentares/subnav.yaml

@ -5,4 +5,4 @@
- title: Filiações Partidárias
url: parlamentar_filiacao
- title: Dependentes
url: parlamentar_dependente
url: dependente_list

Loading…
Cancel
Save