"""
Additional `path converters`_.
.. _path converters: https://docs.djangoproject.com/en/stable/topics/http/urls/#registering-custom-path-converters
"""
# Exports
__all__ = (
"FourDigitYearConverter",
"MD5Converter",
"VersionConverter",
)
# Converters
[docs]class FourDigitYearConverter:
"""Path converter for a year in YYYY format. Straight from the Django documentation."""
regex = '[0-9]{4}'
# noinspection PyMethodMayBeStatic
[docs] def to_python(self, value):
"""Return the value as an integration."""
return int(value)
# noinspection PyMethodMayBeStatic
[docs] def to_url(self, value):
"""Return the value for the URL."""
return '%04d' % value
[docs]class MD5Converter:
"""Path converter for matching an MD5 hex digest."""
regex = r"[\d\w]{32}"
# noinspection PyMethodMayBeStatic
[docs] def to_python(self, value):
"""Return the value as a string."""
return str(value)
# noinspection PyMethodMayBeStatic
[docs] def to_url(self, value):
"""Return the value for the URL."""
return "%s" % value
[docs]class VersionConverter:
"""Path converter for matching a `semantic versioning <semver.org>`_ string."""
regex = r"[\d\w.-]+"
# noinspection PyMethodMayBeStatic
[docs] def to_python(self, value):
"""Returns the version as a ``str``."""
return str(value)
# noinspection PyMethodMayBeStatic
[docs] def to_url(self, value):
"""Return the value for the URL."""
return "%s" % value