diff --git a/crud/base.py b/crud/base.py index 48bcb1e62..c14671a4d 100644 --- a/crud/base.py +++ b/crud/base.py @@ -107,6 +107,13 @@ class CrudListView(ListView): paginate_by = 10 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): return [ (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) # pagination - page_obj = context['page_obj'] - paginator = context['paginator'] - context['page_range'] = make_pagination( - page_obj.number, paginator.num_pages) + if self.paginate_by: + page_obj = context['page_obj'] + paginator = context['paginator'] + context['page_range'] = make_pagination( + page_obj.number, paginator.num_pages) # rows 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 return context diff --git a/parlamentares/views.py b/parlamentares/views.py index ab50cf8ec..03c49e68f 100644 --- a/parlamentares/views.py +++ b/parlamentares/views.py @@ -2,6 +2,7 @@ from django.contrib import messages from django.core.urlresolvers import reverse, reverse_lazy from django.http import HttpResponseRedirect from django.shortcuts import redirect +from django.utils.datastructures import MultiValueDictKeyError from django.utils.translation import ugettext_lazy as _ from django.views.generic import CreateView, FormView, UpdateView @@ -34,10 +35,6 @@ class ParlamentarCrud(Crud): model = Parlamentar help_path = '' - class BaseMixin(crud.base.BaseMixin): - # TODO: partido... - list_field_names = ['nome_parlamentar', 'ativo'] - class CreateView(crud.base.CrudCreateView): form_class = ParlamentarCreateForm @@ -45,6 +42,58 @@ class ParlamentarCrud(Crud): def layout_key(self): 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): data_filiacao = form.cleaned_data['data'] diff --git a/templates/crud/list.html b/templates/crud/list.html index 297be1c3d..0d98cbdd2 100644 --- a/templates/crud/list.html +++ b/templates/crud/list.html @@ -10,6 +10,9 @@ {% block more_buttons %}{% endblock more_buttons %} +

+{% block extra_content %} {% endblock %} + {% if not rows %}

{{ NO_ENTRIES_MSG }}

{% else %} diff --git a/templates/parlamentares/parlamentares_cadastro.html b/templates/parlamentares/parlamentares_cadastro.html deleted file mode 100644 index 354d3aa46..000000000 --- a/templates/parlamentares/parlamentares_cadastro.html +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "parlamentares/parlamentares_detail.html" %} -{% load i18n %} -{% load crispy_forms_tags %} -{% block actions %}{% endblock %} -{% block detail_content %} - {% crispy form %} -{% endblock %} \ No newline at end of file diff --git a/templates/parlamentares/parlamentares_detail.html b/templates/parlamentares/parlamentares_detail.html index 3eed3c3dd..5f2f0a8d1 100644 --- a/templates/parlamentares/parlamentares_detail.html +++ b/templates/parlamentares/parlamentares_detail.html @@ -3,7 +3,7 @@ {% block actions %}
{% if legislatura_id != 0 %} - {% trans 'Incluir Parlamentar' %} + {% trans 'Incluir Parlamentar' %} {% endif %}
{% endblock actions %} diff --git a/templates/parlamentares/parlamentares_list.html b/templates/parlamentares/parlamentares_list.html index 3d00cce02..bd8fd613d 100644 --- a/templates/parlamentares/parlamentares_list.html +++ b/templates/parlamentares/parlamentares_list.html @@ -1,10 +1,10 @@ -{% extends "parlamentares/parlamentares_detail.html" %} +{% extends "crud/list.html" %} {% load i18n %} {% load crispy_forms_tags %} -{% block detail_content %} +{% block extra_content %}
Selecione o Período -
+ {% csrf_token %}