6
6
import os
7
7
import re
8
8
9
- import django
10
9
from django .urls import resolve
11
10
from django .urls .resolvers import get_resolver
12
11
import openapi_core
@@ -72,52 +71,13 @@ def _extract_headers(request):
72
71
return request_headers
73
72
74
73
75
- def _resolve_django1x (path , resolver = None ):
76
- """Resolve a given path to its matching regex (Django 1.x).
77
-
78
- This is essentially a re-implementation of ``RegexURLResolver.resolve``
79
- that builds and returns the matched regex instead of the view itself.
80
-
81
- >>> _resolve_django1x('/api/1.0/patches/1/checks/')
82
- "^api/(?:(?P<version>(1.0|1.1))/)patches/(?P<patch_id>[^/]+)/checks/$"
83
- """
84
- from django .urls .resolvers import RegexURLResolver # noqa
85
-
86
- resolver = resolver or get_resolver ()
87
- match = resolver .regex .search (path )
88
-
89
- if not match :
90
- return
91
-
92
- if isinstance (resolver , RegexURLResolver ):
93
- sub_path = path [match .end ():]
94
- for sub_resolver in resolver .url_patterns :
95
- sub_match = _resolve_django1x (sub_path , sub_resolver )
96
- if not sub_match :
97
- continue
98
-
99
- kwargs = dict (match .groupdict ())
100
- kwargs .update (sub_match [2 ])
101
- args = sub_match [1 ]
102
- if not kwargs :
103
- args = match .groups () + args
104
-
105
- regex = resolver .regex .pattern + sub_match [0 ].lstrip ('^' )
106
-
107
- return regex , args , kwargs
108
- else : # RegexURLPattern
109
- kwargs = match .groupdict ()
110
- args = () if kwargs else match .groups ()
111
- return resolver .regex .pattern , args , kwargs
112
-
113
-
114
- def _resolve_django2x (path , resolver = None ):
74
+ def _resolve (path , resolver = None ):
115
75
"""Resolve a given path to its matching regex (Django 2.x).
116
76
117
77
This is essentially a re-implementation of ``URLResolver.resolve`` that
118
78
builds and returns the matched regex instead of the view itself.
119
79
120
- >>> _resolve_django2x ('/api/1.0/patches/1/checks/')
80
+ >>> _resolve ('/api/1.0/patches/1/checks/')
121
81
"^api/(?:(?P<version>(1.0|1.1))/)patches/(?P<patch_id>[^/]+)/checks/$"
122
82
"""
123
83
from django .urls .resolvers import URLResolver # noqa
@@ -135,7 +95,7 @@ def _resolve_django2x(path, resolver=None):
135
95
if isinstance (resolver , URLResolver ):
136
96
sub_path , args , kwargs = match
137
97
for sub_resolver in resolver .url_patterns :
138
- sub_match = _resolve_django2x (sub_path , sub_resolver )
98
+ sub_match = _resolve (sub_path , sub_resolver )
139
99
if not sub_match :
140
100
continue
141
101
@@ -150,12 +110,6 @@ def _resolve_django2x(path, resolver=None):
150
110
return resolver .pattern ._regex , args , kwargs
151
111
152
112
153
- if django .VERSION < (2 , 0 ):
154
- _resolve = _resolve_django1x
155
- else :
156
- _resolve = _resolve_django2x
157
-
158
-
159
113
def _resolve_path_to_kwargs (path ):
160
114
"""Convert a path to the kwargs used to resolve it.
161
115
0 commit comments