from rest_framework import serializers

from newsreader.news.collection.models import CollectionRule


class RuleSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())
    unread = serializers.SerializerMethodField()

    def get_unread(self, rule):
        return rule.posts.filter(read=False).count()

    class Meta:
        model = CollectionRule
        fields = ("id", "type", "name", "url", "favicon", "category", "user", "unread")
