"""DRF-serializers for application report."""
from rest_framework import serializers

from report.models import Report


class ReportBaseSerializer(serializers.ModelSerializer):
    """Serializer for model Report."""

    category_display = serializers.CharField(source='get_category_display', read_only=True)

    class Meta:
        """Meta class."""
        model = Report
        fields = [
            'id',
            'category',
            'category_display',
            'url',
            'description',
        ]
        extra_kwargs = {
            'source': {'required': False},
            'category': {'write_only': True}
        }

    def validate(self, attrs):
        """An overridden validate method."""
        attrs['source'] = self.context.get('view').get_source()
        return attrs

    def create(self, validated_data):
        """An overridden create method."""
        return self.Meta.model.objects.make(**validated_data)
