<filename>app/report/serializers.py
from core.models import Boec, Brigade, Season, SeasonReport
from core.serializers import DynamicFieldsModelSerializer
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from so.serializers import BoecInfoSerializer, BrigadeShortSerializer


class SeasonReportListSerializer(serializers.ModelSerializer):
    """serializer for the SeasonReport object"""

    class Meta:
        model = SeasonReport
        fields = ("id", "year", "boec_count", "state", "employer")
        read_only_fields = ("id", "boec_count")


class SeasonReportSerializer(DynamicFieldsModelSerializer):
    """serializer for the SeasonReport object"""

    can_edit = serializers.BooleanField(read_only=True)
    brigade = BrigadeShortSerializer(fields=("id", "full_title"), read_only=True)
    brigade_id = serializers.PrimaryKeyRelatedField(
        queryset=Brigade.objects.all(), source="brigade", required=False
    )

    class Meta:
        model = SeasonReport
        fields = (
            "id",
            "year",
            "boec_count",
            "state",
            "employer",
            "can_edit",
            "brigade",
            "brigade_id",
        )
        read_only_fields = ("id", "boec_count", "can_edit", "brigade")


class SeasonSerializer(DynamicFieldsModelSerializer):
    """serializer for season objects"""

    boec = BoecInfoSerializer(read_only=True)
    boec_id = serializers.PrimaryKeyRelatedField(
        queryset=Boec.objects.all(), source="boec"
    )
    reports = SeasonReportSerializer(
        fields=("brigade", "id", "employer", "year"),
        source="season_reports",
        many=True,
        read_only=True,
    )

    class Meta:
        model = Season
        fields = ("id", "boec", "boec_id", "state", "reports")
        read_only_fields = ("id", "boec", "reports")


class BoecSeasonsSerializer(DynamicFieldsModelSerializer):
    """serializer for season objects"""

    reports = SeasonReportSerializer(
        fields=("brigade", "id", "employer", "year"),
        source="season_reports",
        many=True,
        read_only=True,
    )
    boec_id = serializers.PrimaryKeyRelatedField(
        queryset=Boec.objects.all(), source="boec", required=False
    )

    class Meta:
        model = Season
        fields = ("id", "state", "reports", "boec_id")
        read_only_fields = ("id", "reports")


class SeasonRequestSerializer(serializers.ModelSerializer):
    """serializer for the SeasonReport object"""

    seasons = SeasonSerializer(many=True)

    class Meta:
        model = SeasonReport
        fields = ("id", "year", "seasons", "employer")
        read_only_fields = ("id",)
