Skip to content

Commit de26af4

Browse files
committed
Move determine_path_prefix() logic into CoreAPI module.
1 parent e309a4f commit de26af4

File tree

2 files changed

+43
-44
lines changed

2 files changed

+43
-44
lines changed

rest_framework/schemas/coreapi.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@
2020
header_regex = re.compile('^[a-zA-Z][0-9A-Za-z_]*:')
2121

2222
# Generator #
23-
# TODO: Pull some of this into base.
23+
24+
25+
def common_path(paths):
26+
split_paths = [path.strip('/').split('/') for path in paths]
27+
s1 = min(split_paths)
28+
s2 = max(split_paths)
29+
common = s1
30+
for i, c in enumerate(s1):
31+
if c != s2[i]:
32+
common = s1[:i]
33+
break
34+
return '/' + '/'.join(common)
2435

2536

2637
def is_custom_action(action):
@@ -209,6 +220,37 @@ def get_keys(self, subpath, method, view):
209220
# Default action, eg "/users/", "/users/{pk}/"
210221
return named_path_components + [action]
211222

223+
def determine_path_prefix(self, paths):
224+
"""
225+
Given a list of all paths, return the common prefix which should be
226+
discounted when generating a schema structure.
227+
228+
This will be the longest common string that does not include that last
229+
component of the URL, or the last component before a path parameter.
230+
231+
For example:
232+
233+
/api/v1/users/
234+
/api/v1/users/{pk}/
235+
236+
The path prefix is '/api/v1'
237+
"""
238+
prefixes = []
239+
for path in paths:
240+
components = path.strip('/').split('/')
241+
initial_components = []
242+
for component in components:
243+
if '{' in component:
244+
break
245+
initial_components.append(component)
246+
prefix = '/'.join(initial_components[:-1])
247+
if not prefix:
248+
# We can just break early in the case that there's at least
249+
# one URL that doesn't have a path prefix.
250+
return '/'
251+
prefixes.append('/' + prefix + '/')
252+
return common_path(prefixes)
253+
212254
# View Inspectors #
213255

214256

rest_framework/schemas/generators.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@
1818
from rest_framework.utils.model_meta import _get_pk
1919

2020

21-
def common_path(paths):
22-
split_paths = [path.strip('/').split('/') for path in paths]
23-
s1 = min(split_paths)
24-
s2 = max(split_paths)
25-
common = s1
26-
for i, c in enumerate(s1):
27-
if c != s2[i]:
28-
common = s1[:i]
29-
break
30-
return '/' + '/'.join(common)
31-
32-
3321
def get_pk_name(model):
3422
meta = model._meta.concrete_model._meta
3523
return _get_pk(meta).name
@@ -236,37 +224,6 @@ def coerce_path(self, path, method, view):
236224
def get_schema(self, request=None, public=False):
237225
raise NotImplementedError(".get_schema() must be implemented in subclasses.")
238226

239-
def determine_path_prefix(self, paths):
240-
"""
241-
Given a list of all paths, return the common prefix which should be
242-
discounted when generating a schema structure.
243-
244-
This will be the longest common string that does not include that last
245-
component of the URL, or the last component before a path parameter.
246-
247-
For example:
248-
249-
/api/v1/users/
250-
/api/v1/users/{pk}/
251-
252-
The path prefix is '/api/v1'
253-
"""
254-
prefixes = []
255-
for path in paths:
256-
components = path.strip('/').split('/')
257-
initial_components = []
258-
for component in components:
259-
if '{' in component:
260-
break
261-
initial_components.append(component)
262-
prefix = '/'.join(initial_components[:-1])
263-
if not prefix:
264-
# We can just break early in the case that there's at least
265-
# one URL that doesn't have a path prefix.
266-
return '/'
267-
prefixes.append('/' + prefix + '/')
268-
return common_path(prefixes)
269-
270227
def has_view_permissions(self, path, method, view):
271228
"""
272229
Return `True` if the incoming request has the correct view permissions.

0 commit comments

Comments
 (0)