Allow suffix_require=None, only non-suffixed URL

In the current format_suffix_patterns, there are only two options
1) suffix_required=False: URL with both suffix and non-suffixed will be generated
2) suffix_required=True: Only URL with suffixed will be generated.
There is no option for 
3) suffix_required=None: Only URL with non-suffixed will be generated.
By allowing suffix_required to None, we can achieve 3). This makes it cleaner when you use django rest swagger as you will not have duplicated endpoint one with suffix and the other without suffix from appearing.
This commit is contained in:
Jax Xie 2019-02-11 16:44:40 +08:00 committed by GitHub
parent dc6b3bf42e
commit 83f744350e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -76,8 +76,10 @@ def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required, suffix_r
new_pattern = path(route, view, kwargs, name)
else:
new_pattern = url(regex, view, kwargs, name)
ret.append(new_pattern)
# If suffix_required is None, we don't need suffix at all
if suffix_required is not None:
ret.append(new_pattern)
return ret
@ -93,6 +95,7 @@ def format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None):
suffix_required:
If `True`, only suffixed URLs will be generated, and non-suffixed
URLs will not be used. Defaults to `False`.
If None, only non-suffixed URLs will be used.
allowed:
An optional tuple/list of allowed suffixes. eg ['json', 'api']