Browse Source

Move serializers da api para o core

pull/3453/head
Leandro Roberto 3 years ago
parent
commit
a581bdaccb
  1. 4
      sapl/api/core/__init__.py
  2. 50
      sapl/api/core/serializers.py
  3. 4
      sapl/api/deprecated.py
  4. 26
      sapl/api/serializers.py
  5. 2
      sapl/api/urls.py
  6. 13
      sapl/api/viewset.py

4
sapl/api/core/__init__.py

@ -22,8 +22,6 @@ from rest_framework.viewsets import ModelViewSet
from sapl.api.core.filters import SaplFilterSetMixin
from sapl.api.permissions import SaplModelPermissions
from sapl.api.serializers import ChoiceSerializer, ParlamentarSerializer, \
ParlamentarEditSerializer, ParlamentarResumeSerializer
# ATENÇÃO: MUDANÇAS NO CORE DEVEM SER REALIZADAS COM
# EXTREMA CAUTELA
@ -55,7 +53,7 @@ class SaplApiViewSetConstrutor():
@classonlymethod
def build_class(cls):
import inspect
from sapl.api import serializers
from sapl.api.core import serializers
# Carrega todas as classes de sapl.api.serializers que possuam
# "Serializer" como Sufixo.

50
sapl/api/core/serializers.py

@ -0,0 +1,50 @@
import logging
from django.conf import settings
from rest_framework import serializers
from rest_framework.relations import StringRelatedField
from sapl.base.models import CasaLegislativa
class IntRelatedField(StringRelatedField):
def to_representation(self, value):
return int(value)
class ChoiceSerializer(serializers.Serializer):
value = serializers.SerializerMethodField()
text = serializers.SerializerMethodField()
def get_text(self, obj):
return obj[1]
def get_value(self, obj):
return obj[0]
class ModelChoiceSerializer(ChoiceSerializer):
def get_text(self, obj):
return str(obj)
def get_value(self, obj):
return obj.id
class ModelChoiceObjectRelatedField(serializers.RelatedField):
def to_representation(self, value):
return ModelChoiceSerializer(value).data
class CasaLegislativaSerializer(serializers.ModelSerializer):
version = serializers.SerializerMethodField()
def get_version(self, obj):
return settings.SAPL_VERSION
class Meta:
model = CasaLegislativa
fields = '__all__'

4
sapl/api/deprecated.py

@ -19,8 +19,8 @@ from rest_framework.permissions import (IsAuthenticated,
IsAuthenticatedOrReadOnly, AllowAny)
from rest_framework.viewsets import GenericViewSet
from sapl.api.serializers import ModelChoiceSerializer, AutorSerializer, \
ChoiceSerializer
from sapl.api.core.serializers import ModelChoiceSerializer, ChoiceSerializer
from sapl.api.serializers import AutorSerializer
from sapl.base.models import TipoAutor, Autor, CasaLegislativa
from sapl.materia.models import MateriaLegislativa
from sapl.parlamentares.models import Legislatura

26
sapl/api/serializers.py

@ -5,9 +5,9 @@ from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.db.models import Q
from image_cropping.utils import get_backend
from rest_framework import serializers
from rest_framework.relations import StringRelatedField
from sapl.base.models import Autor, CasaLegislativa
from sapl.api.core.serializers import ModelChoiceObjectRelatedField
from sapl.base.models import Autor
from sapl.parlamentares.models import Parlamentar, Mandato, Legislatura
@ -22,18 +22,7 @@ class AutorSerializer(serializers.ModelSerializer):
fields = '__all__'
class CasaLegislativaSerializer(serializers.ModelSerializer):
version = serializers.SerializerMethodField()
def get_version(self, obj):
return settings.SAPL_VERSION
class Meta:
model = CasaLegislativa
fields = '__all__'
class ParlamentarSerializer(serializers.ModelSerializer):
class ParlamentarSerializerPublic(serializers.ModelSerializer):
class Meta:
model = Parlamentar
@ -43,14 +32,7 @@ class ParlamentarSerializer(serializers.ModelSerializer):
"telefone_residencia", "titulo_eleitor", "fax_residencia"]
class ParlamentarEditSerializer(serializers.ModelSerializer):
class Meta:
model = Parlamentar
fields = '__all__'
class ParlamentarResumeSerializer(serializers.ModelSerializer):
class ParlamentarSerializerVerbose(serializers.ModelSerializer):
titular = serializers.SerializerMethodField('check_titular')
partido = serializers.SerializerMethodField('check_partido')
fotografia_cropped = serializers.SerializerMethodField('crop_fotografia')

2
sapl/api/urls.py

@ -8,7 +8,7 @@ from sapl.api.deprecated import MateriaLegislativaViewSet, SessaoPlenariaViewSet
AutoresProvaveisListView, AutoresPossiveisListView, AutorListView, \
ModelChoiceView
from sapl.api.views import AppVersionView, recria_token
from sapl.api.views_customize import SaplApiViewSetConstrutor
from sapl.api.viewset import SaplApiViewSetConstrutor
from .apps import AppConfig

13
sapl/api/views_customize.py → sapl/api/viewset.py

@ -11,9 +11,10 @@ from rest_framework.response import Response
from sapl.api.core import customize, SaplApiViewSetConstrutor, \
wrapper_queryset_response_for_drf_action, \
BusinessRulesNotImplementedMixin
from sapl.api.core.serializers import ChoiceSerializer
from sapl.api.permissions import SaplModelPermissions
from sapl.api.serializers import ChoiceSerializer, \
ParlamentarEditSerializer, ParlamentarResumeSerializer
from sapl.api.serializers import ParlamentarSerializerVerbose, \
ParlamentarSerializerPublic
from sapl.base.models import Autor, AppConfig, DOC_ADM_OSTENSIVO
from sapl.materia.models import Proposicao, TipoMateriaLegislativa, \
MateriaLegislativa, Tramitacao
@ -112,8 +113,8 @@ class _ParlamentarViewSet:
permission_classes = (ParlamentarPermission,)
def get_serializer(self, *args, **kwargs):
if self.request.user.has_perm('parlamentares.add_parlamentar'):
self.serializer_class = ParlamentarEditSerializer
if not self.request.user.has_perm('parlamentares.add_parlamentar'):
self.serializer_class = ParlamentarSerializerPublic
return super().get_serializer(*args, **kwargs)
@action(detail=True)
@ -147,7 +148,7 @@ class _ParlamentarViewSet:
nome = request.query_params.get('nome_parlamentar', '')
parlamentares = Parlamentar.objects.filter(
nome_parlamentar__icontains=nome)
serializer_class = ParlamentarResumeSerializer(
serializer_class = ParlamentarSerializerVerbose(
parlamentares, many=True, context={'request': request})
return Response(serializer_class.data)
@ -164,7 +165,7 @@ class _LegislaturaViewSet:
}
def get_serializer_class():
return ParlamentarResumeSerializer
return ParlamentarSerializerVerbose
self.get_serializer_context = get_serializer_context
self.get_serializer_class = get_serializer_class
Loading…
Cancel
Save