Browse Source

Refatora cruds da app sessao para usar novo Crud

pull/675/head
LeandroRoberto 8 years ago
parent
commit
35e5d50c4e
  1. 2
      sapl/crispy_layout_mixin.py
  2. 28
      sapl/crud/base.py
  3. 2
      sapl/parlamentares/views.py
  4. 1
      sapl/sessao/models.py
  5. 12
      sapl/sessao/urls.py
  6. 187
      sapl/sessao/views.py
  7. 2
      sapl/templates/parlamentares/composicaomesa_form.html
  8. 2
      sapl/templates/sessao/mesa.html

2
sapl/crispy_layout_mixin.py

@ -150,7 +150,7 @@ class CrispyLayoutFormMixin:
in the first fieldset of the layout.
'''
obj = self.crud if hasattr(self, 'crud') else self
if hasattr(obj, 'list_field_names'):
if hasattr(obj, 'list_field_names') and obj.list_field_names:
return obj.list_field_names
rows = self.get_layout()[0][1:]
return [fieldname for row in rows for fieldname, __ in row]

28
sapl/crud/base.py

@ -668,7 +668,9 @@ class CrudDetailView(PermissionRequiredContainerCrudMixin,
for i, name in enumerate(self.list_field_names_set)]
def get_object(self, queryset=None):
if hasattr(self, 'object'):
return self.object
return DetailView.get_object(self, queryset=queryset)
def get(self, request, *args, **kwargs):
self.object = self.model.objects.get(pk=kwargs.get('pk'))
@ -680,7 +682,10 @@ class CrudDetailView(PermissionRequiredContainerCrudMixin,
def get_queryset(self):
obj = self.crud if hasattr(self, 'crud') else self
if hasattr(obj, 'model_set') and obj.model_set:
queryset = getattr(self.object, obj.model_set).all()
else:
queryset = super().get_queryset()
if not self.request.user.is_authenticated():
return queryset
@ -782,13 +787,6 @@ class CrudDeleteView(PermissionRequiredContainerCrudMixin,
return self.list_url
class DeactivatedMixin(View):
@classmethod
def get_url_regex(cls):
return r'^dummy$'
class Crud:
BaseMixin = CrudBaseMixin
ListView = CrudListView
@ -842,28 +840,16 @@ class Crud:
@classonlymethod
def build(cls, _model, _help_path, _model_set=None, list_field_names=[]):
def create_class_with_list_field(_list_field_names):
def create_class(_list_field_names):
class ModelCrud(cls):
model = _model
model_set = _model_set
help_path = _help_path
class BaseMixin(cls.BaseMixin):
list_field_names = _list_field_names
return ModelCrud
def create_class():
class ModelCrud(cls):
model = _model
model_set = _model_set
help_path = _help_path
return ModelCrud
ModelCrud = create_class() if not list_field_names\
else create_class_with_list_field(list_field_names)
ModelCrud = create_class(list_field_names)
ModelCrud.__name__ = '%sCrud' % _model.__name__
return ModelCrud

2
sapl/parlamentares/views.py

@ -15,7 +15,7 @@ from sapl.comissoes.models import Participacao
from sapl.crud.base import (Crud, CrudBaseMixin, CrudCreateView,
CrudDeleteView, CrudDetailView, CrudListView,
CrudUpdateView, MasterDetailCrud, CrudAux,
RP_CHANGE, RP_DETAIL, RP_LIST, DeactivatedMixin,
RP_CHANGE, RP_DETAIL, RP_LIST,
CrudBaseForListAndDetailExternalAppView)
from sapl.materia.models import Proposicao, Relatoria
from sapl.parlamentares.apps import AppConfig

1
sapl/sessao/models.py

@ -41,6 +41,7 @@ class Bancada(models.Model):
class Meta:
verbose_name = _('Bancada')
verbose_name_plural = _('Bancadas')
ordering = ('-legislatura__numero', )
def __str__(self):
return self.nome

12
sapl/sessao/urls.py

@ -39,6 +39,7 @@ urlpatterns = [
ExpedienteMateriaCrud.get_urls() +
MateriaOrdemDiaCrud.get_urls())),
url(r'^sessao/(?P<pk>\d+)/mesa$', MesaView.as_view(), name='mesa'),
url(r'^recuperar-materia/', recuperar_materia),
url(r'^recuperar-numero-sessao/', recuperar_numero_sessao),
@ -86,17 +87,16 @@ urlpatterns = [
PautaOrdemDetail.as_view(), name='pauta_ordem_detail'),
# Subnav sessão
url(r'^(?P<pk>\d+)/expediente$',
url(r'^sessao/(?P<pk>\d+)/expediente$',
ExpedienteView.as_view(), name='expediente'),
url(r'^(?P<pk>\d+)/presenca$',
url(r'^sessao/(?P<pk>\d+)/presenca$',
PresencaView.as_view(), name='presenca'),
url(r'^(?P<pk>\d+)/painel$',
url(r'^sessao/(?P<pk>\d+)/painel$',
PainelView.as_view(), name='painel'),
url(r'^(?P<pk>\d+)/presencaordemdia$',
url(r'^sessao/(?P<pk>\d+)/presencaordemdia$',
PresencaOrdemDiaView.as_view(),
name='presencaordemdia'),
url(r'^(?P<pk>\d+)/mesa$', MesaView.as_view(), name='mesa'),
url(r'^(?P<pk>\d+)/resumo$',
url(r'^sessao/(?P<pk>\d+)/resumo$',
ResumoView.as_view(), name='resumo'),
url(r'^sessao/pesquisar-sessao$',
PesquisarSessaoPlenariaView.as_view(), name='pesquisar_sessao'),

187
sapl/sessao/views.py

@ -3,22 +3,24 @@ from re import sub
from braces.views import PermissionRequiredMixin
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.urlresolvers import reverse
from django.forms.utils import ErrorList
from django.http import JsonResponse
from django.http.response import HttpResponseRedirect
from django.utils.datastructures import MultiValueDictKeyError
from django.utils.decorators import method_decorator
from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _
from django.views.generic import ListView, TemplateView
from django.views.generic.detail import DetailView
from django.views.generic.edit import FormMixin
from django_filters.views import FilterView
from rest_framework import generics
from sapl.crud.base import (Crud, CrudBaseMixin, CrudCreateView,
CrudDeleteView, CrudDetailView, CrudListView,
CrudUpdateView, MasterDetailCrud, make_pagination)
from sapl.crud.base import (Crud, MasterDetailCrud, make_pagination,
CrudAux, RP_CHANGE, RP_DETAIL, RP_LIST,)
from sapl.materia.forms import pega_ultima_tramitacao
from sapl.materia.models import (Autoria, DocumentoAcessorio,
TipoMateriaLegislativa, Tramitacao)
@ -26,6 +28,7 @@ from sapl.materia.views import MateriaLegislativaPesquisaView
from sapl.norma.models import NormaJuridica
from sapl.parlamentares.models import (Parlamentar, SessaoLegislativa,
Legislatura)
from sapl.sessao.apps import AppConfig
from sapl.sessao.serializers import SessaoPlenariaSerializer
from sapl.utils import permissao_tb_aux, permissoes_painel, permissoes_sessao
@ -42,9 +45,23 @@ from .models import (Bancada, Bloco, CargoBancada, CargoMesa,
SessaoPlenariaPresenca, TipoExpediente,
TipoResultadoVotacao, TipoSessaoPlenaria, VotoParlamentar)
OrdemDiaCrud = Crud.build(OrdemDia, '')
RegistroVotacaoCrud = Crud.build(RegistroVotacao, '')
TipoSessaoCrud = CrudAux.build(TipoSessaoPlenaria, 'tipo_sessao_plenaria')
TipoExpedienteCrud = CrudAux.build(TipoExpediente, 'tipo_expediente')
CargoBancadaCrud = CrudAux.build(CargoBancada, '')
TipoSessaoCrud = CrudAux.build(TipoSessaoPlenaria, 'tipo_sessao_plenaria')
TipoSessaoCrud = CrudAux.build(TipoSessaoPlenaria, 'tipo_sessao_plenaria')
BlocoCrud = CrudAux.build(
Bloco, '', list_field_names=['nome', 'data_criacao', 'partidos'])
BancadaCrud = CrudAux.build(
Bancada, '', list_field_names=['nome', 'legislatura'])
TipoResultadoVotacaoCrud = CrudAux.build(
TipoResultadoVotacao, 'tipo_resultado_votacao')
def reordernar_materias_expediente(request, pk):
expedientes = ExpedienteMateria.objects.filter(
@ -72,77 +89,6 @@ def reordernar_materias_ordem(request, pk):
reverse('sapl.sessao:ordemdia_list', kwargs={'pk': pk}))
class BlocoCrud(Crud):
model = Bloco
help_path = ''
class BaseMixin(CrudBaseMixin):
list_field_names = ['nome', 'data_criacao', 'partidos']
def has_permission(self):
return permissao_tb_aux(self)
class BancadaCrud(Crud):
model = Bancada
help_path = ''
class BaseMixin(CrudBaseMixin):
list_field_names = ['nome', 'legislatura']
def has_permission(self):
return permissao_tb_aux(self)
class ListView(CrudListView):
ordering = 'legislatura'
class CreateView(CrudCreateView):
form_class = BancadaForm
class UpdateView(CrudUpdateView):
form_class = BancadaForm
class TipoSessaoCrud(Crud):
model = TipoSessaoPlenaria
help_path = 'tipo_sessao_plenaria'
class BaseMixin(CrudBaseMixin):
def has_permission(self):
return permissao_tb_aux(self)
class TipoResultadoVotacaoCrud(Crud):
model = TipoResultadoVotacao
help_path = 'tipo_resultado_votacao'
class BaseMixin(CrudBaseMixin):
def has_permission(self):
return permissao_tb_aux(self)
class TipoExpedienteCrud(Crud):
model = TipoExpediente
help_path = 'tipo_expediente'
class BaseMixin(CrudBaseMixin):
def has_permission(self):
return permissao_tb_aux(self)
class CargoBancadaCrud(Crud):
model = CargoBancada
help_path = ''
class BaseMixin(CrudBaseMixin):
def has_permission(self):
return permissao_tb_aux(self)
def abrir_votacao_expediente_view(request, pk, spk):
existe_votacao_aberta = ExpedienteMateria.objects.filter(
sessao_plenaria_id=spk, votacao_aberta=True
@ -245,7 +191,7 @@ class MateriaOrdemDiaCrud(MasterDetailCrud):
url = reverse('sapl.sessao:abrir_votacao', kwargs={
'pk': obj.pk, 'spk': obj.sessao_plenaria_id})
if self.request.user.has_perms(permissoes_sessao()):
if self.request.user.has_module_perms(AppConfig.label):
btn_abrir = '''
Matéria não votada<br />
<a href="%s"
@ -255,7 +201,7 @@ class MateriaOrdemDiaCrud(MasterDetailCrud):
else:
obj.resultado = '''Não há resultado'''
else:
if self.request.user.has_perms(permissoes_sessao()):
if self.request.user.has_module_perms(AppConfig.label):
url = ''
if obj.tipo_votacao == 1:
url = reverse('sapl.sessao:votacaosimbolicaedit',
@ -309,7 +255,7 @@ class ExpedienteMateriaCrud(MasterDetailCrud):
list_field_names = ['numero_ordem', 'materia',
'observacao', 'resultado']
class ListView(MasterDetailCrud.ListView):
class ListView(Crud.PublicMixin, MasterDetailCrud.ListView):
ordering = ['numero_ordem', 'materia', 'resultado']
def get_rows(self, object_list):
@ -349,6 +295,7 @@ class ExpedienteMateriaCrud(MasterDetailCrud):
<a href="%s"
class="btn btn-primary"
role="button">Abrir Votação</a>''' % (url)
obj.resultado = btn_abrir
else:
url = ''
@ -376,7 +323,6 @@ class ExpedienteMateriaCrud(MasterDetailCrud):
class CreateView(MasterDetailCrud.CreateView):
form_class = ExpedienteMateriaForm
permission_required = permissoes_sessao()
def get_success_url(self):
return reverse('sapl.sessao:expedientemateria_list',
@ -384,7 +330,6 @@ class ExpedienteMateriaCrud(MasterDetailCrud):
class UpdateView(MasterDetailCrud.UpdateView):
form_class = ExpedienteMateriaForm
permission_required = permissoes_sessao()
def get_initial(self):
self.initial['tipo_materia'] = self.object.materia.tipo.id
@ -392,9 +337,6 @@ class ExpedienteMateriaCrud(MasterDetailCrud):
self.initial['ano_materia'] = self.object.materia.ano
return self.initial
class DeleteView(PermissionRequiredMixin, MasterDetailCrud.DeleteView):
permission_required = permissoes_sessao()
class DetailView(MasterDetailCrud.DetailView):
@property
@ -406,25 +348,16 @@ class OradorCrud(MasterDetailCrud):
model = ''
parent_field = 'sessao_plenaria'
help_path = ''
public = [RP_LIST, RP_DETAIL]
class ListView(MasterDetailCrud.ListView):
ordering = ['numero_ordem', 'parlamentar']
class CreateView(MasterDetailCrud.CreateView):
permission_required = permissoes_sessao()
class UpdateView(MasterDetailCrud.UpdateView):
permission_required = permissoes_sessao()
class DeleteView(MasterDetailCrud.DeleteView):
permission_required = permissoes_sessao()
class OradorExpedienteCrud(OradorCrud):
model = OradorExpediente
class CreateView(MasterDetailCrud.CreateView):
permission_required = permissoes_sessao()
form_class = OradorExpedienteForm
def get_success_url(self):
@ -432,18 +365,13 @@ class OradorExpedienteCrud(OradorCrud):
kwargs={'pk': self.kwargs['pk']})
class UpdateView(MasterDetailCrud.UpdateView):
permission_required = permissoes_sessao()
form_class = OradorExpedienteForm
class DeleteView(MasterDetailCrud.DeleteView):
permission_required = permissoes_sessao()
class OradorCrud(OradorCrud):
model = Orador
class CreateView(MasterDetailCrud.CreateView):
permission_required = permissoes_sessao()
form_class = OradorForm
def get_success_url(self):
@ -451,12 +379,8 @@ class OradorCrud(OradorCrud):
kwargs={'pk': self.kwargs['pk']})
class UpdateView(MasterDetailCrud.UpdateView):
permission_required = permissoes_sessao()
form_class = OradorForm
class DeleteView(MasterDetailCrud.DeleteView):
permission_required = permissoes_sessao()
def recuperar_numero_sessao(request):
try:
@ -473,22 +397,19 @@ def recuperar_numero_sessao(request):
class SessaoCrud(Crud):
model = SessaoPlenaria
help_path = 'sessao_plenaria'
public = [RP_DETAIL]
class BaseMixin(CrudBaseMixin):
class BaseMixin(Crud.BaseMixin):
list_field_names = ['data_inicio', 'legislatura', 'sessao_legislativa',
'tipo']
# FIXME!!!! corrigir referencias no codigo e remover isso!!!!!
# fazer com #230
class CrudDetailView(CrudDetailView):
class CrudDetailView(DetailView):
model = SessaoPlenaria
help_path = 'sessao_plenaria'
class ListView(CrudListView):
class ListView(Crud.ListView):
ordering = ['-data_inicio']
class CreateView(CrudCreateView):
permission_required = permissoes_sessao()
class CreateView(Crud.CreateView):
def get_initial(self):
legislatura = Legislatura.objects.order_by('-numero')[0]
@ -497,12 +418,6 @@ class SessaoCrud(Crud):
return {'legislatura': legislatura,
'sessao_legislativa': sessao_legislativa}
class UpdateView(CrudUpdateView):
permission_required = permissoes_sessao()
class DeleteView(CrudDeleteView):
permission_required = permissoes_sessao()
class PresencaMixin:
@ -535,18 +450,24 @@ class PresencaMixin:
yield (parlamentar, False)
class PresencaView(FormMixin,
PresencaMixin,
SessaoCrud.CrudDetailView):
class PresencaView(FormMixin, PresencaMixin, DetailView):
template_name = 'sessao/presenca.html'
form_class = PresencaForm
model = SessaoPlenaria
@method_decorator(permission_required((
'%s.add_%s' % (
AppConfig.label, SessaoPlenariaPresenca._meta.model_name),
'%s.change_%s' % (
AppConfig.label, SessaoPlenariaPresenca._meta.model_name),
'%s.delete_%s' % (
AppConfig.label, SessaoPlenariaPresenca._meta.model_name),
)))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if not self.request.user.has_perms(permissoes_sessao()):
if not self.request.user.has_module_perms(AppConfig.label):
return self.form_invalid(form)
if form.is_valid():
@ -586,12 +507,16 @@ class PainelView(PermissionRequiredMixin, TemplateView):
permission_required = permissoes_painel()
class PresencaOrdemDiaView(FormMixin,
PresencaMixin,
SessaoCrud.CrudDetailView):
class PresencaOrdemDiaView(FormMixin, PresencaMixin, DetailView):
template_name = 'sessao/presenca_ordemdia.html'
form_class = PresencaForm
model = SessaoPlenaria
@method_decorator(permission_required((
'%s.add_%s' % (AppConfig.label, PresencaOrdemDia._meta.model_name),
'%s.change_%s' % (AppConfig.label, PresencaOrdemDia._meta.model_name),
'%s.delete_%s' % (AppConfig.label, PresencaOrdemDia._meta.model_name),
)))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
@ -634,9 +559,10 @@ class PresencaOrdemDiaView(FormMixin,
return reverse('sapl.sessao:presencaordemdia', kwargs={'pk': pk})
class ListMateriaOrdemDiaView(FormMixin, SessaoCrud.CrudDetailView):
class ListMateriaOrdemDiaView(FormMixin, DetailView):
template_name = 'sessao/materia_ordemdia_list.html'
form_class = ListMateriaForm
model = SessaoPlenaria
def get(self, request, *args, **kwargs):
self.object = self.get_object()
@ -673,6 +599,11 @@ class ListMateriaOrdemDiaView(FormMixin, SessaoCrud.CrudDetailView):
return self.render_to_response(context)
@method_decorator(permission_required((
'%s.add_%s' % (AppConfig.label, OrdemDia._meta.model_name),
'%s.change_%s' % (AppConfig.label, OrdemDia._meta.model_name),
'%s.delete_%s' % (AppConfig.label, OrdemDia._meta.model_name),
)))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
@ -737,12 +668,13 @@ class ListMateriaOrdemDiaView(FormMixin, SessaoCrud.CrudDetailView):
return self.get(self, request, args, kwargs)
class MesaView(PermissionRequiredMixin, FormMixin, SessaoCrud.CrudDetailView):
class MesaView(FormMixin, DetailView):
template_name = 'sessao/mesa.html'
form_class = MesaForm
permission_required = permissoes_sessao()
model = SessaoPlenaria
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
@ -762,6 +694,11 @@ class MesaView(PermissionRequiredMixin, FormMixin, SessaoCrud.CrudDetailView):
return self.render_to_response(context)
@method_decorator(permission_required((
'%s.add_integrantemesa' % AppConfig.label,
'%s.change_integrantemesa' % AppConfig.label,
'%s.delete_integrantemesa' % AppConfig.label,
)))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = MesaForm(request.POST)

2
sapl/templates/parlamentares/composicaomesa_form.html

@ -48,7 +48,7 @@
<div class="col-md-4" align="center">
<br /><br />
{% if cargos_vagos %}
{% if perms.parlamentares.add_cargomesa%}
{% if perms.parlamentares.add_cargomesa %}
<input type="submit" name="Incluir" Value="Incluir" class="btn btn-primary" />
{% endif %}
{% endif %}

2
sapl/templates/sessao/mesa.html

@ -20,10 +20,12 @@
</div>
<div class="col-md-4" align="center">
{% if perms.sessao %}
<br /><br />
{% if view.get_cargos_mesa %}<input type="submit" name="Incluir" Value="Incluir" class="btn btn-primary" />{% endif %}
<br /><br />
<input type="submit" name="Excluir" Value="Excluir" class="btn btn-danger" />
{% endif %}
</div>
{% if view.get_cargos_mesa %}
<div class="col-md-4">

Loading…
Cancel
Save