SciPost Code Repository

Skip to content
Snippets Groups Projects
converters.py 774 B
Newer Older
Jean-Sébastien Caux's avatar
Jean-Sébastien Caux committed
__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"


from django.urls.converters import StringConverter


class UnicodeSlugConverter(StringConverter):
    regex = '[-\w_]+'


Jean-Sébastien Caux's avatar
Jean-Sébastien Caux committed
class FourDigitYearConverter:
    regex = '[0-9]{4}'

    def to_python(self, value):
        return int(value)

    def to_url(self, value):
        return '%04d' % value


class TwoDigitMonthConverter:
    regex = '(0[1-9]{1})|(1[1-2]{1})'

    def to_python(self, value):
        return int(value)

    def to_url(self, value):
        return str(value).zfill(2)


class TwoDigitDayConverter:
    regex = '([0-2]{1}[1-9])|30|31'

    def to_python(self, value):
        return int(value)

    def to_url(self, value):
        return str(value).zfill(2)