Hide keyboard shortcuts

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""" 

2 

3The ``PATTERN_`` constants work with Django's ``path()`` conversion while the ``REGEX_`` constants work with 

4``re_path()``. 

5 

6""" 

7 

8# Exports 

9 

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) 

28 

29# Constants 

30 

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: 

35 

36.. code-block:: py 

37 

38 from superdjango.patterns import PATTERN_LOOKUP 

39 

40 class TaskList(ListView): 

41 lookup_field = PATTERN_LOOKUP.format(**{'lookup': "my_unique_field_name"}) 

42 # ... 

43 

44""" 

45 

46PATTERN_MD5 = '<md5:hash>' 

47"""A pattern which matches an MD5 hex digest string. The MD5 converter must be registered to use this pattern. 

48 

49.. code-block:: python 

50 

51 # main/urls.py 

52 from django.urls import register_converter 

53 from superdjango.patterns import PATTERN_MD5, MD5Converter 

54  

55 register_converter(MD5Converter, "md5") 

56 

57""" 

58 

59PATTERN_PATH = '<path:path>' 

60"""Match any non-empty string.""" 

61 

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.""" 

64 

65PATTERN_SLUG = '<slug:slug>' 

66"""Used when the lookup field is a ``SlugField`` named ``slug``. You should ensure that the slug field is unique.""" 

67 

68PATTERN_UUID = '<uuid:uuid>' 

69"""A regex pattern that may be used when the lookup field is a ``UUIDField`` named ``uuid``.""" 

70 

71PATTERN_VERSION = '<version:version>' 

72"""Matches a `semantic versioning <semver.org>`_ string. The version converter must be registered to use this pattern. 

73 

74.. code-block:: python 

75 

76 # main/urls.py 

77 from django.urls import register_converter 

78 from superdjango.patterns import PATTERN_VERSION, VersionConverter 

79  

80 register_converter(VersionConverter, "version") 

81 

82""" 

83 

84PATTERN_YEAR = '<yyyy:year>' 

85"""A pattern which matches a four digit year. The corresponding converter must be registered. 

86 

87.. code-block:: python 

88 

89 # main/urls.py 

90 from django.urls import register_converter 

91 from superdjango.patterns import PATTERN_YEAR, FourDigitYearConverter 

92  

93 register_converter(FourDigitYearConverter, "yyyy") 

94 

95""" 

96 

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.""" 

108 

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: 

113 

114.. code-block:: py 

115 

116 from superdjango.patterns import REGEX_LOOKUP 

117 

118 context = {'lookup': "my_unique_field_name"} 

119 lookup = REGEX_LOOKUP.format(**context) 

120 

121""" 

122 

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.""" 

125 

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.""" 

128 

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.""" 

131 

132REGEX_UUID = r'(?P<uuid>[\w-]+)' 

133"""A regex pattern that may be used when the lookup field is a ``UUIDField`` named ``uuid``.""" 

134 

135REGEX_VERSION = r'(?P<version>[\d\w.-]+)' 

136"""Matches a `semantic versioning <semver.org>`_ string.""" 

137 

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``."""