from rest_framework import viewsets

from paranuara_api.models import Company, Person
from paranuara_api.serializers import (
        CompanySerializer, CompanyListSerializer, PersonListSerializer,
        PersonSerializer
)

class CompanyViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Company.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': CompanyListSerializer,
        'retrieve': CompanySerializer,
    }

    def get_serializer_class(self):
        return self.serializers[self.action]


class PersonViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Person.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': PersonListSerializer,
        'retrieve': PersonSerializer,
    }

    def get_serializer_class(self):
        return self.serializers[self.action]


from rest_framework import viewsets

from paranuara_api.models import Company, Person
from paranuara_api.serializers import (
        CompanySerializer, CompanyListSerializer, PersonListSerializer,
        PersonSerializer
)

class MultiSerializerMixin(object):

    def get_serializer_class(self):
        return self.serializers[self.action]


class CompanyViewSet(MultiSerializerMixin, viewsets.ReadOnlyModelViewSet):
    queryset = Company.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': CompanyListSerializer,
        'retrieve': CompanySerializer,
    }


class PersonViewSet(MultiSerializerMixin, viewsets.ReadOnlyModelViewSet):
    queryset = Person.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': PersonListSerializer,
        'retrieve': PersonSerializer,
    }


Refactor common serializer selection code.
Refactor common serializer selection code.

  from rest_framework import viewsets
  
  from paranuara_api.models import Company, Person
  from paranuara_api.serializers import (
          CompanySerializer, CompanyListSerializer, PersonListSerializer,
          PersonSerializer
  )
  
+ class MultiSerializerMixin(object):
+ 
+     def get_serializer_class(self):
+         return self.serializers[self.action]
+ 
+ 
- class CompanyViewSet(viewsets.ReadOnlyModelViewSet):
+ class CompanyViewSet(MultiSerializerMixin, viewsets.ReadOnlyModelViewSet):
      queryset = Company.objects.all()
      lookup_field = 'index'
  
      serializers = {
          'list': CompanyListSerializer,
          'retrieve': CompanySerializer,
      }
  
-     def get_serializer_class(self):
-         return self.serializers[self.action]
  
- 
- class PersonViewSet(viewsets.ReadOnlyModelViewSet):
+ class PersonViewSet(MultiSerializerMixin, viewsets.ReadOnlyModelViewSet):
      queryset = Person.objects.all()
      lookup_field = 'index'
  
      serializers = {
          'list': PersonListSerializer,
          'retrieve': PersonSerializer,
      }
  
-     def get_serializer_class(self):
-         return self.serializers[self.action]
  
- 
Refactor common serializer selection code.
## Code Before:
from rest_framework import viewsets

from paranuara_api.models import Company, Person
from paranuara_api.serializers import (
        CompanySerializer, CompanyListSerializer, PersonListSerializer,
        PersonSerializer
)

class CompanyViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Company.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': CompanyListSerializer,
        'retrieve': CompanySerializer,
    }

    def get_serializer_class(self):
        return self.serializers[self.action]


class PersonViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Person.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': PersonListSerializer,
        'retrieve': PersonSerializer,
    }

    def get_serializer_class(self):
        return self.serializers[self.action]


## Instruction:
Refactor common serializer selection code.
## Code After:
from rest_framework import viewsets

from paranuara_api.models import Company, Person
from paranuara_api.serializers import (
        CompanySerializer, CompanyListSerializer, PersonListSerializer,
        PersonSerializer
)

class MultiSerializerMixin(object):

    def get_serializer_class(self):
        return self.serializers[self.action]


class CompanyViewSet(MultiSerializerMixin, viewsets.ReadOnlyModelViewSet):
    queryset = Company.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': CompanyListSerializer,
        'retrieve': CompanySerializer,
    }


class PersonViewSet(MultiSerializerMixin, viewsets.ReadOnlyModelViewSet):
    queryset = Person.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': PersonListSerializer,
        'retrieve': PersonSerializer,
    }

