<commit_before>from rest_framework import serializers
from sorl.thumbnail import get_thumbnail

from leaflets.models import Leaflet, LeafletImage
from constituencies.models import Constituency
from uk_political_parties.models import Party
from people.models import Person


class ConstituencySerializer(serializers.ModelSerializer):
    class Meta:
        model = Constituency
        fields = (
            'pk',
            'name',
            'country_name',
            'slug',
        )


class PartySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Party
        fields = (
            'pk',
            'party_name',
            'party_type',
            'status',
        )

class PersonSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Person
        fields = (
            'pk',
            'name',
            'remote_id',
            'source_name',
            'source_url',
        )


class LeafletImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = LeafletImage
        fields = (
            'image',
            'image_text',
            'image_type',
        )
    image = serializers.ImageField()


class LeafletSerializer(serializers.HyperlinkedModelSerializer):
    images = LeafletImageSerializer(many=True, required=False)
    constituency = ConstituencySerializer(required=False)
    publisher_party = PartySerializer(required=False)
    publisher_person = PersonSerializer(required=False)
    first_page_thumb = serializers.SerializerMethodField()

    def get_first_page_thumb(self, obj):
        image = obj.get_first_image()
        if image:
            return get_thumbnail(obj.get_first_image().image, '350').url

    def validate(self, data):
        if not data.get('status') or not data.get('images'):
            data['status'] = 'draft'
        return data

    class Meta:
        model = Leaflet
        depth = 1
        fields = (
            'pk',
            'title',
            'description',
            'publisher_party',
            'publisher_person',
            'constituency',
            'images',
            'first_page_thumb',
            'date_uploaded',
            'date_delivered',
            'status',
        )


class LeafletMinSerializer(serializers.ModelSerializer):
    images = LeafletImageSerializer(many=True, required=False)

    class Meta:
        model = Leaflet
        depth = 0
        fields = (
            'pk',
            'title',
            'description',
            'publisher_party',
            'constituency',
            'images',
            'date_uploaded',
            'date_delivered',
            'status',
        )<commit_msg>Add first page thumb to API endpoint<commit_after>from rest_framework import serializers
from sorl.thumbnail import get_thumbnail

from leaflets.models import Leaflet, LeafletImage
from constituencies.models import Constituency
from uk_political_parties.models import Party
from people.models import Person


class ConstituencySerializer(serializers.ModelSerializer):
    class Meta:
        model = Constituency
        fields = (
            'pk',
            'name',
            'country_name',
            'slug',
        )


class PartySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Party
        fields = (
            'pk',
            'party_name',
            'party_type',
            'status',
        )

class PersonSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Person
        fields = (
            'pk',
            'name',
            'remote_id',
            'source_name',
            'source_url',
        )


class LeafletImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = LeafletImage
        fields = (
            'image',
            'image_text',
            'image_type',
        )
    image = serializers.ImageField()


class LeafletSerializer(serializers.HyperlinkedModelSerializer):
    images = LeafletImageSerializer(many=True, required=False)
    constituency = ConstituencySerializer(required=False)
    publisher_party = PartySerializer(required=False)
    publisher_person = PersonSerializer(required=False)
    first_page_thumb = serializers.SerializerMethodField()

    def get_first_page_thumb(self, obj):
        image = obj.get_first_image()
        if image:
            return get_thumbnail(obj.get_first_image().image, '350').url

    def validate(self, data):
        if not data.get('status') or not data.get('images'):
            data['status'] = 'draft'
        return data

    class Meta:
        model = Leaflet
        depth = 1
        fields = (
            'pk',
            'title',
            'description',
            'publisher_party',
            'publisher_person',
            'constituency',
            'images',
            'first_page_thumb',
            'date_uploaded',
            'date_delivered',
            'status',
        )


class LeafletMinSerializer(serializers.ModelSerializer):
    images = LeafletImageSerializer(many=True, required=False)
    first_page_thumb = serializers.SerializerMethodField()

    def get_first_page_thumb(self, obj):
        image = obj.get_first_image()
        if image:
            return get_thumbnail(obj.get_first_image().image, '350').url

    class Meta:
        model = Leaflet
        depth = 0
        fields = (
            'pk',
            'title',
            'description',
            'publisher_party',
            'constituency',
            'images',
            'first_page_thumb',
            'date_uploaded',
            'date_delivered',
            'status',
        )
{'fraction_non_alphanumeric': 0.09057527539779682, 'fraction_numerical': 0.00227312467214548, 'mean_word_length': 1.9760665972944849, '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': '3003396', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1505, 'n_words': 311}