def update()

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


    def update(self, instance, validated_data):
        instance.name = validated_data['name']
        instance.help_text = validated_data['help_text']
        instance.order = validated_data['order']
        instance.required = validated_data.get('required', instance.required)
        # logger.debug(f"instance.field_type={instance.field_type}, validated_data={validated_data}")
        if instance.field_type == 'single_choice':
            instance.single_choice.other = validated_data.get('other', instance.single_choice.other)
            choices = validated_data.pop('choices', None)
            if choices:
                choice_ids = [choice['id'] for choice in choices if 'id' in choice]
                # Soft delete any choices that are not in the list
                instance.single_choice.choices.exclude(id__in=choice_ids).update(deleted=True)
                for choice in choices:
                    choice_id = choice.pop('id', None)
                    models.ExtendedUserProfileSingleChoiceFieldChoice.objects.update_or_create(
                        id=choice_id,
                        defaults={**choice, "single_choice_field": instance.single_choice},
                    )
            instance.single_choice.save()
        elif instance.field_type == 'multi_choice':
            instance.multi_choice.other = validated_data.get('other', instance.multi_choice.other)
            choices = validated_data.pop('choices', None)
            if choices:
                choice_ids = [choice['id'] for choice in choices if 'id' in choice]
                # Soft delete any choices that are not in the list
                instance.multi_choice.choices.exclude(id__in=choice_ids).update(deleted=True)
                for choice in choices:
                    choice_id = choice.pop('id', None)
                    models.ExtendedUserProfileMultiChoiceFieldChoice.objects.update_or_create(
                        id=choice_id,
                        defaults={**choice, "multi_choice_field": instance.multi_choice},
                    )
            instance.multi_choice.save()
        elif instance.field_type == 'user_agreement':
            instance.user_agreement.checkbox_label = validated_data.pop('checkbox_label', instance.user_agreement.checkbox_label)
            instance.user_agreement.save()

        # update links
        links = validated_data.pop('links', [])
        link_ids = [link['id'] for link in links if 'id' in link]
        instance.links.exclude(id__in=link_ids).delete()
        for link in links:
            link_id = link.pop('id', None)
            link['field'] = instance
            models.ExtendedUserProfileFieldLink.objects.update_or_create(
                id=link_id,
                defaults=link,
            )

        instance.save()
        return instance