From 650544f40833aba3ca3a3659d45bf696c0ddced7 Mon Sep 17 00:00:00 2001 From: Edward <9326037+edwardoliveira@users.noreply.github.com> Date: Mon, 16 Nov 2020 12:22:57 -0300 Subject: [PATCH] Permite exclude em Meta de ModelSerializers customizados (#3318) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Permite exclude em Meta de ModelSerializers * Refatora lógica * adiciona serializer de edição * Adiciona classe de edição * Adiciona campos em exclude Co-authored-by: eribeiro --- sapl/api/serializers.py | 18 +++++++++++------- sapl/api/views.py | 23 +++++++++++++++-------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/sapl/api/serializers.py b/sapl/api/serializers.py index 00bcbda86..2dcb518ba 100644 --- a/sapl/api/serializers.py +++ b/sapl/api/serializers.py @@ -67,13 +67,17 @@ class ParlamentarSerializer(serializers.ModelSerializer): class Meta: model = Parlamentar - fields = ["id", "nome_completo", - "nome_parlamentar", "sexo", - "data_nascimento", "numero_gab_parlamentar", - "telefone", "endereco_web", - "profissao", "email", - "locais_atuacao", "ativo", - "biografia", "fotografia", "nivel_instrucao"] + exclude = ["cpf", "rg", "fax", + "endereco_residencia", "municipio_residencia", + "uf_residencia", "cep_residencia", "situacao_militar", + "telefone_residencia", "titulo_eleitor", "fax_residencia"] + + +class ParlamentarEditSerializer(serializers.ModelSerializer): + + class Meta: + model = Parlamentar + fields = '__all__' class ParlamentarResumeSerializer(serializers.ModelSerializer): diff --git a/sapl/api/views.py b/sapl/api/views.py index 256c7b18d..05893ad00 100644 --- a/sapl/api/views.py +++ b/sapl/api/views.py @@ -29,7 +29,7 @@ from rest_framework.viewsets import ModelViewSet from sapl.api.forms import SaplFilterSetMixin from sapl.api.permissions import SaplModelPermissions -from sapl.api.serializers import ChoiceSerializer, ParlamentarResumeSerializer +from sapl.api.serializers import ChoiceSerializer, ParlamentarEditSerializer, ParlamentarResumeSerializer from sapl.base.models import Autor, AppConfig, DOC_ADM_OSTENSIVO from sapl.materia.models import Proposicao, TipoMateriaLegislativa,\ MateriaLegislativa, Tramitacao @@ -133,13 +133,16 @@ class SaplApiViewSetConstrutor(): if not hasattr(_meta_serializer, 'model'): model = _model - if not hasattr(_meta_serializer, 'fields'): - fields = '__all__' - elif _meta_serializer.fields != '__all__': - fields = list( - _meta_serializer.fields) + ['__str__', ] + if hasattr(_meta_serializer, 'exclude'): + exclude = _meta_serializer.exclude else: - fields = _meta_serializer.fields + if not hasattr(_meta_serializer, 'fields'): + fields = '__all__' + elif _meta_serializer.fields != '__all__': + fields = list( + _meta_serializer.fields) + ['__str__', ] + else: + fields = _meta_serializer.fields def get___str__(self, obj): return str(obj) @@ -347,6 +350,10 @@ class _AutorViewSet: class _ParlamentarViewSet: class ParlamentarPermission(SaplModelPermissions): def has_permission(self, request, view): + + if request.user.has_perm('parlamentares.add_parlamentar'): + self.serializer_class = ParlamentarEditSerializer + if request.method == 'GET': return True else: @@ -423,7 +430,7 @@ class _ParlamentarViewSet: @customize(Proposicao) -class _ProposicaoViewSet(): +class _ProposicaoViewSet: """ list: Retorna lista de Proposições