<commit_before>from rest_framework import serializers

from surveys.models.surveys import Survey, SurveyHour
from surveys.models.observers import Observer

# Helpers
class SurveyHourSerializer(serializers.ModelSerializer):
    class Meta:
        model = SurveyHour
        exclude = ('survey',)

class ObserverSerializer(serializers.ModelSerializer):
    class Meta:
        model = Observer
        fields = '__all__'

# Report serializers
class ReportSurveySerializer(serializers.ModelSerializer):
    hours = SurveyHourSerializer(many=True)
    observer = ObserverSerializer(many=False)
    challenge = serializers.CharField(allow_blank=True, required=False)

    class Meta:
        model = Survey
        exclude = ('status',)

    def create(self, validated_data):
        observer_data = validated_data.pop('observer')
        hours_data = validated_data.pop('hours')

        observer = Observer.objects.create(**observer_data)
        survey = Survey.objects.create(observer=observer, **validated_data)

        for hour_data in hours_data:
            SurveyHour.objects.create(survey=survey, **hour_data)

        return survey

    def validate(self, data):
        """ Basic check to deter spam submissions """
        if data.pop('challenge', None) != 'kea':
            raise serializers.ValidationError('Invalid submission')
        return data<commit_msg>Added null value to max_flock_size serialiser instead of 0<commit_after>from rest_framework import serializers

from surveys.models.surveys import Survey, SurveyHour
from surveys.models.observers import Observer

# Helpers
class SurveyHourSerializer(serializers.ModelSerializer):
    class Meta:
        model = SurveyHour
        exclude = ('survey',)

class ObserverSerializer(serializers.ModelSerializer):
    class Meta:
        model = Observer
        fields = '__all__'

# Report serializers
class ReportSurveySerializer(serializers.ModelSerializer):
    hours = SurveyHourSerializer(many=True)
    observer = ObserverSerializer(many=False)
    challenge = serializers.CharField(allow_blank=True, required=False)

    class Meta:
        model = Survey
        exclude = ('status',)

    def create(self, validated_data):
        observer_data = validated_data.pop('observer')
        hours_data = validated_data.pop('hours')

        # Set a max_flock_size of 0 to 'null'
        try:
            if (validated_data['max_flock_size'] == 0): validated_data.pop('max_flock_size')
        except:
            pass

        observer = Observer.objects.create(**observer_data)
        survey = Survey.objects.create(observer=observer, **validated_data)

        for hour_data in hours_data:
            SurveyHour.objects.create(survey=survey, **hour_data)

        return survey

    def validate(self, data):
        """ Basic check to deter spam submissions """
        if data.pop('challenge', None) != 'kea':
            raise serializers.ValidationError('Invalid submission')
        return data
{'fraction_non_alphanumeric': 0.0903594222371515, 'fraction_numerical': 0.001007725898555593, 'mean_word_length': 3.3347889374090247, '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': '4803307', 'n_tokens_mistral': 832, 'n_tokens_neox': 784, 'n_words': 191}