SciPost Code Repository

Skip to content
Snippets Groups Projects
converters.py 2.16 KiB
Newer Older
__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"


from django.db.utils import ProgrammingError
class JournalDOILabelConverter:
    def __init__(self):
        try:
            from journals.models import Journal

            self.regex = "|".join([j.doi_label for j in Journal.objects.all()])
        except ProgrammingError:
            self.regex = "SciPost"

    def to_python(self, value):
        from journals.models import Journal
        try:
            return Journal.objects.get(doi_label=value).doi_label
        except Journal.DoesNotExist:
            return ValueError
        return value

    def to_url(self, value):
        return value
Jean-Sébastien Caux's avatar
Jean-Sébastien Caux committed


class IssueDOILabelConverter:
    """
    Converter for journal issue DOI labels.
    """

    def __init__(self):
        try:
            from journals.models import Journal

            self.regex = "|".join([j.doi_label for j in Journal.objects.all()])
Jean-Sébastien Caux's avatar
Jean-Sébastien Caux committed
        except ProgrammingError:
            self.regex = "SciPost"
        self.regex = "(" + self.regex + ")" + r"\.[0-9]+(\.[0-9]+)?"
Jean-Sébastien Caux's avatar
Jean-Sébastien Caux committed

    def to_python(self, value):
        from journals.models import Publication
Jean-Sébastien Caux's avatar
Jean-Sébastien Caux committed
        try:
            return Publication.objects.get(doi_label=value).doi_label
        except Publication.DoesNotExist:
            return ValueError
        return value

    def to_url(self, value):
        return value


class PublicationDOILabelConverter:
    """
    Converter for publication DOI labels.
    """

    def __init__(self):
        try:
            from journals.models import Journal

            self.regex = "|".join([j.doi_label for j in Journal.objects.all()])
Jean-Sébastien Caux's avatar
Jean-Sébastien Caux committed
        except ProgrammingError:
            self.regex = "SciPost"
Jean-Sébastien Caux's avatar
Jean-Sébastien Caux committed
        self.regex = (
            "("
            + self.regex
            + ")"
            + r"\.[0-9]+(\.[0-9]+(\.[0-9]+)?)?(-r[0-9]+(\.[0-9]+)?)?"
        )
Jean-Sébastien Caux's avatar
Jean-Sébastien Caux committed

    def to_python(self, value):
        from journals.models import Publication
Jean-Sébastien Caux's avatar
Jean-Sébastien Caux committed
        try:
            return Publication.objects.get(doi_label=value).doi_label
        except Publication.DoesNotExist:
            return ValueError
        return value

    def to_url(self, value):
        return value