<commit_before>__author__ = 'jpi'

from rest_framework import serializers
from stadtgedaechtnis_backend.models import *


class LocationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Location
        fields = ('id', 'label', 'description', 'latitude', 'longitude', 'altitude',
                  'created', 'modified', 'dbpedia_link')


class LocationSerializerWithStoryIDs(serializers.ModelSerializer):
    class Meta:
        model = Location
        fields = ('id', 'label', 'description', 'latitude', 'longitude', 'altitude',
                  'created', 'modified', 'dbpedia_link', 'stories')


class StoryTitleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Story
        fields = ('id', 'title', 'location')


class LocationSerializerWithStoryTitle(serializers.ModelSerializer):
    class Meta:
        model = Location
        fields = ('id', 'label', 'description', 'latitude', 'longitude', 'altitude',
                  'created', 'modified', 'dbpedia_link', 'stories')

    stories = StoryTitleSerializer(many=True)


class AssetURLSerializer(serializers.ModelSerializer):
    class AssetSourceSerializer(serializers.URLField):
        def to_native(self, value):
            if value.instance.sources.first() is not None:
                return value.instance.sources.first().file.url
            else:
                return None

    class Meta:
        model = Asset
        fields = ('alt', 'sources')

    sources = AssetSourceSerializer()


class StoryImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Story
        fields = ('id', 'title', 'abstract', 'assets')

    assets = AssetURLSerializer(many=False)


class LocationSerializerWithStoryImages(LocationSerializerWithStoryTitle):
    stories = StoryImageSerializer(many=True)


class StoryWithAssetSerializer(serializers.ModelSerializer):
    class Meta:
        model = Story
        fields = ('id', 'title', 'abstract', 'text', 'author',
                  'time_start', 'time_end', 'created', 'modified', 'location', 'categories', 'assets')


class StoryWithAssetImageSerializer(StoryWithAssetSerializer):
    assets = AssetURLSerializer(many=False)


class LocationSerializerWithStories(serializers.ModelSerializer):
    class Meta:
        model = Location
        fields = ('id', 'label', 'description', 'latitude', 'longitude', 'altitude',
                  'created', 'modified', 'dbpedia_link', 'stories')

    stories = StoryWithAssetSerializer(many=True)<commit_msg>Add comments to serializers<commit_after>__author__ = 'jpi'

from rest_framework import serializers
from stadtgedaechtnis_backend.models import *


class LocationSerializer(serializers.ModelSerializer):
    """
    Serializes a location
    """
    class Meta:
        model = Location
        fields = ('id', 'label', 'description', 'latitude', 'longitude', 'altitude',
                  'created', 'modified', 'dbpedia_link')


class LocationSerializerWithStoryIDs(serializers.ModelSerializer):
    """
    Serializes a location and the IDs of attached stories
    """
    class Meta:
        model = Location
        fields = ('id', 'label', 'description', 'latitude', 'longitude', 'altitude',
                  'created', 'modified', 'dbpedia_link', 'stories')


class StoryTitleSerializer(serializers.ModelSerializer):
    """
    Serializes a story with title and location
    """
    class Meta:
        model = Story
        fields = ('id', 'title', 'location')


class LocationSerializerWithStoryTitle(serializers.ModelSerializer):
    """
    Serializes a location with stories using the StoryTitleSerializer
    """
    class Meta:
        model = Location
        fields = ('id', 'label', 'description', 'latitude', 'longitude', 'altitude',
                  'created', 'modified', 'dbpedia_link', 'stories')

    stories = StoryTitleSerializer(many=True)


class AssetURLSerializer(serializers.ModelSerializer):
    """
    Serializes an asset, providing the first source URL
    """
    class AssetSourceSerializer(serializers.URLField):
        """
        Serializes the first asset source to a source URL
        """
        def to_native(self, value):
            if value.instance.sources.first() is not None:
                return value.instance.sources.first().file.url
            else:
                return None

    class Meta:
        model = Asset
        fields = ('alt', 'sources')

    sources = AssetSourceSerializer()


class StoryImageSerializer(serializers.ModelSerializer):
    """
    Serializes a story with only title and abstract and all the assets belonging to this story using the AssetURLSerializer
    """
    class Meta:
        model = Story
        fields = ('id', 'title', 'abstract', 'assets')

    assets = AssetURLSerializer(many=False)


class LocationSerializerWithStoryImages(LocationSerializerWithStoryTitle):
    """
    Serializes a Location and the attached stories using the StoryImageSerializer
    """
    stories = StoryImageSerializer(many=True)


class StoryWithAssetSerializer(serializers.ModelSerializer):
    """
    Serializes a story and all the IDs of attached stories
    """
    class Meta:
        model = Story
        fields = ('id', 'title', 'abstract', 'text', 'author',
                  'time_start', 'time_end', 'created', 'modified', 'location', 'categories', 'assets')


class StoryWithAssetImageSerializer(StoryWithAssetSerializer):
    """
    Serializes a story completely using AssetURLSerializer for its assets
    """
    assets = AssetURLSerializer(many=False)


class LocationSerializerWithStories(serializers.ModelSerializer):
    """
    Serializes a location with their attached stories using a StoryWithAssetSerializer
    """
    class Meta:
        model = Location
        fields = ('id', 'label', 'description', 'latitude', 'longitude', 'altitude',
                  'created', 'modified', 'dbpedia_link', 'stories')

    stories = StoryWithAssetSerializer(many=True)


class UserSerizalier(serializers.HyperlinkedModelSerializer):
    """
    Serializes a user with an absolute URL.
    """
    class Meta:
        model = User
        fields = ('url', 'id', 'username', 'first_name', 'last_name')
{'fraction_non_alphanumeric': 0.11294987146529563, 'fraction_numerical': 0.0, 'mean_word_length': 3.260780287474333, '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': '295218', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1581, 'n_words': 435}