<commit_before>from crowdsourcing import models
from rest_framework import serializers
from crowdsourcing.serializers.dynamic import DynamicFieldsModelSerializer
from rest_framework.exceptions import ValidationError


class TemplateItemSerializer(DynamicFieldsModelSerializer):
    class Meta:
        model = models.TemplateItem
        fields = ('id', 'name', 'type', 'sub_type', 'position', 'template', 'role', 'required', 'aux_attributes')

    def create(self, *args, **kwargs):
        item = models.TemplateItem.objects.create(**self.validated_data)
        return item


class TemplateSerializer(DynamicFieldsModelSerializer):
    template_items = TemplateItemSerializer(many=True, required=False)

    class Meta:
        model = models.Template
        fields = ('id', 'name', 'template_items')
        read_only_fields = ('template_items',)

    def create(self, with_default, *args, **kwargs):
        template = models.Template.objects.create(owner=kwargs['owner'], **self.validated_data)
        if with_default:
            item = {
                "type": "radio",
                "values": "Option 1,Option 2,Option 3",
                "label": "Add question here",
                "data_source": None,
                "role": "input",
                "layout": "row",
                "id_string": "radio_0",
                "name": "radio_0",
                "icon": "radio_button_checked",
                "position": 1,
                "template": template.id
            }
            template_item_serializer = TemplateItemSerializer(data=item)
            if template_item_serializer.is_valid():
                template_item_serializer.create()
            else:
                raise ValidationError(template_item_serializer.errors)
        return template<commit_msg>fix create new project default template item radio<commit_after>from crowdsourcing import models
from rest_framework import serializers
from crowdsourcing.serializers.dynamic import DynamicFieldsModelSerializer
from rest_framework.exceptions import ValidationError


class TemplateItemSerializer(DynamicFieldsModelSerializer):
    class Meta:
        model = models.TemplateItem
        fields = ('id', 'name', 'type', 'sub_type', 'position', 'template', 'role', 'required', 'aux_attributes')

    def create(self, *args, **kwargs):
        item = models.TemplateItem.objects.create(**self.validated_data)
        return item


class TemplateSerializer(DynamicFieldsModelSerializer):
    template_items = TemplateItemSerializer(many=True, required=False)

    class Meta:
        model = models.Template
        fields = ('id', 'name', 'template_items')
        read_only_fields = ('template_items',)

    def create(self, with_default, *args, **kwargs):
        template = models.Template.objects.create(owner=kwargs['owner'], **self.validated_data)
        if with_default:
            item = {
                "type": "radio",
                "role": "input",
                "name": "radio_0",
                "icon": "radio_button_checked",
                "position": 1,
                "template": template.id,
                "aux_attributes": {
                        "question": {
                            "source": "static",
                            "value": "Untitled Question",
                            "description": None
                        },
                        "layout": 'column',
                        "options": [
                            {
                                "source": 'static',
                                "value": 'Option 1',
                                "position": 1
                            },
                            {
                                "source": 'static',
                                "value": 'Option 2',
                                "position": 2
                            }
                        ],
                        "shuffle_options": "false"
                    },
            }
            template_item_serializer = TemplateItemSerializer(data=item)
            if template_item_serializer.is_valid():
                template_item_serializer.create()
            else:
                raise ValidationError(template_item_serializer.errors)
        return template
{'fraction_non_alphanumeric': 0.10159176029962547, 'fraction_numerical': 0.0028089887640449437, 'mean_word_length': 1.7909862834748531, 'pattern_counts': {'":': 31, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3207808', 'n_tokens_mistral': 1051, 'n_tokens_neox': 984, 'n_words': 225}