<commit_before>from rest_framework import serializers
from rest_framework.exceptions import ParseError

from ..models import (NewsCategory, RadioCategory, ProjectCategory,
                      NewsShow, RadioShow, ProjectShow,
                      NewsPodcast, RadioPodcast, ProjectPodcast)


class TagListSerializer(serializers.WritableField):
    def from_native(self, data):
        if type(data) is not list:
            raise ParseError("expected a list of data")
        return data

    def to_native(self, obj):
        if type(obj) is not list:
            return [tag.name for tag in obj.all()]
        return obj


class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'name',)


class NewsCategorySerializer(CategorySerializer):
    class Meta(CategorySerializer.Meta):
        model = NewsCategory


class RadioCategorySerializer(CategorySerializer):
    class Meta(CategorySerializer.Meta):
        model = RadioCategory


class ProjectCategorySerializer(CategorySerializer):
    class Meta(CategorySerializer.Meta):
        model = ProjectCategory


class ShowSerializer(serializers.ModelSerializer):
    tags = TagListSerializer(blank=True)

    class Meta:
        fields = ('id', 'name', 'description', 'image', 'categories', 'tags')


class NewsShowSerializer(ShowSerializer):
    class Meta(ShowSerializer.Meta):
        model = NewsShow


class RadioShowSerializer(ShowSerializer):
    class Meta(ShowSerializer.Meta):
        model = RadioShow


class ProjectShowSerializer(ShowSerializer):
    class Meta(ShowSerializer.Meta):<commit_msg>Serialize nested categories for every show<commit_after>from rest_framework import serializers
from rest_framework.exceptions import ParseError

from ..models import (NewsCategory, RadioCategory, ProjectCategory,
                      NewsShow, RadioShow, ProjectShow,
                      NewsPodcast, RadioPodcast, ProjectPodcast)


class TagListSerializer(serializers.WritableField):
    def from_native(self, data):
        if type(data) is not list:
            raise ParseError("expected a list of data")
        return data

    def to_native(self, obj):
        if type(obj) is not list:
            return [tag.name for tag in obj.all()]
        return obj


class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'name',)


class NewsCategorySerializer(CategorySerializer):
    class Meta(CategorySerializer.Meta):
        model = NewsCategory


class RadioCategorySerializer(CategorySerializer):
    class Meta(CategorySerializer.Meta):
        model = RadioCategory


class ProjectCategorySerializer(CategorySerializer):
    class Meta(CategorySerializer.Meta):
        model = ProjectCategory


class ShowSerializer(serializers.ModelSerializer):
    tags = TagListSerializer(blank=True)

    class Meta:
        fields = ('id', 'name', 'description', 'image', 'categories', 'tags')


class NewsShowSerializer(ShowSerializer):
    categories = NewsCategorySerializer(many=True)

    class Meta(ShowSerializer.Meta):
        model = NewsShow


class RadioShowSerializer(ShowSerializer):
    categories = RadioCategorySerializer(many=True)

    class Meta(ShowSerializer.Meta):
        model = RadioShow


class ProjectShowSerializer(ShowSerializer):
    categories = ProjectCategorySerializer(many=True)

    class Meta(ShowSerializer.Meta):
{'fraction_non_alphanumeric': 0.08242566970856638, 'fraction_numerical': 0.0, 'mean_word_length': 3.654794520547945, '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': '1330543', 'n_tokens_mistral': 847, 'n_tokens_neox': 820, 'n_words': 226}