Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
3The ``PATTERN_`` constants work with Django's ``path()`` conversion while the ``REGEX_`` constants work with
4``re_path()``.
6"""
8# Exports
10__all__ = (
11 "PATTERN_LOOKUP",
12 "PATTERN_MD5",
13 "PATTERN_PATH",
14 "PATTERN_PK",
15 "PATTERN_SLUG",
16 "PATTERN_UUID",
17 "PATTERN_VERSION",
18 "PATTERN_YEAR",
19 "PATTERNS",
20 "REGEX_LOOKUP",
21 "REGEX_PASSWORD_RESET",
22 "REGEX_PK",
23 "REGEX_SLUG",
24 "REGEX_UUID",
25 "REGEX_VERSION",
26 "REGEX_PATTERNS",
27)
29# Constants
31PATTERN_LOOKUP = '<str:{lookup}>'
32"""
33``PATTERN_LOOKUP`` may be used for all other lookup fields, but cannot be used as is. The lookup keyword must be
34formatted in order to use the pattern:
36.. code-block:: py
38 from superdjango.patterns import PATTERN_LOOKUP
40 class TaskList(ListView):
41 lookup_field = PATTERN_LOOKUP.format(**{'lookup': "my_unique_field_name"})
42 # ...
44"""
46PATTERN_MD5 = '<md5:hash>'
47"""A pattern which matches an MD5 hex digest string. The MD5 converter must be registered to use this pattern.
49.. code-block:: python
51 # main/urls.py
52 from django.urls import register_converter
53 from superdjango.patterns import PATTERN_MD5, MD5Converter
55 register_converter(MD5Converter, "md5")
57"""
59PATTERN_PATH = '<path:path>'
60"""Match any non-empty string."""
62PATTERN_PK = '<int:pk>'
63"""The standard pattern for classed-based views, assuming the primary key of the model is used for the lookup field."""
65PATTERN_SLUG = '<slug:slug>'
66"""Used when the lookup field is a ``SlugField`` named ``slug``. You should ensure that the slug field is unique."""
68PATTERN_UUID = '<uuid:uuid>'
69"""A regex pattern that may be used when the lookup field is a ``UUIDField`` named ``uuid``."""
71PATTERN_VERSION = '<version:version>'
72"""Matches a `semantic versioning <semver.org>`_ string. The version converter must be registered to use this pattern.
74.. code-block:: python
76 # main/urls.py
77 from django.urls import register_converter
78 from superdjango.patterns import PATTERN_VERSION, VersionConverter
80 register_converter(VersionConverter, "version")
82"""
84PATTERN_YEAR = '<yyyy:year>'
85"""A pattern which matches a four digit year. The corresponding converter must be registered.
87.. code-block:: python
89 # main/urls.py
90 from django.urls import register_converter
91 from superdjango.patterns import PATTERN_YEAR, FourDigitYearConverter
93 register_converter(FourDigitYearConverter, "yyyy")
95"""
97PATTERNS = {
98 'lookup': PATTERN_LOOKUP,
99 'md5': PATTERN_MD5,
100 'path': PATTERN_PATH,
101 'pk': PATTERN_PK,
102 'slug': PATTERN_SLUG,
103 'uuid': PATTERN_UUID,
104 'version': PATTERN_VERSION,
105 'year': PATTERN_YEAR,
106}
107"""A dictionary of pattern values."""
109REGEX_LOOKUP = r'(?P<{lookup}>[^/]+)'
110"""
111``REGEX_LOOKUP`` may be used for all other lookup fields, but cannot be used as is. The lookup keyword must be
112formatted in order to use the pattern:
114.. code-block:: py
116 from superdjango.patterns import REGEX_LOOKUP
118 context = {'lookup': "my_unique_field_name"}
119 lookup = REGEX_LOOKUP.format(**context)
121"""
123REGEX_PASSWORD_RESET = r"(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})"
124"""The standard pattern for password reset variables."""
126REGEX_PK = r'(?P<pk>\d+)'
127"""The standard pattern for classed-based views, assuming the primary key of the model is used for the lookup field."""
129REGEX_SLUG = r'(?P<slug>[\w-]+)'
130"""Used when the lookup field is a ``SlugField`` named ``slug``. You should ensure that the slug field is unique."""
132REGEX_UUID = r'(?P<uuid>[\w-]+)'
133"""A regex pattern that may be used when the lookup field is a ``UUIDField`` named ``uuid``."""
135REGEX_VERSION = r'(?P<version>[\d\w.-]+)'
136"""Matches a `semantic versioning <semver.org>`_ string."""
138REGEX_PATTERNS = {
139 'lookup': REGEX_LOOKUP,
140 'password': REGEX_PASSWORD_RESET,
141 'pk': REGEX_PK,
142 'slug': REGEX_SLUG,
143 'uuid': REGEX_UUID,
144 'version': REGEX_VERSION,
145}
146"""A collection of regex patterns that must be processed with ``re_path``."""