<s> from rest_framework import serializers <EOL> from rest_framework . exceptions import ValidationError <EOL> from crowdsourcing . serializers . dynamic import DynamicFieldsModelSerializer <EOL> from mturk . models import MTurkAccount <EOL> from mturk . interface import MTurkProvider <EOL> from crowdsourcing . crypto import AESUtil <EOL> from csp . settings import AWS_DAEMO_KEY , SITE_HOST <EOL> class MTurkAccountSerializer ( DynamicFieldsModelSerializer ) : <EOL> client_id = serializers . CharField ( required = True ) <EOL> client_secret = serializers . CharField ( write_only = True , required = True ) <EOL> class Meta : <EOL> model = MTurkAccount <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> read_only_fields = ( '<STR_LIT>' , ) <EOL> def create ( self , ** kwargs ) : <EOL> provider = MTurkProvider ( host = SITE_HOST , aws_access_key_id = self . validated_data [ '<STR_LIT>' ] , <EOL> aws_secret_access_key = self . validated_data [ '<STR_LIT>' ] ) <EOL> balance , is_valid = provider . test_connection ( ) <EOL> if not is_valid : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> client_secret = AESUtil ( key = AWS_DAEMO_KEY ) . encrypt ( self . validated_data . pop ( '<STR_LIT>' ) ) <EOL> if not hasattr ( kwargs . get ( '<STR_LIT:user>' ) , '<STR_LIT>' ) : <EOL> account = MTurkAccount . objects . create ( user = kwargs . get ( '<STR_LIT:user>' ) , client_secret = client_secret , <EOL> ** self . validated_data ) <EOL> return account <EOL> else : <EOL> kwargs . get ( '<STR_LIT:user>' ) . mturk_account . client_id = self . validated_data [ '<STR_LIT>' ] <EOL> kwargs . get ( '<STR_LIT:user>' ) . mturk_account . client_secret = client_secret <EOL> kwargs . get ( '<STR_LIT:user>' ) . mturk_account . save ( ) <EOL> return kwargs . get ( '<STR_LIT:user>' ) . mturk_account </s>
