<commit_before>from core.models.boot_script import BootScript, ScriptType
from core.models.user import AtmosphereUser
from rest_framework import serializers


class BootScriptSerializer(serializers.HyperlinkedModelSerializer):
    created_by = serializers.SlugRelatedField(
        slug_field='username', queryset=AtmosphereUser.objects.all())
    text = serializers.CharField(source='script_text')
    type = serializers.SlugRelatedField(
        source='script_type',
        slug_field='name',
        queryset=ScriptType.objects.all())

    def create(self, validated_data):

        if 'created_by' not in validated_data:
            request = self.context.get('request')
            if request and request.user:
                validated_data['created_by'] = request.user
        return super(BootScriptSerializer, self).create(validated_data)

    class Meta:
        model = BootScript
        # view_name = 'api:v2:boot_script-detail' -- not needed?
        fields = ('id', 'created_by', 'title', 'text', 'type')<commit_msg>Fix 'boot script' serializer to allow 'created_by' to be inferred as the request.user<commit_after>from core.models.boot_script import BootScript, ScriptType
from core.models.user import AtmosphereUser
from rest_framework import serializers


class BootScriptSerializer(serializers.HyperlinkedModelSerializer):
    created_by = serializers.SlugRelatedField(
        slug_field='username', queryset=AtmosphereUser.objects.all(),
        required=False)
    text = serializers.CharField(source='script_text')
    type = serializers.SlugRelatedField(
        source='script_type',
        slug_field='name',
        queryset=ScriptType.objects.all())

    def create(self, validated_data):

        if 'created_by' not in validated_data:
            request = self.context.get('request')
            if request and request.user:
                validated_data['created_by'] = request.user
        return super(BootScriptSerializer, self).create(validated_data)

    class Meta:
        model = BootScript
        # view_name = 'api:v2:boot_script-detail' -- not needed?
        fields = ('id', 'created_by', 'title', 'text', 'type')
{'fraction_non_alphanumeric': 0.10129509713228492, 'fraction_numerical': 0.0009250693802035153, 'mean_word_length': 3.572938689217759, 'pattern_counts': {'":': 0, '<': 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': '7379017', 'n_tokens_mistral': 615, 'n_tokens_neox': 593, 'n_words': 130}