diff --git a/sapl/crispy_layout_mixin.py b/sapl/crispy_layout_mixin.py index 2c9bc7c37..82bc2538a 100644 --- a/sapl/crispy_layout_mixin.py +++ b/sapl/crispy_layout_mixin.py @@ -1,12 +1,12 @@ from math import ceil -import rtyaml from crispy_forms.bootstrap import FormActions from crispy_forms.helper import FormHelper from crispy_forms.layout import HTML, Div, Fieldset, Layout, Submit from django import template from django.utils import formats from django.utils.translation import ugettext as _ +import rtyaml def heads_and_tails(list_of_lists): @@ -60,15 +60,16 @@ def get_field_display(obj, fieldname): field = '' try: field = obj._meta.get_field(fieldname) - except: + except Exception as e: + """ nos casos que o fieldname não é um field_model, + ele pode ser um aggregate, annotate, um property, um manager, + ou mesmo uma método no model. + """ field = getattr(obj, fieldname) - if 'ManyRelatedManager' not in str(type(field))\ - and 'RelatedManager' not in str(type(field))\ - and 'GenericRelatedObjectManager' not in str(type(field)): - return '', str(field) verbose_name = str(field.verbose_name)\ if hasattr(field, 'verbose_name') else '' + if hasattr(field, 'choices') and field.choices: value = getattr(obj, 'get_%s_display' % fieldname)() else: diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index 485439d93..0f7f0ee70 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -1,6 +1,7 @@ from django.contrib import messages from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse, reverse_lazy +from django.db.models import F from django.http.response import HttpResponseRedirect from django.utils.datastructures import MultiValueDictKeyError from django.utils.translation import ugettext_lazy as _ @@ -21,6 +22,7 @@ from .models import (CargoMesa, Coligacao, ComposicaoColigacao, ComposicaoMesa, NivelInstrucao, Parlamentar, Partido, SessaoLegislativa, SituacaoMilitar, TipoAfastamento, TipoDependente, Votante) + CargoMesaCrud = CrudAux.build(CargoMesa, 'cargo_mesa') PartidoCrud = CrudAux.build(Partido, 'partidos') SessaoLegislativaCrud = CrudAux.build(SessaoLegislativa, 'sessao_legislativa') @@ -60,8 +62,8 @@ class VotanteView(MasterDetailCrud): if obj.user: obj.user.delete() return HttpResponseRedirect( - reverse('sapl.parlamentares:votante_list', - kwargs={'pk': obj.parlamentar.pk})) + reverse('sapl.parlamentares:votante_list', + kwargs={'pk': obj.parlamentar.pk})) class FrenteList(MasterDetailCrud): @@ -107,7 +109,7 @@ class ProposicaoParlamentarCrud(CrudBaseForListAndDetailExternalAppView): def get_context_data(self, **kwargs): context = CrudBaseForListAndDetailExternalAppView\ - .ListView.get_context_data(self, **kwargs) + .ListView.get_context_data(self, **kwargs) context['title'] = context['title'].replace( 'Proposições', 'Matérias') return context @@ -264,7 +266,7 @@ class ParlamentarCrud(Crud): class BaseMixin(Crud.BaseMixin): ordered_list = False list_field_names = [ - 'avatar_html', 'nome_parlamentar', 'filiacao_atual', 'ativo'] + 'avatar_html', 'nome_parlamentar', 'filiacao_atual', 'ativo', 'mandato_titular'] class DetailView(Crud.DetailView): @@ -310,7 +312,8 @@ class ParlamentarCrud(Crud): legislatura_id = self.take_legislatura_id() if legislatura_id != 0: return queryset.filter( - mandato__legislatura_id=legislatura_id) + mandato__legislatura_id=legislatura_id).annotate( + mandato_titular=F('mandato__titular')) else: try: l = Legislatura.objects.all().order_by( @@ -320,10 +323,11 @@ class ParlamentarCrud(Crud): else: if l is None: return Legislatura.objects.all() - return queryset.filter(mandato__legislatura_id=l) + return queryset.filter(mandato__legislatura_id=l).annotate( + mandato_titular=F('mandato__titular')) def get_headers(self): - return ['', _('Parlamentar'), _('Partido'), _('Ativo?')] + return ['', _('Parlamentar'), _('Partido'), _('Ativo?'), _('Titular?')] def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -404,7 +408,7 @@ class MesaDiretoraView(FormView): sessao_legislativa_id=sessao_plenaria.id, # parlamentar_id = integrante.parlamentar_id, cargo_id=cargo.id - ).exists() + ).exists() def post(self, request, *args, **kwargs):