<s> from django . db import transaction <EOL> from django . contrib . auth import get_user_model <EOL> from report_builder . models import Report , DisplayField , FilterField , Format <EOL> from rest_framework import serializers <EOL> import datetime <EOL> User = get_user_model ( ) <EOL> class FormatSerializer ( serializers . ModelSerializer ) : <EOL> class Meta : <EOL> model = Format <EOL> class DisplayFieldSerializer ( serializers . ModelSerializer ) : <EOL> class Meta : <EOL> model = DisplayField <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT:path>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:width>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> read_only_fields = ( '<STR_LIT:id>' ) <EOL> class FilterFieldSerializer ( serializers . ModelSerializer ) : <EOL> class Meta : <EOL> model = FilterField <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT:path>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> read_only_fields = ( '<STR_LIT:id>' , '<STR_LIT>' ) <EOL> class UserSerializer ( serializers . ModelSerializer ) : <EOL> class Meta : <EOL> model = User <EOL> fields = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:id>" ) <EOL> class ReportSerializer ( serializers . HyperlinkedModelSerializer ) : <EOL> root_model = serializers . PrimaryKeyRelatedField ( <EOL> queryset = Report . allowed_models ( ) ) <EOL> root_model_name = serializers . StringRelatedField ( source = '<STR_LIT>' ) <EOL> user_created = UserSerializer ( read_only = True ) <EOL> class Meta : <EOL> model = Report <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> class ReportNestedSerializer ( ReportSerializer ) : <EOL> displayfield_set = DisplayFieldSerializer ( required = False , many = True ) <EOL> filterfield_set = FilterFieldSerializer ( required = False , many = True ) <EOL> user_created = serializers . PrimaryKeyRelatedField ( read_only = True ) <EOL> user_modified = serializers . PrimaryKeyRelatedField ( read_only = True ) <EOL> class Meta : <EOL> model = Report <EOL> fields = ( <EOL> '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> read_only_fields = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def validate ( self , data ) : <EOL> if '<STR_LIT>' in data : <EOL> for filter_field_data in data [ '<STR_LIT>' ] : <EOL> filter_field = FilterField ( ) <EOL> for key , value in filter_field_data . items ( ) : <EOL> setattr ( filter_field , key , value ) <EOL> filter_field . clean ( ) <EOL> filter_field_data [ '<STR_LIT>' ] = filter_field . filter_value <EOL> return data <EOL> def update ( self , instance , validated_data ) : <EOL> displayfields_data = validated_data . pop ( '<STR_LIT>' ) <EOL> filterfields_data = validated_data . pop ( '<STR_LIT>' ) <EOL> with transaction . atomic ( ) : <EOL> instance . name = validated_data . get ( '<STR_LIT:name>' , instance . name ) <EOL> instance . description = validated_data . get ( '<STR_LIT:description>' , instance . description ) <EOL> instance . distinct = validated_data . get ( <EOL> '<STR_LIT>' , instance . distinct ) <EOL> instance . modified = datetime . date . today ( ) <EOL> instance . save ( ) <EOL> instance . displayfield_set . all ( ) . delete ( ) <EOL> for displayfield_data in displayfields_data : <EOL> display_field = DisplayField ( ) <EOL> for key , value in displayfield_data . items ( ) : <EOL> setattr ( display_field , key , value ) <EOL> display_field . save ( ) <EOL> instance . filterfield_set . all ( ) . delete ( ) <EOL> for filterfield_data in filterfields_data : <EOL> filter_field = FilterField ( ) <EOL> for key , value in filterfield_data . items ( ) : <EOL> setattr ( filter_field , key , value ) <EOL> filter_field . save ( ) <EOL> return instance </s>
