Code

Ticket #2784: http-methods-patch.diff

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

HTTP Method support in URLs patch

Line 
1Index: /Users/smn/Documents/Repositories/Django/django/conf/urls/defaults.py
2===================================================================
3--- /Users/smn/Documents/Repositories/Django/django/conf/urls/defaults.py       (revision 3777)
4+++ /Users/smn/Documents/Repositories/Django/django/conf/urls/defaults.py       (working copy)
5@@ -7,6 +7,8 @@
6 
7 include = lambda urlconf_module: [urlconf_module]
8 
9+request_methods = ['GET','POST','PUT','DELETE']
10+
11 def patterns(prefix, *tuples):
12     pattern_list = []
13     for t in tuples:
14@@ -14,6 +16,9 @@
15         default_kwargs = t[2:]
16         if type(view_or_include) == list:
17             pattern_list.append(RegexURLResolver(regex, view_or_include[0], *default_kwargs))
18+        elif type(view_or_include) == dict:
19+            for m in [method for method in view_or_include.keys() if method in request_methods]:
20+                pattern_list.append(RegexURLPattern(regex, prefix and (prefix + '.' + view_or_include.get(m)) or view_or_include.get(m), method=m, *default_kwargs))
21         else:
22             pattern_list.append(RegexURLPattern(regex, prefix and (prefix + '.' + view_or_include) or view_or_include, *default_kwargs))
23     return pattern_list
24Index: /Users/smn/Documents/Repositories/Django/django/core/urlresolvers.py
25===================================================================
26--- /Users/smn/Documents/Repositories/Django/django/core/urlresolvers.py        (revision 3777)
27+++ /Users/smn/Documents/Repositories/Django/django/core/urlresolvers.py        (working copy)
28@@ -84,12 +84,13 @@
29         return str(value) # TODO: Unicode?
30 
31 class RegexURLPattern(object):
32-    def __init__(self, regex, callback, default_args=None):
33+    def __init__(self, regex, callback, method=None, default_args=None):
34         # regex is a string representing a regular expression.
35         # callback is either a string like 'foo.views.news.stories.story_detail'
36         # which represents the path to a module and a view function name, or a
37         # callable object (view).
38         self.regex = re.compile(regex)
39+        self.method = method
40         if callable(callback):
41             self._callback = callback
42         else:
43@@ -97,9 +98,9 @@
44             self._callback_str = callback
45         self.default_args = default_args or {}
46 
47-    def resolve(self, path):
48+    def resolve(self, path, method=None):
49         match = self.regex.search(path)
50-        if match:
51+        if match and method == self.method:
52             # If there are any named groups, use those as kwargs, ignoring
53             # non-named groups. Otherwise, pass all non-named arguments as
54             # positional arguments.
55@@ -148,14 +149,14 @@
56         self.callback = None
57         self.default_kwargs = default_kwargs or {}
58 
59-    def resolve(self, path):
60+    def resolve(self, path, method=None):
61         tried = []
62         match = self.regex.search(path)
63         if match:
64             new_path = path[match.end():]
65             for pattern in self.urlconf_module.urlpatterns:
66                 try:
67-                    sub_match = pattern.resolve(new_path)
68+                    sub_match = pattern.resolve(new_path,method=method)
69                 except Resolver404, e:
70                     tried.extend([(pattern.regex.pattern + '   ' + t) for t in e.args[0]['tried']])
71                 else:
72@@ -220,7 +221,7 @@
73         result = reverse_helper(self.regex, *args, **kwargs)
74         return result + sub_match
75 
76-def resolve(path, urlconf=None):
77+def resolve(path, urlconf=None, method=None):
78     if urlconf is None:
79         from django.conf import settings
80         urlconf = settings.ROOT_URLCONF
81Index: /Users/smn/Documents/Repositories/Django/django/core/handlers/base.py
82===================================================================
83--- /Users/smn/Documents/Repositories/Django/django/core/handlers/base.py       (revision 3777)
84+++ /Users/smn/Documents/Repositories/Django/django/core/handlers/base.py       (working copy)
85@@ -62,7 +62,7 @@
86 
87         resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF)
88         try:
89-            callback, callback_args, callback_kwargs = resolver.resolve(path)
90+            callback, callback_args, callback_kwargs = resolver.resolve(path, method=request.method)
91 
92             # Apply view middleware
93             for middleware_method in self._view_middleware: