from rest_framework import serializers
from core.apps.eggs.models import History


class HistorySerializer(serializers.ModelSerializer):
    avatar = serializers.SerializerMethodField()

    class Meta:
        model = History
        fields = [
            "id",
            "content_type",
            "object_id",
            "action",
            "avatar",
            "timestamp",
            "created_by",
            "created_who",
            "comment",
            "reason",
        ]

    def get_avatar(self, obj):
        if obj.avatar and hasattr(obj.avatar, "url"):
            return obj.avatar.url
        return None
