mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 08:29:59 +03:00
another batch
found more with another pattern
This commit is contained in:
parent
431830261b
commit
268298397d
|
@ -297,7 +297,7 @@ class OrderingFilter(BaseFilterBackend):
|
||||||
ascending_translation = _("ascending")
|
ascending_translation = _("ascending")
|
||||||
descending_translation = _("descending")
|
descending_translation = _("descending")
|
||||||
options.append((key, f'{label} - {ascending_translation}'))
|
options.append((key, f'{label} - {ascending_translation}'))
|
||||||
options.append(('-' + key, f'{label} - {descending_translation}'))
|
options.append((f'-{key}', f'{label} - {descending_translation}'))
|
||||||
context['options'] = options
|
context['options'] = options
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
|
@ -118,7 +118,7 @@ def _reverse_ordering(ordering_tuple):
|
||||||
ordering and return a new tuple, eg. `('created', '-uuid')`.
|
ordering and return a new tuple, eg. `('created', '-uuid')`.
|
||||||
"""
|
"""
|
||||||
def invert(x):
|
def invert(x):
|
||||||
return x[1:] if x.startswith('-') else '-' + x
|
return x[1:] if x.startswith('-') else f'-{x}'
|
||||||
|
|
||||||
return tuple([invert(item) for item in ordering_tuple])
|
return tuple([invert(item) for item in ordering_tuple])
|
||||||
|
|
||||||
|
|
|
@ -344,7 +344,7 @@ class HTMLFormRenderer(BaseRenderer):
|
||||||
if 'template' in style:
|
if 'template' in style:
|
||||||
template_name = style['template']
|
template_name = style['template']
|
||||||
else:
|
else:
|
||||||
template_name = style['template_pack'].strip('/') + '/' + style['base_template']
|
template_name = style['template_pack'].strip('/') + f"/{style['base_template']}"
|
||||||
|
|
||||||
template = loader.get_template(template_name)
|
template = loader.get_template(template_name)
|
||||||
context = {'field': field, 'style': style}
|
context = {'field': field, 'style': style}
|
||||||
|
|
|
@ -254,7 +254,7 @@ class SimpleRouter(BaseRouter):
|
||||||
# so a slash in the beginning will (A) cause Django to give
|
# so a slash in the beginning will (A) cause Django to give
|
||||||
# warnings and (B) generate URLS that will require using '//'.
|
# warnings and (B) generate URLS that will require using '//'.
|
||||||
if not prefix and regex[:2] == '^/':
|
if not prefix and regex[:2] == '^/':
|
||||||
regex = '^' + regex[2:]
|
regex = f'^{regex[2:]}'
|
||||||
|
|
||||||
initkwargs = route.initkwargs.copy()
|
initkwargs = route.initkwargs.copy()
|
||||||
initkwargs.update({
|
initkwargs.update({
|
||||||
|
@ -283,7 +283,7 @@ class APIRootView(views.APIView):
|
||||||
namespace = request.resolver_match.namespace
|
namespace = request.resolver_match.namespace
|
||||||
for key, url_name in self.api_root_dict.items():
|
for key, url_name in self.api_root_dict.items():
|
||||||
if namespace:
|
if namespace:
|
||||||
url_name = namespace + ':' + url_name
|
url_name = f"{namespace}:{url_name}"
|
||||||
try:
|
try:
|
||||||
ret[key] = reverse(
|
ret[key] = reverse(
|
||||||
url_name,
|
url_name,
|
||||||
|
|
|
@ -240,7 +240,7 @@ class SchemaGenerator(BaseSchemaGenerator):
|
||||||
# We can just break early in the case that there's at least
|
# We can just break early in the case that there's at least
|
||||||
# one URL that doesn't have a path prefix.
|
# one URL that doesn't have a path prefix.
|
||||||
return '/'
|
return '/'
|
||||||
prefixes.append('/' + prefix + '/')
|
prefixes.append(f'/{prefix}/')
|
||||||
return common_path(prefixes)
|
return common_path(prefixes)
|
||||||
|
|
||||||
# View Inspectors #
|
# View Inspectors #
|
||||||
|
|
|
@ -97,7 +97,7 @@ class ViewInspector:
|
||||||
current_section, separator, lead = line.partition(':')
|
current_section, separator, lead = line.partition(':')
|
||||||
sections[current_section] = lead.strip()
|
sections[current_section] = lead.strip()
|
||||||
else:
|
else:
|
||||||
sections[current_section] += '\n' + line
|
sections[current_section] += f'\n{line}'
|
||||||
|
|
||||||
# TODO: SCHEMA_COERCE_METHOD_NAMES appears here and in `SchemaGenerator.get_keys`
|
# TODO: SCHEMA_COERCE_METHOD_NAMES appears here and in `SchemaGenerator.get_keys`
|
||||||
coerce_method_names = api_settings.SCHEMA_COERCE_METHOD_NAMES
|
coerce_method_names = api_settings.SCHEMA_COERCE_METHOD_NAMES
|
||||||
|
|
|
@ -477,7 +477,7 @@ class Serializer(BaseSerializer, metaclass=SerializerMetaclass):
|
||||||
fields = self._writable_fields
|
fields = self._writable_fields
|
||||||
|
|
||||||
for field in fields:
|
for field in fields:
|
||||||
validate_method = getattr(self, 'validate_' + field.field_name, None)
|
validate_method = getattr(self, f'validate_{field.field_name}', None)
|
||||||
primitive_value = field.get_value(data)
|
primitive_value = field.get_value(data)
|
||||||
try:
|
try:
|
||||||
validated_value = field.run_validation(primitive_value)
|
validated_value = field.run_validation(primitive_value)
|
||||||
|
|
|
@ -187,12 +187,9 @@ def add_class(value, css_class):
|
||||||
html = str(value)
|
html = str(value)
|
||||||
match = class_re.search(html)
|
match = class_re.search(html)
|
||||||
if match:
|
if match:
|
||||||
m = re.search(r'^%s$|^%s\s|\s%s\s|\s%s$' % (css_class, css_class,
|
m = re.search(fr'[^|\s]{css_class}[$|\s]', match.group(1))
|
||||||
css_class, css_class),
|
|
||||||
match.group(1))
|
|
||||||
if not m:
|
if not m:
|
||||||
return mark_safe(class_re.sub(match.group(1) + " " + css_class,
|
return mark_safe(class_re.sub(f"{match.group(1)} {css_class}", html))
|
||||||
html))
|
|
||||||
else:
|
else:
|
||||||
return mark_safe(html.replace('>', f' class="{css_class}">', 1))
|
return mark_safe(html.replace('>', f' class="{css_class}">', 1))
|
||||||
return value
|
return value
|
||||||
|
@ -318,5 +315,5 @@ def break_long_headers(header):
|
||||||
when possible (are comma separated)
|
when possible (are comma separated)
|
||||||
"""
|
"""
|
||||||
if len(header) > 160 and ',' in header:
|
if len(header) > 160 and ',' in header:
|
||||||
header = mark_safe('<br> ' + ', <br>'.join(header.split(',')))
|
header = mark_safe(f"<br> {header.replace(',', ', <br>')}")
|
||||||
return header
|
return header
|
||||||
|
|
|
@ -171,7 +171,7 @@ class APIRequestFactory(DjangoRequestFactory):
|
||||||
"Set TEST_REQUEST_RENDERER_CLASSES to enable "
|
"Set TEST_REQUEST_RENDERER_CLASSES to enable "
|
||||||
"extra request formats.".format(
|
"extra request formats.".format(
|
||||||
format,
|
format,
|
||||||
', '.join(["'" + fmt + "'" for fmt in self.renderer_classes])
|
', '.join([f"'{fmt}'" for fmt in self.renderer_classes])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ def _get_format_path_converter(suffix_kwarg, allowed):
|
||||||
else:
|
else:
|
||||||
allowed_cases = "|".join(allowed)
|
allowed_cases = "|".join(allowed)
|
||||||
allowed_pattern = f'(?:{allowed_cases})'
|
allowed_pattern = f'(?:{allowed_cases})'
|
||||||
suffix_pattern = r"\.%s/?" % allowed_pattern
|
suffix_pattern = fr"\.{allowed_pattern}/?"
|
||||||
else:
|
else:
|
||||||
suffix_pattern = r"\.[a-z0-9]+/?"
|
suffix_pattern = r"\.[a-z0-9]+/?"
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ def _get_format_path_converter(suffix_kwarg, allowed):
|
||||||
return value.strip('./')
|
return value.strip('./')
|
||||||
|
|
||||||
def to_url(self, value):
|
def to_url(self, value):
|
||||||
return '.' + value + '/'
|
return f'.{value}/'
|
||||||
|
|
||||||
converter_name = 'drf_format_suffix'
|
converter_name = 'drf_format_suffix'
|
||||||
if allowed:
|
if allowed:
|
||||||
|
@ -102,9 +102,9 @@ def format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None):
|
||||||
else:
|
else:
|
||||||
allowed_cases = "|".join(allowed)
|
allowed_cases = "|".join(allowed)
|
||||||
allowed_pattern = f'({allowed_cases})'
|
allowed_pattern = f'({allowed_cases})'
|
||||||
suffix_pattern = r'\.(?P<%s>%s)/?$' % (suffix_kwarg, allowed_pattern)
|
suffix_pattern = fr'\.(?P<{suffix_kwarg}>{allowed_pattern})/?$'
|
||||||
else:
|
else:
|
||||||
suffix_pattern = r'\.(?P<%s>[a-z0-9]+)/?$' % suffix_kwarg
|
suffix_pattern = fr'\.(?P<{suffix_kwarg}>[a-z0-9]+)/?$'
|
||||||
|
|
||||||
converter_name, suffix_converter = _get_format_path_converter(suffix_kwarg, allowed)
|
converter_name, suffix_converter = _get_format_path_converter(suffix_kwarg, allowed)
|
||||||
register_converter(suffix_converter, converter_name)
|
register_converter(suffix_converter, converter_name)
|
||||||
|
|
|
@ -63,7 +63,7 @@ def markup_description(description):
|
||||||
description = apply_markdown(description)
|
description = apply_markdown(description)
|
||||||
else:
|
else:
|
||||||
description = escape(description).replace('\n', '<br />')
|
description = escape(description).replace('\n', '<br />')
|
||||||
description = '<p>' + description + '</p>'
|
description = f'<p>{description}</p>'
|
||||||
return mark_safe(description)
|
return mark_safe(description)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ def parse_html_dict(dictionary, prefix=''):
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
ret = MultiValueDict()
|
ret = MultiValueDict()
|
||||||
regex = re.compile(r'^%s\.(.+)$' % re.escape(prefix))
|
regex = re.compile(fr'^{re.escape(prefix)}\.(.+)$')
|
||||||
for field in dictionary:
|
for field in dictionary:
|
||||||
match = regex.match(field)
|
match = regex.match(field)
|
||||||
if not match:
|
if not match:
|
||||||
|
|
|
@ -77,7 +77,7 @@ def serializer_repr(serializer, indent, force_many=None):
|
||||||
fields = serializer.fields
|
fields = serializer.fields
|
||||||
|
|
||||||
for field_name, field in fields.items():
|
for field_name, field in fields.items():
|
||||||
ret += '\n' + indent_str + field_name + ' = '
|
ret += f'\n{indent_str}{field_name} = '
|
||||||
if hasattr(field, 'fields'):
|
if hasattr(field, 'fields'):
|
||||||
ret += serializer_repr(field, indent + 1)
|
ret += serializer_repr(field, indent + 1)
|
||||||
elif hasattr(field, 'child'):
|
elif hasattr(field, 'child'):
|
||||||
|
|
|
@ -135,7 +135,7 @@ class NamespaceVersioning(BaseVersioning):
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_versioned_viewname(self, viewname, request):
|
def get_versioned_viewname(self, viewname, request):
|
||||||
return request.version + ':' + viewname
|
return f"{request.version}:{viewname}"
|
||||||
|
|
||||||
|
|
||||||
class HostNameVersioning(BaseVersioning):
|
class HostNameVersioning(BaseVersioning):
|
||||||
|
|
|
@ -39,7 +39,7 @@ def get_view_name(view):
|
||||||
# Suffix may be set by some Views, such as a ViewSet.
|
# Suffix may be set by some Views, such as a ViewSet.
|
||||||
suffix = getattr(view, 'suffix', None)
|
suffix = getattr(view, 'suffix', None)
|
||||||
if suffix:
|
if suffix:
|
||||||
name += ' ' + suffix
|
name += f' {suffix}'
|
||||||
|
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
|
|
@ -162,7 +162,7 @@ class ViewSetMixin:
|
||||||
if self.request and self.request.resolver_match:
|
if self.request and self.request.resolver_match:
|
||||||
namespace = self.request.resolver_match.namespace
|
namespace = self.request.resolver_match.namespace
|
||||||
if namespace:
|
if namespace:
|
||||||
url_name = namespace + ':' + url_name
|
url_name = f"{namespace}:{url_name}"
|
||||||
kwargs.setdefault('request', self.request)
|
kwargs.setdefault('request', self.request)
|
||||||
|
|
||||||
return reverse(url_name, *args, **kwargs)
|
return reverse(url_name, *args, **kwargs)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user