Browse Source

Usa crud para listagem de parlamentares

pull/298/head
Eduardo Edson Batista Cordeiro Alves 9 years ago
parent
commit
0802e40317
  1. 22
      crud/base.py
  2. 57
      parlamentares/views.py
  3. 3
      templates/crud/list.html
  4. 7
      templates/parlamentares/parlamentares_cadastro.html
  5. 2
      templates/parlamentares/parlamentares_detail.html
  6. 28
      templates/parlamentares/parlamentares_list.html

22
crud/base.py

@ -107,6 +107,13 @@ class CrudListView(ListView):
paginate_by = 10 paginate_by = 10
no_entries_msg = _('Nenhum registro encontrado.') no_entries_msg = _('Nenhum registro encontrado.')
def get_rows(self, object_list):
return [self._as_row(obj) for obj in object_list]
def get_headers(self):
return [self.model._meta.get_field(fieldname).verbose_name
for fieldname in self.list_field_names]
def _as_row(self, obj): def _as_row(self, obj):
return [ return [
(get_field_display(obj, name)[1], obj.pk if i == 0 else None) (get_field_display(obj, name)[1], obj.pk if i == 0 else None)
@ -117,18 +124,17 @@ class CrudListView(ListView):
context.setdefault('title', self.verbose_name_plural) context.setdefault('title', self.verbose_name_plural)
# pagination # pagination
page_obj = context['page_obj'] if self.paginate_by:
paginator = context['paginator'] page_obj = context['page_obj']
context['page_range'] = make_pagination( paginator = context['paginator']
page_obj.number, paginator.num_pages) context['page_range'] = make_pagination(
page_obj.number, paginator.num_pages)
# rows # rows
object_list = context['object_list'] object_list = context['object_list']
context['rows'] = [self._as_row(obj) for obj in object_list] context['headers'] = self.get_headers()
context['rows'] = self.get_rows(object_list)
context['headers'] = [
self.model._meta.get_field(fieldname).verbose_name
for fieldname in self.list_field_names]
context['NO_ENTRIES_MSG'] = self.no_entries_msg context['NO_ENTRIES_MSG'] = self.no_entries_msg
return context return context

57
parlamentares/views.py

@ -2,6 +2,7 @@ from django.contrib import messages
from django.core.urlresolvers import reverse, reverse_lazy from django.core.urlresolvers import reverse, reverse_lazy
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import redirect from django.shortcuts import redirect
from django.utils.datastructures import MultiValueDictKeyError
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.generic import CreateView, FormView, UpdateView from django.views.generic import CreateView, FormView, UpdateView
@ -34,10 +35,6 @@ class ParlamentarCrud(Crud):
model = Parlamentar model = Parlamentar
help_path = '' help_path = ''
class BaseMixin(crud.base.BaseMixin):
# TODO: partido...
list_field_names = ['nome_parlamentar', 'ativo']
class CreateView(crud.base.CrudCreateView): class CreateView(crud.base.CrudCreateView):
form_class = ParlamentarCreateForm form_class = ParlamentarCreateForm
@ -45,6 +42,58 @@ class ParlamentarCrud(Crud):
def layout_key(self): def layout_key(self):
return 'ParlamentarCreate' return 'ParlamentarCreate'
class ListView(crud.base.CrudListView):
template_name = "parlamentares/parlamentares_list.html"
paginate_by = None
def take_legislatura_id(self):
legislaturas = Legislatura.objects.all().order_by(
'-data_inicio', '-data_fim')
try:
legislatura_id = int(self.request.GET['periodo'])
except MultiValueDictKeyError:
legislatura_id = legislaturas.first().id
return legislatura_id
def get_queryset(self):
mandatos = Mandato.objects.filter(
legislatura_id=self.take_legislatura_id())
return mandatos
def get_rows(self, object_list):
parlamentares = []
for m in object_list:
if m.parlamentar.filiacao_set.last():
partido = m.parlamentar.filiacao_set.last().partido.sigla
else:
partido = _('Sem Registro')
parlamentar = [
(m.parlamentar.nome_parlamentar, m.parlamentar.id),
(partido, None),
('Sim' if m.parlamentar.ativo else 'Não', None)
]
parlamentares.append(parlamentar)
return parlamentares
def get_headers(self):
return ['Parlamentar', 'Partido', 'Ativo?']
def get_context_data(self, **kwargs):
context = super(ParlamentarCrud.ListView, self
).get_context_data(**kwargs)
context.setdefault('title', self.verbose_name_plural)
# Adiciona legislatura para filtrar parlamentares
legislaturas = Legislatura.objects.all().order_by(
'-data_inicio', '-data_fim')
context['legislaturas'] = legislaturas
context['legislatura_id'] = self.take_legislatura_id()
return context
def validate(form, parlamentar, filiacao, request): def validate(form, parlamentar, filiacao, request):
data_filiacao = form.cleaned_data['data'] data_filiacao = form.cleaned_data['data']

3
templates/crud/list.html

@ -10,6 +10,9 @@
{% block more_buttons %}{% endblock more_buttons %} {% block more_buttons %}{% endblock more_buttons %}
</div> </div>
<br/><br/>
{% block extra_content %} {% endblock %}
{% if not rows %} {% if not rows %}
<p>{{ NO_ENTRIES_MSG }}</p> <p>{{ NO_ENTRIES_MSG }}</p>
{% else %} {% else %}

7
templates/parlamentares/parlamentares_cadastro.html

@ -1,7 +0,0 @@
{% extends "parlamentares/parlamentares_detail.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block actions %}{% endblock %}
{% block detail_content %}
{% crispy form %}
{% endblock %}

2
templates/parlamentares/parlamentares_detail.html

@ -3,7 +3,7 @@
{% block actions %} {% block actions %}
<div class="actions btn-group pull-right" role="group"> <div class="actions btn-group pull-right" role="group">
{% if legislatura_id != 0 %} {% if legislatura_id != 0 %}
<a href="{% url 'parlamentares:parlamentares_cadastro' legislatura_id %}" id="incluir-parlamentar" class="btn btn-default">{% trans 'Incluir Parlamentar' %}</a> <a href="{% url 'parlamentares:parlamentar_create'%}" id="incluir-parlamentar" class="btn btn-default">{% trans 'Incluir Parlamentar' %}</a>
{% endif %} {% endif %}
</div> </div>
{% endblock actions %} {% endblock actions %}

28
templates/parlamentares/parlamentares_list.html

@ -1,10 +1,10 @@
{% extends "parlamentares/parlamentares_detail.html" %} {% extends "crud/list.html" %}
{% load i18n %} {% load i18n %}
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
{% block detail_content %} {% block extra_content %}
<fieldset class="form-group"> <fieldset class="form-group">
<legend>Selecione o Período</legend> <legend>Selecione o Período</legend>
<form method="POST"> <form method="GET">
{% csrf_token %} {% csrf_token %}
<select name="periodo" class="form-control" onChange="form.submit();"> <select name="periodo" class="form-control" onChange="form.submit();">
{% for l in legislaturas %} {% for l in legislaturas %}
@ -16,26 +16,4 @@
</form> </form>
</fieldset> </fieldset>
<br/> <br/>
<fieldset class="form-group">
<legend>Parlamentares</legend>
<table class="table table-striped table-bordered">
<thead class="thead-default">
<tr>
<th>Nome do Parlamentar</th>
<th>Partido</th>
<th>Ativo?</th>
</tr>
</thead>
{% for p in parlamentares %}
<tr>
<td>
<a href="{% url 'parlamentares:parlamentares_editar' p.id %}"> {{p.nome}}</a>
</td>
<td>{{p.partido}}</td>
<td>{{p.ativo|yesno:"Sim,Não"}}</td>
</tr>
{% endfor %}
</table>
</fieldset>
{% endblock %} {% endblock %}

Loading…
Cancel
Save