<s> '''<STR_LIT>''' <EOL> from django . utils . datastructures import SortedDict <EOL> from rest_framework import serializers <EOL> from rest_framework . fields import ( <EOL> BooleanField , <EOL> CharField , <EOL> DateTimeField , <EOL> DecimalField , <EOL> FloatField , <EOL> IntegerField , <EOL> ) <EOL> from sqlalchemy . orm import class_mapper <EOL> from sqlalchemy . orm . properties import ColumnProperty , RelationshipProperty <EOL> from sqlalchemy . types import ( <EOL> BIGINT , <EOL> CHAR , <EOL> CLOB , <EOL> DATE , <EOL> DECIMAL , <EOL> INTEGER , <EOL> SMALLINT , <EOL> TIMESTAMP , <EOL> VARCHAR , <EOL> BigInteger , <EOL> Boolean , <EOL> DateTime , <EOL> Float , <EOL> Numeric , <EOL> String , <EOL> ) <EOL> from djangorest_alchemy . fields import AlchemyRelatedField , AlchemyUriField <EOL> from . inspector import KeyNotFoundException , primary_key <EOL> class AlchemyModelSerializer ( serializers . Serializer ) : <EOL> """<STR_LIT>""" <EOL> field_mapping = { <EOL> String : CharField , <EOL> INTEGER : IntegerField , <EOL> SMALLINT : IntegerField , <EOL> BIGINT : IntegerField , <EOL> VARCHAR : CharField , <EOL> CHAR : CharField , <EOL> TIMESTAMP : DateTimeField , <EOL> DATE : DateTimeField , <EOL> Float : FloatField , <EOL> BigInteger : IntegerField , <EOL> Numeric : IntegerField , <EOL> DateTime : DateTimeField , <EOL> Boolean : BooleanField , <EOL> CLOB : CharField , <EOL> DECIMAL : DecimalField , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> assert "<STR_LIT>" in kwargs , "<STR_LIT>" <EOL> assert '<STR_LIT>' in kwargs [ '<STR_LIT>' ] , "<STR_LIT>" <EOL> self . cls = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( AlchemyModelSerializer , self ) . __init__ ( * args , ** kwargs ) <EOL> def get_fields ( self ) : <EOL> ret = SortedDict ( ) <EOL> mapper = class_mapper ( self . cls . __class__ ) <EOL> r = self . context [ '<STR_LIT>' ] <EOL> try : <EOL> pk_field = primary_key ( self . cls . __class__ ) <EOL> ret [ '<STR_LIT>' ] = AlchemyUriField ( source = pk_field , <EOL> path = r . build_absolute_uri ( r . path ) , <EOL> read_only = True ) <EOL> except KeyNotFoundException : <EOL> pass <EOL> for col_prop in mapper . iterate_properties : <EOL> if isinstance ( col_prop , ColumnProperty ) : <EOL> field_nm = str ( col_prop ) . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> ] <EOL> field_cls = col_prop . columns [ <NUM_LIT:0> ] . type . __class__ <EOL> assert field_cls in self . field_mapping , "<STR_LIT>" % field_cls <EOL> ret [ field_nm ] = self . field_mapping [ field_cls ] ( ) <EOL> for rel_prop in mapper . iterate_properties : <EOL> if isinstance ( rel_prop , RelationshipProperty ) : <EOL> field_nm = str ( rel_prop ) . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> ] <EOL> kwargs = dict ( <EOL> path = r . build_absolute_uri ( r . path ) , <EOL> read_only = True <EOL> ) <EOL> if rel_prop . uselist : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> ret [ field_nm ] = AlchemyRelatedField ( ** kwargs ) <EOL> return ret <EOL> class AlchemyListSerializer ( AlchemyModelSerializer ) : <EOL> def get_fields ( self ) : <EOL> ret = SortedDict ( ) <EOL> try : <EOL> pk_field = primary_key ( self . cls . __class__ ) <EOL> request = self . context [ '<STR_LIT>' ] <EOL> ret [ "<STR_LIT>" ] = AlchemyUriField ( <EOL> source = pk_field , <EOL> path = request . build_absolute_uri ( request . path ) , <EOL> read_only = True , <EOL> ) <EOL> except KeyNotFoundException : <EOL> return super ( AlchemyListSerializer , self ) . get_fields ( ) <EOL> return ret </s>
