<commit_before>from modularodm.exceptions import ValidationValueError
from rest_framework import exceptions
from rest_framework import serializers as ser

from api.base.serializers import (
    JSONAPISerializer, IDField, JSONAPIListField, LinksField,
    RelationshipField, JSONAPIRelationshipSerializer, relationship_diff
)
from api.base.exceptions import Conflict, RelationshipPostMakesNoChanges
from api.base.utils import absolute_reverse, get_user_auth
from api.nodes.serializers import NodeTagField
from api.taxonomies.serializers import TaxonomyField
from framework.exceptions import PermissionsError
from website.models import StoredFileNode, PreprintProvider, Node


class PrimaryFileRelationshipField(RelationshipField):
    def get_object(self, file_id):
        return StoredFileNode.load(file_id)

    def to_internal_value(self, data):
        file = self.get_object(data)
        return {'primary_file': file}


class PreprintSerializer(JSONAPISerializer):
    filterable_fields = frozenset([
        'id',
        'title',
        'tags',
        'date_created',
        'date_modified',
        'contributors',
        'subjects',
        'doi'
    ])

    title = ser.CharField(required=False)
    subjects = JSONAPIListField(child=TaxonomyField(), required=False, source='preprint_subjects')
    provider = ser.CharField(source='preprint_provider', required=False)
    date_created = ser.DateTimeField(read_only=True, source='preprint_created')
    date_modified = ser.DateTimeField(read_only=True)
    id = IDField(source='_id', required=False)
    abstract = ser.CharField(source='description', required=False)
    tags = JSONAPIListField(child=NodeTagField(), required=False)
    doi = ser.CharField(source='preprint_doi', required=False)

    primary_file = PrimaryFileRelationshipField(
        related_view='files:file-detail',
        related_view_kwargs={'file_id': '<preprint_file._id>'},
        lookup_url_kwarg='file_id',
        read_only=False
    )

    files = RelationshipField(
        related_view='nodes:node-providers',
        related_view_kwargs={'node_id': '<pk>'}
    )

    providers = RelationshipField(
        related_view='preprints:preprint-preprint_providers',
        related_view_kwargs={'node_id': '<pk>'},
        self_view='preprints:preprint-relationships-preprint_providers',
        self_view_kwargs={'node_id': '<pk>'}
    )

    links = LinksField(
        {
            'self': 'get_preprint_url',
            'html': 'get_absolute_html_url',
            'doi': 'get_doi_url'
        }
    )

    contributors = RelationshipField(
        related_view='preprints:preprint-contributors',
        related_view_kwargs={'node_id': '<pk>'},
        related_meta={'count': 'get_contrib_count'},
    )

    class Meta:
        type_ = 'preprints'

    def get_preprint_url(self, obj):
        return absolute_reverse('preprints:preprint-detail', kwargs={'node_id': obj._id})

    def get_absolute_url(self, obj):
        return self.get_preprint_url(obj)

    def get_doi_url(self, obj):
        return 'https://dx.doi.org/{}'.format(obj.preprint_doi)

    def create(self, validated_data):
        node = Node.load(validated_data.pop('_id', None))

        if not node:<commit_msg>Properly exclude DOI url if DOI is none<commit_after>from modularodm.exceptions import ValidationValueError
from rest_framework import exceptions
from rest_framework import serializers as ser

from api.base.serializers import (
    JSONAPISerializer, IDField, JSONAPIListField, LinksField,
    RelationshipField, JSONAPIRelationshipSerializer, relationship_diff
)
from api.base.exceptions import Conflict, RelationshipPostMakesNoChanges
from api.base.utils import absolute_reverse, get_user_auth
from api.nodes.serializers import NodeTagField
from api.taxonomies.serializers import TaxonomyField
from framework.exceptions import PermissionsError
from website.models import StoredFileNode, PreprintProvider, Node


class PrimaryFileRelationshipField(RelationshipField):
    def get_object(self, file_id):
        return StoredFileNode.load(file_id)

    def to_internal_value(self, data):
        file = self.get_object(data)
        return {'primary_file': file}


class PreprintSerializer(JSONAPISerializer):
    filterable_fields = frozenset([
        'id',
        'title',
        'tags',
        'date_created',
        'date_modified',
        'contributors',
        'subjects',
        'doi'
    ])

    title = ser.CharField(required=False)
    subjects = JSONAPIListField(child=TaxonomyField(), required=False, source='preprint_subjects')
    provider = ser.CharField(source='preprint_provider', required=False)
    date_created = ser.DateTimeField(read_only=True, source='preprint_created')
    date_modified = ser.DateTimeField(read_only=True)
    id = IDField(source='_id', required=False)
    abstract = ser.CharField(source='description', required=False)
    tags = JSONAPIListField(child=NodeTagField(), required=False)
    doi = ser.CharField(source='preprint_doi', required=False)

    primary_file = PrimaryFileRelationshipField(
        related_view='files:file-detail',
        related_view_kwargs={'file_id': '<preprint_file._id>'},
        lookup_url_kwarg='file_id',
        read_only=False
    )

    files = RelationshipField(
        related_view='nodes:node-providers',
        related_view_kwargs={'node_id': '<pk>'}
    )

    providers = RelationshipField(
        related_view='preprints:preprint-preprint_providers',
        related_view_kwargs={'node_id': '<pk>'},
        self_view='preprints:preprint-relationships-preprint_providers',
        self_view_kwargs={'node_id': '<pk>'}
    )

    links = LinksField(
        {
            'self': 'get_preprint_url',
            'html': 'get_absolute_html_url',
            'doi': 'get_doi_url'
        }
    )

    contributors = RelationshipField(
        related_view='preprints:preprint-contributors',
        related_view_kwargs={'node_id': '<pk>'},
        related_meta={'count': 'get_contrib_count'},
    )

    class Meta:
        type_ = 'preprints'

    def get_preprint_url(self, obj):
        return absolute_reverse('preprints:preprint-detail', kwargs={'node_id': obj._id})

    def get_absolute_url(self, obj):
        return self.get_preprint_url(obj)

    def get_doi_url(self, obj):
        return 'https://dx.doi.org/{}'.format(obj.preprint_doi) if obj.preprint_doi else None

    def create(self, validated_data):
        node = Node.load(validated_data.pop('_id', None))

        if not node:
{'fraction_non_alphanumeric': 0.10765696784073507, 'fraction_numerical': 0.0, 'mean_word_length': 3.848552338530067, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '4905144', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1928, 'n_words': 350}