Ticket #3474: regex_flags_pass_to_patterns.patch
File regex_flags_pass_to_patterns.patch, 4.1 KB (added by , 18 years ago) |
---|
-
django/conf/urls/defaults.py
7 7 8 8 include = lambda urlconf_module: [urlconf_module] 9 9 10 def patterns(prefix, *tuples): 10 def patterns(prefix, *tuples, **kwargs): 11 for key in kwargs.keys():# this is the only way otherwise one of tuples gets eaten 12 if key != 'flags': 13 raise ValueError("The only keyword argument patterns accepts is flags") 14 if 'flags' in kwargs: 15 flags = kwargs['flags'] 16 else: 17 flags = 0 11 18 pattern_list = [] 12 19 for t in tuples: 13 20 regex, view_or_include = t[:2] 14 21 default_kwargs = t[2:] 15 22 if type(view_or_include) == list: 16 pattern_list.append(RegexURLResolver(regex, view_or_include[0], *default_kwargs))23 pattern_list.append(RegexURLResolver(regex, view_or_include[0], flags=flags, *default_kwargs)) 17 24 else: 18 pattern_list.append(RegexURLPattern(regex, prefix and (prefix + '.' + view_or_include) or view_or_include, *default_kwargs))25 pattern_list.append(RegexURLPattern(regex, prefix and (prefix + '.' + view_or_include) or view_or_include, flags=flags, *default_kwargs)) 19 26 return pattern_list -
django/core/urlresolvers.py
88 88 return str(value) # TODO: Unicode? 89 89 90 90 class RegexURLPattern(object): 91 def __init__(self, regex, callback, default_args=None ):91 def __init__(self, regex, callback, default_args=None, flags=0): 92 92 # regex is a string representing a regular expression. 93 93 # callback is either a string like 'foo.views.news.stories.story_detail' 94 94 # which represents the path to a module and a view function name, or a 95 95 # callable object (view). 96 self.regex = re.compile(regex )96 self.regex = re.compile(regex, flags) 97 97 if callable(callback): 98 98 self._callback = callback 99 99 else: … … 144 144 return reverse_helper(self.regex, *args, **kwargs) 145 145 146 146 class RegexURLResolver(object): 147 def __init__(self, regex, urlconf_name, default_kwargs=None ):147 def __init__(self, regex, urlconf_name, default_kwargs=None, flags=0): 148 148 # regex is a string representing a regular expression. 149 149 # urlconf_name is a string representing the module containing urlconfs. 150 self.regex = re.compile(regex )150 self.regex = re.compile(regex, flags) 151 151 self.urlconf_name = urlconf_name 152 152 self.callback = None 153 153 self.default_kwargs = default_kwargs or {} -
docs/url_dispatch.txt
148 148 In both cases, it will pass any extra keyword arguments as keyword arguments. 149 149 See "Passing extra options to view functions" below. 150 150 151 Flags 152 ===== 153 154 Flags to be used on the regular expressions in a patterns block can be passed to it via the keyword argument flags. The flags must be imported from python's built in ``re`` module. See `the contents of the re module`_ for a list of flags. 155 156 .. _the contents of the re module: http://diveintopython.org/regular_expressions/street_addresses.html#re.matching.2.3 157 151 158 What the URLconf searches against 152 159 ================================= 153 160 … … 190 197 ...where ``optional dictionary`` is optional. (See 191 198 _`Passing extra options to view functions` below.) 192 199 200 Finally, the keyword argument flags can be passed containing `regexp flags_`to 201 be applied to each regular expression in that patterns block. 202 193 203 handler404 194 204 ---------- 195 205 … … 215 225 A function that takes a full Python import path to another URLconf that should 216 226 be "included" in this place. See _`Including other URLconfs` below. 217 227 228 .. _regexp flags: http://docs.python.org/lib/node46.html#l2h-394 229 218 230 Notes on capturing text in URLs 219 231 =============================== 220 232