<s> import logging <EOL> from django . db import models <EOL> from rest_framework import serializers <EOL> from ralph . admin . sites import ralph_site <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class QuerysetRelatedMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> select_related = None <EOL> prefetch_related = None <EOL> _skip_admin_list_select_related = False <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . select_related = kwargs . pop ( <EOL> '<STR_LIT>' , <EOL> self . select_related <EOL> ) or [ ] <EOL> self . prefetch_related = kwargs . pop ( <EOL> '<STR_LIT>' , self . prefetch_related <EOL> ) or [ ] <EOL> if getattr ( self , '<STR_LIT>' , None ) is not None : <EOL> admin_site = ralph_site . _registry . get ( self . queryset . model ) <EOL> if ( <EOL> admin_site and <EOL> not self . _skip_admin_list_select_related and <EOL> admin_site . list_select_related <EOL> ) : <EOL> self . select_related . extend ( admin_site . list_select_related ) <EOL> super ( ) . __init__ ( * args , ** kwargs ) <EOL> def get_queryset ( self ) : <EOL> queryset = super ( ) . get_queryset ( ) <EOL> if self . select_related : <EOL> queryset = queryset . select_related ( * self . select_related ) <EOL> if self . prefetch_related : <EOL> queryset = queryset . prefetch_related ( * self . prefetch_related ) <EOL> return queryset <EOL> class PolymorphicViewSetMixin ( QuerysetRelatedMixin ) : <EOL> """<STR_LIT>""" <EOL> def get_queryset ( self ) : <EOL> queryset = super ( ) . get_queryset ( ) <EOL> polymorphic_select_related = { } <EOL> for model , view in self . _viewsets_registry . items ( ) : <EOL> if model in queryset . model . _polymorphic_descendants : <EOL> polymorphic_select_related [ model . _meta . object_name ] = ( <EOL> view . select_related <EOL> ) <EOL> return queryset . polymorphic_select_related ( ** polymorphic_select_related ) <EOL> def get_serializer ( self , * args , ** kwargs ) : <EOL> serializer_class = self . get_serializer_class ( ) <EOL> kwargs [ '<STR_LIT>' ] = self . get_serializer_context ( ) <EOL> if not kwargs . get ( '<STR_LIT>' ) : <EOL> try : <EOL> serializer_class = serializer_class . _serializers_registry [ <EOL> args [ <NUM_LIT:0> ] . __class__ <EOL> ] <EOL> except KeyError : <EOL> logger . warning ( '<STR_LIT>' . format ( <EOL> args [ <NUM_LIT:0> ] . __class__ <EOL> ) ) <EOL> return serializer_class ( * args , ** kwargs ) <EOL> class PolymorphicListSerializer ( serializers . ListSerializer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . child_serializers = kwargs . pop ( '<STR_LIT>' ) <EOL> return super ( ) . __init__ ( * args , ** kwargs ) <EOL> def to_representation ( self , data ) : <EOL> iterable = data . all ( ) if isinstance ( data , models . Manager ) else data <EOL> return [ <EOL> self . child_serializers [ item . __class__ ] . to_representation ( item ) <EOL> for item in iterable <EOL> ] <EOL> class PolymorphicSerializer ( serializers . Serializer ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def many_init ( cls , * args , ** kwargs ) : <EOL> child_serializer = cls ( * args , ** kwargs ) <EOL> child_serializers = { } <EOL> for descendant_model in cls . Meta . model . _polymorphic_descendants : <EOL> child_serializers [ descendant_model ] = cls . _serializers_registry . get ( <EOL> descendant_model , cls <EOL> ) ( * args , ** kwargs ) <EOL> list_kwargs = { <EOL> '<STR_LIT>' : child_serializer , <EOL> '<STR_LIT>' : child_serializers , <EOL> } <EOL> list_kwargs . update ( dict ( [ <EOL> ( key , value ) for key , value in kwargs . items ( ) <EOL> if key in serializers . LIST_SERIALIZER_KWARGS <EOL> ] ) ) <EOL> return PolymorphicListSerializer ( * args , ** list_kwargs ) </s>
