Coverage for app/venv/lib/python3.14/site-packages/weblate/api/throttling.py: 93%

23 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 07:15 +0000

1# Copyright © Michal Čihař <michal@weblate.org> 

2# 

3# SPDX-License-Identifier: GPL-3.0-or-later 

4 

5from __future__ import annotations 

6 

7from functools import wraps 

8from typing import TYPE_CHECKING 

9 

10from rest_framework.throttling import AnonRateThrottle as DRFAnonRateThrottle 

11from rest_framework.throttling import UserRateThrottle as DRFUserRateThrottle 

12 

13if TYPE_CHECKING: 13 ↛ 14line 13 didn't jump to line 14 because the condition on line 13 was never true

14 from rest_framework.request import Request 

15 

16 

17def patch_throttle_request(func): 

18 """Store throttling state in request to be picked up by ThrottlingMiddleware.""" 

19 

20 @wraps(func) 

21 def patched(self, request: Request, view): 

22 result = func(self, request, view) 

23 if hasattr(self, "history"): 

24 request.META["throttling_state"] = self 

25 return result 

26 

27 return patched 

28 

29 

30class AnonRateThrottle(DRFAnonRateThrottle): 

31 @patch_throttle_request 

32 def allow_request(self, request: Request, view): 

33 return super().allow_request(request, view) 

34 

35 

36class UserRateThrottle(DRFUserRateThrottle): 

37 @patch_throttle_request 

38 def allow_request(self, request: Request, view): 

39 return super().allow_request(request, view)