Ticket #3474: regex_flags_pass_to_patterns.patch

File regex_flags_pass_to_patterns.patch, 4.1 KB (added by frankie@…, 18 years ago)

Against [4485]

  • django/conf/urls/defaults.py

     
    77
    88include = lambda urlconf_module: [urlconf_module]
    99
    10 def patterns(prefix, *tuples):
     10def 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
    1118    pattern_list = []
    1219    for t in tuples:
    1320        regex, view_or_include = t[:2]
    1421        default_kwargs = t[2:]
    1522        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))
    1724        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))
    1926    return pattern_list
  • django/core/urlresolvers.py

     
    8888        return str(value) # TODO: Unicode?
    8989
    9090class RegexURLPattern(object):
    91     def __init__(self, regex, callback, default_args=None):
     91    def __init__(self, regex, callback, default_args=None, flags=0):
    9292        # regex is a string representing a regular expression.
    9393        # callback is either a string like 'foo.views.news.stories.story_detail'
    9494        # which represents the path to a module and a view function name, or a
    9595        # callable object (view).
    96         self.regex = re.compile(regex)
     96        self.regex = re.compile(regex, flags)
    9797        if callable(callback):
    9898            self._callback = callback
    9999        else:
     
    144144        return reverse_helper(self.regex, *args, **kwargs)
    145145
    146146class RegexURLResolver(object):
    147     def __init__(self, regex, urlconf_name, default_kwargs=None):
     147    def __init__(self, regex, urlconf_name, default_kwargs=None, flags=0):
    148148        # regex is a string representing a regular expression.
    149149        # urlconf_name is a string representing the module containing urlconfs.
    150         self.regex = re.compile(regex)
     150        self.regex = re.compile(regex, flags)
    151151        self.urlconf_name = urlconf_name
    152152        self.callback = None
    153153        self.default_kwargs = default_kwargs or {}
  • docs/url_dispatch.txt

     
    148148In both cases, it will pass any extra keyword arguments as keyword arguments.
    149149See "Passing extra options to view functions" below.
    150150
     151Flags
     152=====
     153
     154Flags 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
    151158What the URLconf searches against
    152159=================================
    153160
     
    190197...where ``optional dictionary`` is optional. (See
    191198_`Passing extra options to view functions` below.)
    192199
     200Finally, the keyword argument flags can be passed containing `regexp flags_`to
     201be applied to each regular expression in that patterns block.
     202
    193203handler404
    194204----------
    195205
     
    215225A function that takes a full Python import path to another URLconf that should
    216226be "included" in this place. See _`Including other URLconfs` below.
    217227
     228.. _regexp flags: http://docs.python.org/lib/node46.html#l2h-394
     229
    218230Notes on capturing text in URLs
    219231===============================
    220232
Back to Top