def create()

in django_airavata/apps/auth/serializers.py [0:0]


    def create(self, validated_data):
        request = self.context['request']
        user = request.user
        user_profile = user.user_profile

        # Support create/update in the many=True situation. When many=True and
        # .save() is called, .create() will be called on each value. Here we
        # need to see if there is an id and if so call .update() instead.
        if "id" in validated_data:
            instance = models.ExtendedUserProfileValue.objects.get(id=validated_data["id"])
            return self.update(instance, validated_data)

        ext_user_profile_field = validated_data.pop('ext_user_profile_field')
        if ext_user_profile_field.field_type == 'text':
            text_value = validated_data.pop('text_value')
            return models.ExtendedUserProfileTextValue.objects.create(
                ext_user_profile_field=ext_user_profile_field,
                user_profile=user_profile,
                text_value=text_value)
        elif ext_user_profile_field.field_type == 'single_choice':
            choices = validated_data.pop('choices', [])
            choice = choices[0] if len(choices) > 0 else None
            other_value = validated_data.pop('other_value', '')
            return models.ExtendedUserProfileSingleChoiceValue.objects.create(
                ext_user_profile_field=ext_user_profile_field,
                user_profile=user_profile,
                choice=choice,
                other_value=other_value,
            )
        elif ext_user_profile_field.field_type == 'multi_choice':
            choices = validated_data.pop('choices', [])
            other_value = validated_data.pop('other_value', '')
            value = models.ExtendedUserProfileMultiChoiceValue.objects.create(
                ext_user_profile_field=ext_user_profile_field,
                user_profile=user_profile,
                other_value=other_value,
            )
            for choice in choices:
                models.ExtendedUserProfileMultiChoiceValueChoice.objects.create(
                    value=choice,
                    multi_choice_value=value
                )
            return value
        elif ext_user_profile_field.field_type == 'user_agreement':
            agreement_value = validated_data.get('agreement_value')
            return models.ExtendedUserProfileAgreementValue.objects.create(
                ext_user_profile_field=ext_user_profile_field,
                user_profile=user_profile,
                agreement_value=agreement_value
            )