SciPost Code Repository

Skip to content
Snippets Groups Projects
models.py 1.86 KiB
Newer Older
__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
Jorran de Wit's avatar
Jorran de Wit committed
__license__ = "AGPL v3"


from django.urls import reverse
Jorran de Wit's avatar
Jorran de Wit committed
from django.db import models
from django.http import Http404


class Preprint(models.Model):
    """
    A preprint object, either at SciPost or with link to external preprint server.
Jorran de Wit's avatar
Jorran de Wit committed

    If the instance is a SciPost preprint, the `_file` field should be filled.
Jorran de Wit's avatar
Jorran de Wit committed
    """

    # (arXiv) identifiers with/without version number
    identifier_w_vn_nr = models.CharField(max_length=25, unique=True, db_index=True)
Jorran de Wit's avatar
Jorran de Wit committed
    url = models.URLField(blank=True)
Jorran de Wit's avatar
Jorran de Wit committed

Jorran de Wit's avatar
Jorran de Wit committed
    _file = models.FileField(
        verbose_name='Preprint file', help_text='Preprint file for SciPost standalone preprints',
        upload_to='UPLOADS/PREPRINTS/%Y/%m/', max_length=200, blank=True)
Jorran de Wit's avatar
Jorran de Wit committed

    # Dates
    modified = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-identifier_w_vn_nr']


Jorran de Wit's avatar
Jorran de Wit committed
    def __str__(self):
        return 'Preprint {}'.format(self.identifier_w_vn_nr)

    def get_absolute_url(self):
        """Return either saved url or url to open the pdf."""
Jorran de Wit's avatar
Jorran de Wit committed
        if self.url:
            return self.url
        if self._file:
            return reverse('preprints:pdf', args=(self.identifier_w_vn_nr,))
Jorran de Wit's avatar
Jorran de Wit committed
        raise Http404

    @property
    def citation_pdf_url(self):
        """Return the absolute URL of the pdf for the meta tag for Google Scholar."""
        if self._file: # means this is a SciPost-hosted preprint
            return "https://scipost.org%s" % self.get_absolute_url()
        elif self.is_arXiv:
            return self.get_absolute_url().replace("/abs/", "/pdf/")
        else:
            return self.get_absolute_url()

    @property
    def is_arXiv(self):
        """Return True if this preprint is hosted on arXiv."""
        return 'arxiv.org' in self.url