Browse Source

Aplica ref do crud em ParlamentarCrud

pull/675/head
LeandroRoberto 8 years ago
parent
commit
410a4dc578
  1. 2
      sapl/base/views.py
  2. 2
      sapl/comissoes/views.py
  3. 9
      sapl/crispy_layout_mixin.py
  4. 3
      sapl/crud/base.py
  5. 13
      sapl/parlamentares/models.py
  6. 129
      sapl/parlamentares/views.py
  7. 5
      sapl/static/styles/app.scss
  8. 2
      sapl/templates/crud/list.html

2
sapl/base/views.py

@ -225,7 +225,6 @@ class RelatorioMateriasPorAutorView(FilterView):
class CasaLegislativaCrud(CrudAux): class CasaLegislativaCrud(CrudAux):
model = CasaLegislativa model = CasaLegislativa
help_path = ''
class BaseMixin(CrudAux.BaseMixin): class BaseMixin(CrudAux.BaseMixin):
list_field_names = ['codigo', 'nome', 'sigla'] list_field_names = ['codigo', 'nome', 'sigla']
@ -248,7 +247,6 @@ class HelpView(PermissionRequiredMixin, TemplateView):
class AppConfigCrud(CrudAux): class AppConfigCrud(CrudAux):
model = AppConfig model = AppConfig
help_path = ''
class BaseMixin(CrudAux.BaseMixin): class BaseMixin(CrudAux.BaseMixin):
form_class = ConfiguracoesAppForm form_class = ConfiguracoesAppForm

2
sapl/comissoes/views.py

@ -26,7 +26,6 @@ TipoComissaoCrud = CrudAux.build(
class ParticipacaoCrud(MasterDetailCrud): class ParticipacaoCrud(MasterDetailCrud):
model = Participacao model = Participacao
parent_field = 'composicao__comissao' parent_field = 'composicao__comissao'
help_path = ''
class BaseMixin(MasterDetailCrud.BaseMixin): class BaseMixin(MasterDetailCrud.BaseMixin):
list_field_names = ['composicao', 'parlamentar', 'cargo'] list_field_names = ['composicao', 'parlamentar', 'cargo']
@ -38,7 +37,6 @@ class ParticipacaoCrud(MasterDetailCrud):
class ComposicaoCrud(MasterDetailCrud): class ComposicaoCrud(MasterDetailCrud):
model = Composicao model = Composicao
parent_field = 'comissao' parent_field = 'comissao'
help_path = ''
model_set = 'participacao_set' model_set = 'participacao_set'
class ListView(MasterDetailCrud.ListView): class ListView(MasterDetailCrud.ListView):

9
sapl/crispy_layout_mixin.py

@ -1,12 +1,12 @@
from math import ceil from math import ceil
import rtyaml
from crispy_forms.bootstrap import FormActions from crispy_forms.bootstrap import FormActions
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, Div, Fieldset, Layout, Submit from crispy_forms.layout import HTML, Div, Fieldset, Layout, Submit
from django import template from django import template
from django.utils import formats from django.utils import formats
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
import rtyaml
def heads_and_tails(list_of_lists): def heads_and_tails(list_of_lists):
@ -49,7 +49,12 @@ class SaplFormLayout(Layout):
def get_field_display(obj, fieldname): def get_field_display(obj, fieldname):
field = obj._meta.get_field(fieldname) field = ''
try:
field = obj._meta.get_field(fieldname)
except:
value = getattr(obj, fieldname)
return '', value
verbose_name = str(field.verbose_name)\ verbose_name = str(field.verbose_name)\
if hasattr(field, 'verbose_name') else '' if hasattr(field, 'verbose_name') else ''
if hasattr(field, 'choices') and field.choices: if hasattr(field, 'choices') and field.choices:

3
sapl/crud/base.py

@ -13,8 +13,8 @@ from django.db import models
from django.http.response import Http404 from django.http.response import Http404
from django.utils.decorators import classonlymethod from django.utils.decorators import classonlymethod
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import string_concat from django.utils.translation import string_concat
from django.utils.translation import ugettext_lazy as _
from django.views.generic import (CreateView, DeleteView, DetailView, ListView, from django.views.generic import (CreateView, DeleteView, DetailView, ListView,
UpdateView) UpdateView)
from django.views.generic.base import ContextMixin from django.views.generic.base import ContextMixin
@ -23,6 +23,7 @@ from django.views.generic.list import MultipleObjectMixin
from sapl.crispy_layout_mixin import CrispyLayoutFormMixin, get_field_display from sapl.crispy_layout_mixin import CrispyLayoutFormMixin, get_field_display
from sapl.utils import normalize from sapl.utils import normalize
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
ACTION_LIST, ACTION_CREATE, ACTION_DETAIL, ACTION_UPDATE, ACTION_DELETE = \ ACTION_LIST, ACTION_CREATE, ACTION_DETAIL, ACTION_UPDATE, ACTION_DELETE = \

13
sapl/parlamentares/models.py

@ -267,6 +267,19 @@ class Parlamentar(models.Model):
def __str__(self): def __str__(self):
return self.nome_completo return self.nome_completo
@property
def filiacao_atual(self):
ultima_filiacao = self.filiacao_set.order_by('-data').first()
if ultima_filiacao and not ultima_filiacao.data_desfiliacao:
return ultima_filiacao.partido.sigla
else:
return _('Sem Partido')
@property
def avatar_html(self):
return '<img class="avatar-parlamentar" src='\
+ self.fotografia.url + '/>'if self.fotografia else ''
class TipoDependente(models.Model): class TipoDependente(models.Model):
descricao = models.CharField(max_length=50, verbose_name=_('Descrição')) descricao = models.CharField(max_length=50, verbose_name=_('Descrição'))

129
sapl/parlamentares/views.py

@ -1,4 +1,7 @@
from datetime import date
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
@ -11,7 +14,8 @@ from django.views.generic import FormView, ListView
from sapl.comissoes.models import Participacao from sapl.comissoes.models import Participacao
from sapl.crud.base import (Crud, CrudBaseMixin, CrudCreateView, from sapl.crud.base import (Crud, CrudBaseMixin, CrudCreateView,
CrudDeleteView, CrudDetailView, CrudListView, CrudDeleteView, CrudDetailView, CrudListView,
CrudUpdateView, MasterDetailCrud) CrudUpdateView, MasterDetailCrud, CrudAux,
RP_CHANGE)
from sapl.materia.models import Proposicao, Relatoria from sapl.materia.models import Proposicao, Relatoria
from sapl.utils import permissao_tb_aux, permissoes_parlamentares from sapl.utils import permissao_tb_aux, permissoes_parlamentares
@ -38,27 +42,17 @@ class FrenteList(ListView):
return context return context
class FrenteCrud(Crud): class FrenteCrud(CrudAux):
model = Frente model = Frente
help_path = ''
class BaseMixin(CrudBaseMixin): class BaseMixin(CrudAux.BaseMixin):
list_field_names = ['nome', 'data_criacao', 'parlamentares'] list_field_names = ['nome', 'data_criacao', 'parlamentares']
def has_permission(self):
return permissao_tb_aux(self)
class CreateView(CrudCreateView):
form_class = FrenteForm
class UpdateView(CrudUpdateView):
form_class = FrenteForm form_class = FrenteForm
class RelatoriaParlamentarCrud(MasterDetailCrud): class RelatoriaParlamentarCrud(MasterDetailCrud):
model = Relatoria model = Relatoria
parent_field = 'parlamentar' parent_field = 'parlamentar'
help_path = ''
class ListView(MasterDetailCrud.ListView): class ListView(MasterDetailCrud.ListView):
permission_required = permissoes_parlamentares() permission_required = permissoes_parlamentares()
@ -334,110 +328,69 @@ class FiliacaoCrud(MasterDetailCrud):
ordering = '-data' ordering = '-data'
def get_parlamentar_permissions():
lista_permissoes = []
cts = ContentType.objects.filter(app_label='parlamentares')
perms_parlamentares = list(Permission.objects.filter(
content_type__in=cts))
for p in perms_parlamentares:
lista_permissoes.append('parlamentares.' + p.codename)
return set(lista_permissoes)
class ParlamentarCrud(Crud): class ParlamentarCrud(Crud):
model = Parlamentar model = Parlamentar
help_path = ''
class DetailView(CrudDetailView):
def get_template_names(self): class BaseMixin(Crud.BaseMixin):
usuario = self.request.user form_class = ParlamentarCreateForm
lista_permissoes = get_parlamentar_permissions() list_field_names = [
'avatar_html', 'nome_parlamentar', 'filiacao_atual', 'ativo']
if usuario.has_perms(lista_permissoes): class DetailView(Crud.DetailView):
return ['crud/detail.html'] permission_required = []
else: def get_template_names(self):
return ['parlamentares/parlamentar_perfil_publico.html'] return ['crud/detail.html']\
if self.request.user.has_perm(self.permission(RP_CHANGE))\
else ['parlamentares/parlamentar_perfil_publico.html']
class UpdateView(CrudUpdateView): class UpdateView(Crud.UpdateView):
form_class = ParlamentarForm form_class = ParlamentarForm
permission_required = permissoes_parlamentares()
class CreateView(CrudCreateView): class CreateView(Crud.CreateView):
form_class = ParlamentarCreateForm
permission_required = permissoes_parlamentares()
@property @property
def layout_key(self): def layout_key(self):
return 'ParlamentarCreate' return 'ParlamentarCreate'
class DeleteView(CrudDeleteView): class ListView(Crud.ListView):
form_class = ParlamentarCreateForm permission_required = []
permission_required = permissoes_parlamentares()
class ListView(CrudListView):
template_name = "parlamentares/parlamentares_list.html" template_name = "parlamentares/parlamentares_list.html"
paginate_by = None paginate_by = None
ordering = '-nome_parlamentar'
def take_legislatura_id(self): def take_legislatura_id(self):
legislaturas = Legislatura.objects.all().order_by( try:
'-numero') return int(self.request.GET['periodo'])
except:
if legislaturas: for l in Legislatura.objects.all():
try: if l.atual():
legislatura_id = int(self.request.GET['periodo']) return l.id
except MultiValueDictKeyError:
for l in Legislatura.objects.all():
if l.atual():
return l.id
return legislatura_id
else:
return 0 return 0
def get_queryset(self): def get_queryset(self):
if self.take_legislatura_id() != 0: queryset = super().get_queryset()
mandatos = Mandato.objects.filter(
legislatura_id=self.take_legislatura_id()).order_by(
'parlamentar__nome_parlamentar')
return mandatos
return []
def get_rows(self, object_list): legislatura_id = self.take_legislatura_id()
parlamentares = [] if legislatura_id != 0:
for m in object_list: queryset = queryset.filter(
ultima_filiacao = m.parlamentar.filiacao_set.order_by( mandato__legislatura_id=legislatura_id)
'-data').first() return queryset
if ultima_filiacao and not ultima_filiacao.data_desfiliacao:
partido = ultima_filiacao.partido.sigla
else:
partido = _('Sem Partido')
parlamentar = [
("<img src=" + m.parlamentar.fotografia.url + " \
height='42' width='42' />" if m.parlamentar.fotografia
else '', ''),
(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): def get_headers(self):
return ['', 'Parlamentar', 'Partido', 'Ativo?'] return ['', _('Parlamentar'), _('Partido'), _('Ativo?')]
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(ParlamentarCrud.ListView, self context = super().get_context_data(**kwargs)
).get_context_data(**kwargs)
context.setdefault('title', self.verbose_name_plural)
# Adiciona legislatura para filtrar parlamentares # Adiciona legislatura para filtrar parlamentares
legislaturas = Legislatura.objects.all().order_by( legislaturas = Legislatura.objects.all().order_by('-numero')
'-numero')
context['legislaturas'] = legislaturas context['legislaturas'] = legislaturas
context['legislatura_id'] = self.take_legislatura_id() context['legislatura_id'] = self.take_legislatura_id()
# Tira Link do avatar_html e coloca no nome
for row in context['rows']:
row[1] = (row[1][0], row[0][1])
row[0] = (row[0][0], None)
return context return context

5
sapl/static/styles/app.scss

@ -154,6 +154,11 @@ fieldset {
} }
} }
.avatar-parlamentar {
height: 42px;
width: 42px;
}
/* INDEX */ /* INDEX */
#conteudo { #conteudo {
position: relative; position: relative;

2
sapl/templates/crud/list.html

@ -52,7 +52,7 @@
{% for value, href in value_list %} {% for value, href in value_list %}
<td> <td>
{% if href %} {% if href %}
<a href="{{ href }}">{{ value }}</a> <a href="{{ href }}">{{ value|safe }}</a>
{% elif valu != 'core.Cep.None' %} {% elif valu != 'core.Cep.None' %}
{{ value|safe }} {{ value|safe }}
{% endif %} {% endif %}

Loading…
Cancel
Save