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):
model = CasaLegislativa
help_path = ''
class BaseMixin(CrudAux.BaseMixin):
list_field_names = ['codigo', 'nome', 'sigla']
@ -248,7 +247,6 @@ class HelpView(PermissionRequiredMixin, TemplateView):
class AppConfigCrud(CrudAux):
model = AppConfig
help_path = ''
class BaseMixin(CrudAux.BaseMixin):
form_class = ConfiguracoesAppForm

2
sapl/comissoes/views.py

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

9
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):
@ -49,7 +49,12 @@ class SaplFormLayout(Layout):
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)\
if hasattr(field, 'verbose_name') else ''
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.utils.decorators import classonlymethod
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 ugettext_lazy as _
from django.views.generic import (CreateView, DeleteView, DetailView, ListView,
UpdateView)
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.utils import normalize
logger = logging.getLogger(__name__)
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):
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):
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.auth.decorators import permission_required
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
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.crud.base import (Crud, CrudBaseMixin, CrudCreateView,
CrudDeleteView, CrudDetailView, CrudListView,
CrudUpdateView, MasterDetailCrud)
CrudUpdateView, MasterDetailCrud, CrudAux,
RP_CHANGE)
from sapl.materia.models import Proposicao, Relatoria
from sapl.utils import permissao_tb_aux, permissoes_parlamentares
@ -38,27 +42,17 @@ class FrenteList(ListView):
return context
class FrenteCrud(Crud):
class FrenteCrud(CrudAux):
model = Frente
help_path = ''
class BaseMixin(CrudBaseMixin):
class BaseMixin(CrudAux.BaseMixin):
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
class RelatoriaParlamentarCrud(MasterDetailCrud):
model = Relatoria
parent_field = 'parlamentar'
help_path = ''
class ListView(MasterDetailCrud.ListView):
permission_required = permissoes_parlamentares()
@ -334,110 +328,69 @@ class FiliacaoCrud(MasterDetailCrud):
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):
model = Parlamentar
help_path = ''
class DetailView(CrudDetailView):
def get_template_names(self):
usuario = self.request.user
lista_permissoes = get_parlamentar_permissions()
class BaseMixin(Crud.BaseMixin):
form_class = ParlamentarCreateForm
list_field_names = [
'avatar_html', 'nome_parlamentar', 'filiacao_atual', 'ativo']
if usuario.has_perms(lista_permissoes):
return ['crud/detail.html']
class DetailView(Crud.DetailView):
permission_required = []
else:
return ['parlamentares/parlamentar_perfil_publico.html']
def get_template_names(self):
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
permission_required = permissoes_parlamentares()
class CreateView(CrudCreateView):
form_class = ParlamentarCreateForm
permission_required = permissoes_parlamentares()
class CreateView(Crud.CreateView):
@property
def layout_key(self):
return 'ParlamentarCreate'
class DeleteView(CrudDeleteView):
form_class = ParlamentarCreateForm
permission_required = permissoes_parlamentares()
class ListView(CrudListView):
class ListView(Crud.ListView):
permission_required = []
template_name = "parlamentares/parlamentares_list.html"
paginate_by = None
ordering = '-nome_parlamentar'
def take_legislatura_id(self):
legislaturas = Legislatura.objects.all().order_by(
'-numero')
if legislaturas:
try:
legislatura_id = int(self.request.GET['periodo'])
except MultiValueDictKeyError:
for l in Legislatura.objects.all():
if l.atual():
return l.id
return legislatura_id
else:
try:
return int(self.request.GET['periodo'])
except:
for l in Legislatura.objects.all():
if l.atual():
return l.id
return 0
def get_queryset(self):
if self.take_legislatura_id() != 0:
mandatos = Mandato.objects.filter(
legislatura_id=self.take_legislatura_id()).order_by(
'parlamentar__nome_parlamentar')
return mandatos
return []
queryset = super().get_queryset()
def get_rows(self, object_list):
parlamentares = []
for m in object_list:
ultima_filiacao = m.parlamentar.filiacao_set.order_by(
'-data').first()
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
legislatura_id = self.take_legislatura_id()
if legislatura_id != 0:
queryset = queryset.filter(
mandato__legislatura_id=legislatura_id)
return queryset
def get_headers(self):
return ['', 'Parlamentar', 'Partido', 'Ativo?']
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)
context = super().get_context_data(**kwargs)
# Adiciona legislatura para filtrar parlamentares
legislaturas = Legislatura.objects.all().order_by(
'-numero')
legislaturas = Legislatura.objects.all().order_by('-numero')
context['legislaturas'] = legislaturas
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

5
sapl/static/styles/app.scss

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

2
sapl/templates/crud/list.html

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

Loading…
Cancel
Save