From 3686447497de2376afbd2c83f7070eb0e7ca1dd4 Mon Sep 17 00:00:00 2001 From: Eduardo Calil Date: Wed, 29 Jun 2016 19:50:10 -0300 Subject: [PATCH] Implementa a Autorizacao para Proposicoes --- sapl/materia/layouts.yaml | 3 +-- sapl/materia/views.py | 54 ++++++++++++++++++++++++++++++++++++--- sapl/utils.py | 9 +++++++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/sapl/materia/layouts.yaml b/sapl/materia/layouts.yaml index 3ee501bea..e5b3a4cb5 100644 --- a/sapl/materia/layouts.yaml +++ b/sapl/materia/layouts.yaml @@ -97,8 +97,7 @@ ProposicaoCreate: Proposição: - tipo data_envio - descricao - Materia: - - tipo_materia numero_materia ano_materia + Complemento: - texto_original diff --git a/sapl/materia/views.py b/sapl/materia/views.py index 90d069c0b..519b45bb7 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -23,12 +23,14 @@ from django_filters.views import FilterView from sapl.base.models import CasaLegislativa from sapl.compilacao.views import IntegracaoTaView from sapl.crispy_layout_mixin import SaplFormLayout, form_actions, to_row -from sapl.crud.base import (Crud, CrudBaseMixin, CrudCreateView, CrudListView, +from sapl.crud.base import (Crud, CrudBaseMixin, CrudCreateView, + CrudDetailView, CrudListView, CrudUpdateView, CrudDeleteView, make_pagination) from sapl.crud.masterdetail import MasterDetailCrud from sapl.norma.models import LegislacaoCitada from sapl.utils import (autor_label, autor_modal, get_base_url, - permissoes_materia, permissao_tb_aux) + permissoes_autor, permissoes_materia, + permissao_tb_aux) from .forms import (AcompanhamentoMateriaForm, AnexadaForm, AutorForm, AutoriaForm, DespachoInicialForm, DocumentoAcessorioForm, @@ -219,14 +221,53 @@ class ProposicaoCrud(Crud): class UpdateView(PermissionRequiredMixin, CrudUpdateView): form_class = ProposicaoForm - permission_required = permissoes_materia() + permission_required = permissoes_autor() @property def layout_key(self): return 'ProposicaoCreate' - class ListView(CrudListView): + def has_permission(self): + perms = self.get_permission_required() + if self.request.user.has_perms(perms): + if (Proposicao.objects.filter( + id=self.kwargs['pk'], + autor__user_id=self.request.user.id).exists()): + proposicao = Proposicao.objects.get( + id=self.kwargs['pk'], + autor__user_id=self.request.user.id) + if not proposicao.data_recebimento: + return True + else: + msg = _('Essa proposição já foi recebida. ' + + 'Não pode mais ser editada') + messages.add_message(self.request, messages.ERROR, msg) + return False + else: + return False + + class DetailView(PermissionRequiredMixin, CrudDetailView): + permission_required = permissoes_autor() + + @property + def layout_key(self): + return 'ProposicaoCreate' + + def has_permission(self): + perms = self.get_permission_required() + if self.request.user.has_perms(perms): + if (Proposicao.objects.filter( + id=self.kwargs['pk'], + autor__user_id=self.request.user.id).exists()): + return True + else: + return False + else: + return False + + class ListView(PermissionRequiredMixin, CrudListView): ordering = ['-data_envio', 'descricao'] + permission_required = permissoes_autor() def get_rows(self, object_list): @@ -236,6 +277,11 @@ class ProposicaoCrud(Crud): return [self._as_row(obj) for obj in object_list] + def get_queryset(self): + lista = Proposicao.objects.filter( + autor__user_id=self.request.user.id) + return lista + class DeleteView(PermissionRequiredMixin, CrudDeleteView): permission_required = permissoes_materia() diff --git a/sapl/utils.py b/sapl/utils.py index 3fefa1bca..63f233a82 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -302,3 +302,12 @@ def permissao_tb_aux(self): return True else: return False + + +def permissoes_autor(): + lista_permissoes = [] + perms_autor = list(Permission.objects.filter( + group__name='Autor')) + for p in perms_autor: + lista_permissoes.append('materia.' + p.codename) + return set(lista_permissoes)