Browse Source

Permite exclude em Meta de ModelSerializers customizados (#3318)

* 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 <edwardr@senado.leg.br>
pull/3320/head
Edward 4 years ago
committed by GitHub
parent
commit
650544f408
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      sapl/api/serializers.py
  2. 11
      sapl/api/views.py

18
sapl/api/serializers.py

@ -67,13 +67,17 @@ class ParlamentarSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Parlamentar model = Parlamentar
fields = ["id", "nome_completo", exclude = ["cpf", "rg", "fax",
"nome_parlamentar", "sexo", "endereco_residencia", "municipio_residencia",
"data_nascimento", "numero_gab_parlamentar", "uf_residencia", "cep_residencia", "situacao_militar",
"telefone", "endereco_web", "telefone_residencia", "titulo_eleitor", "fax_residencia"]
"profissao", "email",
"locais_atuacao", "ativo",
"biografia", "fotografia", "nivel_instrucao"] class ParlamentarEditSerializer(serializers.ModelSerializer):
class Meta:
model = Parlamentar
fields = '__all__'
class ParlamentarResumeSerializer(serializers.ModelSerializer): class ParlamentarResumeSerializer(serializers.ModelSerializer):

11
sapl/api/views.py

@ -29,7 +29,7 @@ from rest_framework.viewsets import ModelViewSet
from sapl.api.forms import SaplFilterSetMixin from sapl.api.forms import SaplFilterSetMixin
from sapl.api.permissions import SaplModelPermissions 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.base.models import Autor, AppConfig, DOC_ADM_OSTENSIVO
from sapl.materia.models import Proposicao, TipoMateriaLegislativa,\ from sapl.materia.models import Proposicao, TipoMateriaLegislativa,\
MateriaLegislativa, Tramitacao MateriaLegislativa, Tramitacao
@ -133,6 +133,9 @@ class SaplApiViewSetConstrutor():
if not hasattr(_meta_serializer, 'model'): if not hasattr(_meta_serializer, 'model'):
model = _model model = _model
if hasattr(_meta_serializer, 'exclude'):
exclude = _meta_serializer.exclude
else:
if not hasattr(_meta_serializer, 'fields'): if not hasattr(_meta_serializer, 'fields'):
fields = '__all__' fields = '__all__'
elif _meta_serializer.fields != '__all__': elif _meta_serializer.fields != '__all__':
@ -347,6 +350,10 @@ class _AutorViewSet:
class _ParlamentarViewSet: class _ParlamentarViewSet:
class ParlamentarPermission(SaplModelPermissions): class ParlamentarPermission(SaplModelPermissions):
def has_permission(self, request, view): def has_permission(self, request, view):
if request.user.has_perm('parlamentares.add_parlamentar'):
self.serializer_class = ParlamentarEditSerializer
if request.method == 'GET': if request.method == 'GET':
return True return True
else: else:
@ -423,7 +430,7 @@ class _ParlamentarViewSet:
@customize(Proposicao) @customize(Proposicao)
class _ProposicaoViewSet(): class _ProposicaoViewSet:
""" """
list: list:
Retorna lista de Proposições Retorna lista de Proposições

Loading…
Cancel
Save