Ticket #2784: http-methods-patch.diff

File http-methods-patch.diff, 4.3 KB (added by simon@…, 9 years ago)

HTTP Method support in URLs patch

  • Users/smn/Documents/Repositories/Django/django/conf/urls/defaults.py

     
    77
    88include = lambda urlconf_module: [urlconf_module]
    99
     10request_methods = ['GET','POST','PUT','DELETE']
     11
    1012def patterns(prefix, *tuples):
    1113    pattern_list = []
    1214    for t in tuples:
     
    1416        default_kwargs = t[2:]
    1517        if type(view_or_include) == list:
    1618            pattern_list.append(RegexURLResolver(regex, view_or_include[0], *default_kwargs))
     19        elif type(view_or_include) == dict:
     20            for m in [method for method in view_or_include.keys() if method in request_methods]:
     21                pattern_list.append(RegexURLPattern(regex, prefix and (prefix + '.' + view_or_include.get(m)) or view_or_include.get(m), method=m, *default_kwargs))
    1722        else:
    1823            pattern_list.append(RegexURLPattern(regex, prefix and (prefix + '.' + view_or_include) or view_or_include, *default_kwargs))
    1924    return pattern_list
  • Users/smn/Documents/Repositories/Django/django/core/urlresolvers.py

     
    8484        return str(value) # TODO: Unicode?
    8585
    8686class RegexURLPattern(object):
    87     def __init__(self, regex, callback, default_args=None):
     87    def __init__(self, regex, callback, method=None, default_args=None):
    8888        # regex is a string representing a regular expression.
    8989        # callback is either a string like 'foo.views.news.stories.story_detail'
    9090        # which represents the path to a module and a view function name, or a
    9191        # callable object (view).
    9292        self.regex = re.compile(regex)
     93        self.method = method
    9394        if callable(callback):
    9495            self._callback = callback
    9596        else:
     
    9798            self._callback_str = callback
    9899        self.default_args = default_args or {}
    99100
    100     def resolve(self, path):
     101    def resolve(self, path, method=None):
    101102        match = self.regex.search(path)
    102         if match:
     103        if match and method == self.method:
    103104            # If there are any named groups, use those as kwargs, ignoring
    104105            # non-named groups. Otherwise, pass all non-named arguments as
    105106            # positional arguments.
     
    148149        self.callback = None
    149150        self.default_kwargs = default_kwargs or {}
    150151
    151     def resolve(self, path):
     152    def resolve(self, path, method=None):
    152153        tried = []
    153154        match = self.regex.search(path)
    154155        if match:
    155156            new_path = path[match.end():]
    156157            for pattern in self.urlconf_module.urlpatterns:
    157158                try:
    158                     sub_match = pattern.resolve(new_path)
     159                    sub_match = pattern.resolve(new_path,method=method)
    159160                except Resolver404, e:
    160161                    tried.extend([(pattern.regex.pattern + '   ' + t) for t in e.args[0]['tried']])
    161162                else:
     
    220221        result = reverse_helper(self.regex, *args, **kwargs)
    221222        return result + sub_match
    222223
    223 def resolve(path, urlconf=None):
     224def resolve(path, urlconf=None, method=None):
    224225    if urlconf is None:
    225226        from django.conf import settings
    226227        urlconf = settings.ROOT_URLCONF
  • Users/smn/Documents/Repositories/Django/django/core/handlers/base.py

     
    6262
    6363        resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF)
    6464        try:
    65             callback, callback_args, callback_kwargs = resolver.resolve(path)
     65            callback, callback_args, callback_kwargs = resolver.resolve(path, method=request.method)
    6666
    6767            # Apply view middleware
    6868            for middleware_method in self._view_middleware:
Back to Top