fix: Fix invalid ngettext usage in Throttled exception.

Format should be called after ngettext.
If you have overridden `extra_detail_singular` or `extra_detail_plural`,
you should now replace them with a single `def extra_detail` override.

Refs: https://docs.djangoproject.com/en/5.1/topics/i18n/translation/#pluralization
This commit is contained in:
Serhii Tereshchenko 2024-10-14 11:34:40 +03:00
parent 28d0261afc
commit 9ba943db95

View File

@ -226,8 +226,6 @@ class UnsupportedMediaType(APIException):
class Throttled(APIException): class Throttled(APIException):
status_code = status.HTTP_429_TOO_MANY_REQUESTS status_code = status.HTTP_429_TOO_MANY_REQUESTS
default_detail = _('Request was throttled.') default_detail = _('Request was throttled.')
extra_detail_singular = _('Expected available in {wait} second.')
extra_detail_plural = _('Expected available in {wait} seconds.')
default_code = 'throttled' default_code = 'throttled'
def __init__(self, wait=None, detail=None, code=None): def __init__(self, wait=None, detail=None, code=None):
@ -235,14 +233,17 @@ class Throttled(APIException):
detail = force_str(self.default_detail) detail = force_str(self.default_detail)
if wait is not None: if wait is not None:
wait = math.ceil(wait) wait = math.ceil(wait)
detail = ' '.join(( detail = " ".join((detail, force_str(self.extra_detail(wait))))
detail,
force_str(ngettext(self.extra_detail_singular.format(wait=wait),
self.extra_detail_plural.format(wait=wait),
wait))))
self.wait = wait self.wait = wait
super().__init__(detail, code) super().__init__(detail, code)
def extra_detail(self, wait):
return ngettext(
'Expected available in {wait} second.',
'Expected available in {wait} seconds.',
wait,
).format(wait=wait)
def server_error(request, *args, **kwargs): def server_error(request, *args, **kwargs):
""" """