class IBOnboardCreateSerializer(serializers.ModelSerializer): """ For Update (POST) requests only """ employer_address = AddressUpdateSerializer(required=False) tax_address = AddressUpdateSerializer(required=False) class Meta: model = IBOnboard fields = '__all__' def create(self, validated_data): if 'employer_address' in validated_data: employer_address_ser = AddressUpdateSerializer( data=validated_data.pop('employer_address')) employer_address_ser.is_valid(raise_exception=True) validated_data['employer_address'] = employer_address_ser.save() if 'tax_address' in validated_data: tax_address_ser = AddressUpdateSerializer( data=validated_data.pop('tax_address')) tax_address_ser.is_valid(raise_exception=True) validated_data['tax_address'] = tax_address_ser.save() ib_onboard = super(IBOnboardCreateSerializer, self).create(validated_data) return ib_onboard
def create(self, validated_data): # Default to Personal account type for risk profile group on a brand # new client (since they have no accounts yet, we have to assume) rpg = RiskProfileGroup.objects.get( account_types__account_type=constants.ACCOUNT_TYPE_PERSONAL) validated_data['risk_profile_group'] = rpg address_ser = AddressUpdateSerializer( data=validated_data.pop(RESIDENTIAL_ADDRESS_KEY)) address_ser.is_valid(raise_exception=True) validated_data[RESIDENTIAL_ADDRESS_KEY] = address_ser.save() if 'ib_onboard' in validated_data: ib_onboard_ser = IBOnboardCreateSerializer( data=validated_data.pop('ib_onboard')) ib_onboard_ser.is_valid(raise_exception=True) # For now we auto confirm and approve the client. validated_data['is_confirmed'] = True validated_data['is_accepted'] = True client = super(ClientCreateSerializer, self).create(validated_data) client.primary_accounts.create( account_type=constants.ACCOUNT_TYPE_PERSONAL, default_portfolio_set=validated_data['advisor']. default_portfolio_set, confirmed=True, ) if 'ib_onboard_ser' in locals(): ib_onboard = ib_onboard_ser.save(client=client) # connect IB broker status = onboarding_helpers.onboarding_from_invitation(ib_onboard) return client
def update(self, instance, validated_data): add_data = validated_data.pop(RESIDENTIAL_ADDRESS_KEY, None) if add_data is not None: address_ser = AddressUpdateSerializer(data=add_data) address_ser.is_valid(raise_exception=True) validated_data[RESIDENTIAL_ADDRESS_KEY] = address_ser.save() return super(ClientUpdateSerializer, self).update(instance, validated_data)
def create(self, validated_data): employer_address_ser = AddressUpdateSerializer( data=validated_data.pop('employer_address')) tax_address_ser = AddressUpdateSerializer( data=validated_data.pop('tax_address')) employer_address_ser.is_valid(raise_exception=True) tax_address_ser.is_valid(raise_exception=True) validated_data['employer_address'] = employer_address_ser.save() validated_data['tax_address'] = tax_address_ser.save() ib_onboard = super(IBOnboardCreateSerializer, self).create(validated_data) return ib_onboard
class AuthorisedRepresentativeCreateSerializer(serializers.ModelSerializer): """ Create Serializer, POST requests only """ residential_address = AddressUpdateSerializer(required=False) regional_data = serializers.JSONField(required=False) firm = FirmCreateSerializer() class Meta: model = AuthorisedRepresentative fields = ( 'phone_num', 'regional_data', RESIDENTIAL_ADDRESS_KEY, 'betasmartz_agreement', 'date_of_birth', 'civil_status', 'firm', ) @transaction.atomic def create(self, validated_data): if RESIDENTIAL_ADDRESS_KEY in validated_data: address_ser = AddressUpdateSerializer(data=validated_data.pop(RESIDENTIAL_ADDRESS_KEY)) else: address_ser = AddressUpdateSerializer(data=UNSET_ADDRESS) address_ser.is_valid(raise_exception=True) validated_data[RESIDENTIAL_ADDRESS_KEY] = address_ser.save() firm_ser = FirmCreateSerializer(data=validated_data.pop('firm')) firm_ser.is_valid(raise_exception=True) validated_data['firm'] = firm_ser.save() return super(AuthorisedRepresentativeCreateSerializer, self).create(validated_data)
def create(self, validated_data): # Default to Personal account type for risk profile group on a brand # new client (since they have no accounts yet, we have to assume) rpg = RiskProfileGroup.objects.get( account_types__account_type=constants.ACCOUNT_TYPE_PERSONAL) validated_data['risk_profile_group'] = rpg address_ser = AddressUpdateSerializer( data=validated_data.pop(RESIDENTIAL_ADDRESS_KEY)) address_ser.is_valid(raise_exception=True) validated_data[RESIDENTIAL_ADDRESS_KEY] = address_ser.save() # For now we auto confirm and approve the client. validated_data['is_confirmed'] = True validated_data['is_accepted'] = True return super(ClientUpdateSerializer, self).create(validated_data)
class FirmDataCreateSerializer(serializers.ModelSerializer): office_address = AddressUpdateSerializer(required=False) postal_address = AddressUpdateSerializer(required=False) class Meta: model = FirmData exclude = ('firm',) def create(self, validated_data): if 'office_address' in validated_data: office_address_ser = AddressUpdateSerializer(data=validated_data.pop('office_address')) else: office_address_ser = AddressUpdateSerializer(data=UNSET_ADDRESS) office_address_ser.is_valid(raise_exception=True) validated_data['office_address'] = office_address_ser.save() if 'postal_address' in validated_data: postal_address_ser = AddressUpdateSerializer(data=validated_data.pop('postal_address')) else: postal_address_ser = AddressUpdateSerializer(data=UNSET_ADDRESS) postal_address_ser.is_valid(raise_exception=True) validated_data['postal_address'] = postal_address_ser.save() return super(FirmDataCreateSerializer, self).create(validated_data)
def create(self, validated_data): if RESIDENTIAL_ADDRESS_KEY in validated_data: address_ser = AddressUpdateSerializer(data=validated_data.pop(RESIDENTIAL_ADDRESS_KEY)) else: address_ser = AddressUpdateSerializer(data=UNSET_ADDRESS) address_ser.is_valid(raise_exception=True) validated_data[RESIDENTIAL_ADDRESS_KEY] = address_ser.save() firm_ser = FirmCreateSerializer(data=validated_data.pop('firm')) firm_ser.is_valid(raise_exception=True) validated_data['firm'] = firm_ser.save() return super(AuthorisedRepresentativeCreateSerializer, self).create(validated_data)
class AuthorisedRepresentativeUpdateSerializer(serializers.ModelSerializer): """ Update Serializer, PUT requests only """ residential_address = AddressUpdateSerializer() regional_data = serializers.JSONField() firm = FirmUpdateSerializer() class Meta: model = AuthorisedRepresentative fields = ( 'phone_num', 'regional_data', RESIDENTIAL_ADDRESS_KEY, 'betasmartz_agreement', 'date_of_birth', 'civil_status', 'firm', )
def create(self, validated_data): # Default to Personal account type for risk profile group on a brand # new client (since they have no accounts yet, we have to assume) rpg = RiskProfileGroup.objects.get( account_types__account_type=constants.ACCOUNT_TYPE_PERSONAL) validated_data['risk_profile_group'] = rpg if RESIDENTIAL_ADDRESS_KEY in validated_data: address_ser = AddressUpdateSerializer( data=validated_data.pop(RESIDENTIAL_ADDRESS_KEY)) else: address_ser = AddressUpdateSerializer(data=UNSET_ADDRESS) address_ser.is_valid(raise_exception=True) validated_data[RESIDENTIAL_ADDRESS_KEY] = address_ser.save() if 'ib_onboard' in validated_data: ib_onboard_ser = IBOnboardCreateSerializer( data=validated_data.pop('ib_onboard')) ib_onboard_ser.is_valid(raise_exception=True) # For now we auto confirm and approve the client. validated_data['is_confirmed'] = True validated_data['is_accepted'] = True client = super(ClientCreateSerializer, self).create(validated_data) client_account = client.primary_accounts.create( account_type=constants.ACCOUNT_TYPE_PERSONAL, default_portfolio_set=validated_data['advisor']. default_portfolio_set, confirmed=True, ) if 'ib_onboard_ser' in locals(): ib_onboard = ib_onboard_ser.save(client=client) if ib_onboard.account_number is not None: # If the client already has IB account create and link IBAccount object ib_account = IBAccount(bs_account=client_account, ib_account=ib_onboard.account_number) ib_account.save() else: # connect IB broker onboarding ftp status = onboarding.onboard('INDIVIDUAL', ib_onboard) return client
class ClientUpdateSerializer(serializers.ModelSerializer): """ Write PUT update requests only """ qs = RiskProfileAnswer.objects.all() risk_profile_responses = serializers.PrimaryKeyRelatedField(many=True, queryset=qs, required=False) residential_address = AddressUpdateSerializer() regional_data = serializers.JSONField() question_one = serializers.IntegerField(required=True) answer_one = serializers.CharField(required=True) question_two = serializers.IntegerField(required=True) answer_two = serializers.CharField(required=True) class Meta: model = Client fields = ( 'employment_status', RESIDENTIAL_ADDRESS_KEY, 'income', 'other_income', 'occupation', 'industry_sector', 'employer_type', 'student_loan', 'employer', 'civil_status', 'risk_profile_responses', 'betasmartz_agreement', 'advisor_agreement', 'phone_num', 'regional_data', 'smoker', 'daily_exercise', 'weight', 'height', 'drinks', 'date_of_birth', 'question_one', 'answer_one', 'question_two', 'answer_two', ) def validate(self, data): request = self.context.get('request') user = request.user # no user is create request for initial registration # SecurityAnswer checks if data.get('question_one') == data.get('question_two'): logger.error('ClientUpdateSerializer given matching questions %s' % data.get('question_one')) raise serializers.ValidationError( {'question_two': 'Questions must be unique'}) try: sa1 = SecurityAnswer.objects.get(pk=data.get('question_one')) if sa1.user != user: logger.error( 'SecurityAnswer not found for user %s and question %s with ClientUpdateSerializer' % (user.email, data.get('question_one'))) raise serializers.ValidationError( {'question_one': 'User does not own given question'}) except: logger.error('ClientUpdateSerializer question %s not found' % data.get('question_one')) raise serializers.ValidationError( {'question_one': 'Question not found'}) if not sa1.check_answer(data.get('answer_one')): logger.error('ClientUpdateSerializer answer two was wrong') raise serializers.ValidationError({'answer_one': 'Wrong answer'}) try: sa2 = SecurityAnswer.objects.get(pk=data.get('question_two')) if sa2.user != user: logger.error( 'SecurityAnswer not found for user %s and question %s with ClientUpdateSerializer' % (user.email, data.get('question_two'))) raise serializers.ValidationError( {'question_two': 'User does not own given question'}) except: logger.error('ClientUpdateSerializer question %s not found' % data.get('question_two')) raise serializers.ValidationError( {'question_two': 'Question not found'}) if not sa2.check_answer(data.get('answer_two')): logger.error('ClientUpdateSerializer answer two was wrong') raise serializers.ValidationError({'answer_two': 'Wrong answer'}) return data def validate_phone_num(self, phone_num): serializer = PhoneNumberValidationSerializer( data={'number': phone_num}) if not serializer.is_valid(): raise serializers.ValidationError('Invalid phone number') return serializer.validated_data def update(self, instance, validated_data): add_data = validated_data.pop(RESIDENTIAL_ADDRESS_KEY, None) if add_data is not None: address_ser = AddressUpdateSerializer(data=add_data) address_ser.is_valid(raise_exception=True) validated_data[RESIDENTIAL_ADDRESS_KEY] = address_ser.save() return super(ClientUpdateSerializer, self).update(instance, validated_data)
class ClientCreateSerializer(serializers.ModelSerializer): """ For Create POST requests only """ qs = RiskProfileAnswer.objects.all() risk_profile_responses = serializers.PrimaryKeyRelatedField(many=True, queryset=qs, required=False) residential_address = AddressUpdateSerializer() regional_data = serializers.JSONField() ib_onboard = IBOnboardCreateSerializer(required=False) class Meta: model = Client fields = ( 'employment_status', RESIDENTIAL_ADDRESS_KEY, 'income', 'other_income', 'occupation', 'industry_sector', 'employer_type', 'student_loan', 'student_loan_assistance_program', 'student_loan_graduate_looking', 'student_loan_parent_looking', 'hsa_eligible', 'hsa_provider_name', 'hsa_state', 'hsa_coverage_type', 'employer', 'civil_status', 'risk_profile_responses', 'betasmartz_agreement', 'advisor_agreement', 'phone_num', 'regional_data', 'smoker', 'daily_exercise', 'weight', 'height', 'drinks', 'date_of_birth', 'ib_onboard', ) def validate_phone_num(self, phone_num): serializer = PhoneNumberValidationSerializer( data={'number': phone_num}) if not serializer.is_valid(): raise serializers.ValidationError('Invalid phone number') return serializer.validated_data def create(self, validated_data): # Default to Personal account type for risk profile group on a brand # new client (since they have no accounts yet, we have to assume) rpg = RiskProfileGroup.objects.get( account_types__account_type=constants.ACCOUNT_TYPE_PERSONAL) validated_data['risk_profile_group'] = rpg address_ser = AddressUpdateSerializer( data=validated_data.pop(RESIDENTIAL_ADDRESS_KEY)) address_ser.is_valid(raise_exception=True) validated_data[RESIDENTIAL_ADDRESS_KEY] = address_ser.save() if 'ib_onboard' in validated_data: ib_onboard_ser = IBOnboardCreateSerializer( data=validated_data.pop('ib_onboard')) ib_onboard_ser.is_valid(raise_exception=True) # For now we auto confirm and approve the client. validated_data['is_confirmed'] = True validated_data['is_accepted'] = True client = super(ClientCreateSerializer, self).create(validated_data) client.primary_accounts.create( account_type=constants.ACCOUNT_TYPE_PERSONAL, default_portfolio_set=validated_data['advisor']. default_portfolio_set, confirmed=True, ) if 'ib_onboard_ser' in locals(): ib_onboard = ib_onboard_ser.save(client=client) # connect IB broker status = onboarding_helpers.onboarding_from_invitation(ib_onboard) return client
def create(self, validated_data): if 'office_address' in validated_data: office_address_ser = AddressUpdateSerializer(data=validated_data.pop('office_address')) else: office_address_ser = AddressUpdateSerializer(data=UNSET_ADDRESS) office_address_ser.is_valid(raise_exception=True) validated_data['office_address'] = office_address_ser.save() if 'postal_address' in validated_data: postal_address_ser = AddressUpdateSerializer(data=validated_data.pop('postal_address')) else: postal_address_ser = AddressUpdateSerializer(data=UNSET_ADDRESS) postal_address_ser.is_valid(raise_exception=True) validated_data['postal_address'] = postal_address_ser.save() return super(FirmDataCreateSerializer, self).create(validated_data)
class ClientUpdateSerializer(serializers.ModelSerializer): """ Write (POST/PUT) update requests only """ qs = RiskProfileAnswer.objects.all() risk_profile_responses = serializers.PrimaryKeyRelatedField(many=True, queryset=qs, required=False) residential_address = AddressUpdateSerializer() regional_data = serializers.JSONField() class Meta: model = Client fields = ( 'employment_status', RESIDENTIAL_ADDRESS_KEY, 'income', 'occupation', 'employer', 'civil_status', 'risk_profile_responses', 'betasmartz_agreement', 'advisor_agreement', 'phone_num', 'regional_data', 'smoker', 'daily_exercise', 'weight', 'height', 'drinks', 'date_of_birth', ) def validate_phone_num(self, phone_num): serializer = PhoneNumberValidationSerializer( data={'number': phone_num}) if not serializer.is_valid(): raise serializers.ValidationError('Invalid phone number') return serializer.validated_data def create(self, validated_data): # Default to Personal account type for risk profile group on a brand # new client (since they have no accounts yet, we have to assume) rpg = RiskProfileGroup.objects.get( account_types__account_type=constants.ACCOUNT_TYPE_PERSONAL) validated_data['risk_profile_group'] = rpg address_ser = AddressUpdateSerializer( data=validated_data.pop(RESIDENTIAL_ADDRESS_KEY)) address_ser.is_valid(raise_exception=True) validated_data[RESIDENTIAL_ADDRESS_KEY] = address_ser.save() # For now we auto confirm and approve the client. validated_data['is_confirmed'] = True validated_data['is_accepted'] = True return super(ClientUpdateSerializer, self).create(validated_data) def update(self, instance, validated_data): add_data = validated_data.pop(RESIDENTIAL_ADDRESS_KEY, None) if add_data is not None: address_ser = AddressUpdateSerializer(data=add_data) address_ser.is_valid(raise_exception=True) validated_data[RESIDENTIAL_ADDRESS_KEY] = address_ser.save() return super(ClientUpdateSerializer, self).update(instance, validated_data)