request: Refactor to record rate limit data using ZulipRequestNotes.

We will no longer use the HttpRequest to store the rate limit data.
Using ZulipRequestNotes, we can access rate_limit and ratelimits_applied
with type hints support. We also save the process of initializing
ratelimits_applied by giving it a default value.
This commit is contained in:
PIG208
2021-07-09 19:15:19 +08:00
committed by Tim Abbott
parent da6e5ddcae
commit 3f9a5e1e17
4 changed files with 15 additions and 14 deletions

View File

@@ -70,7 +70,7 @@ from zerver.lib.email_validation import email_allowed_for_realm, validate_email_
from zerver.lib.mobile_auth_otp import is_valid_otp
from zerver.lib.rate_limiter import RateLimitedObject
from zerver.lib.redis_utils import get_dict_from_redis, get_redis_client, put_dict_in_redis
from zerver.lib.request import JsonableError
from zerver.lib.request import JsonableError, get_request_notes
from zerver.lib.subdomains import get_subdomain
from zerver.lib.users import check_full_name, validate_user_custom_profile_field
from zerver.models import (
@@ -259,12 +259,11 @@ def rate_limit_authentication_by_username(request: HttpRequest, username: str) -
def auth_rate_limiting_already_applied(request: HttpRequest) -> bool:
if not hasattr(request, "_ratelimits_applied"):
return False
request_notes = get_request_notes(request)
return any(
isinstance(r.entity, RateLimitedAuthenticationByUsername)
for r in request._ratelimits_applied
for r in request_notes.ratelimits_applied
)