2019-06-06 22:22:21 +02:00
|
|
|
from collections import defaultdict
|
2024-09-03 18:07:09 +02:00
|
|
|
from collections.abc import MutableMapping
|
2021-07-08 16:10:54 +02:00
|
|
|
from dataclasses import dataclass, field
|
2024-09-03 18:07:09 +02:00
|
|
|
from typing import Any, Optional
|
2016-05-29 16:52:55 +02:00
|
|
|
|
2022-06-12 21:33:20 +02:00
|
|
|
from django.conf import settings
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2024-09-03 18:07:09 +02:00
|
|
|
from typing_extensions import override
|
2016-05-25 15:02:02 +02:00
|
|
|
|
2022-12-04 10:36:04 +01:00
|
|
|
from zerver.lib import rate_limiter
|
2024-09-03 18:07:09 +02:00
|
|
|
from zerver.lib.exceptions import ErrorCode, JsonableError
|
2021-08-21 19:24:20 +02:00
|
|
|
from zerver.lib.notes import BaseNotes
|
2021-07-08 16:10:54 +02:00
|
|
|
from zerver.models import Client, Realm
|
|
|
|
|
2022-06-12 21:33:20 +02:00
|
|
|
if settings.ZILENCER_ENABLED:
|
|
|
|
from zilencer.models import RemoteZulipServer
|
|
|
|
|
2021-07-08 16:10:54 +02:00
|
|
|
|
|
|
|
@dataclass
|
2021-08-21 19:24:20 +02:00
|
|
|
class RequestNotes(BaseNotes[HttpRequest, "RequestNotes"]):
|
2021-07-08 16:10:54 +02:00
|
|
|
"""This class contains extra metadata that Zulip associated with a
|
2021-08-21 19:24:20 +02:00
|
|
|
Django HttpRequest object. See the docstring for BaseNotes for
|
|
|
|
details on how it works.
|
2021-07-08 16:10:54 +02:00
|
|
|
|
|
|
|
Note that most Optional fields will be definitely not None once
|
2022-02-08 00:13:33 +01:00
|
|
|
middleware has run. In the future, we may want to express that in
|
2021-08-21 19:24:20 +02:00
|
|
|
the types by having different types EarlyRequestNotes and
|
|
|
|
post-middleware RequestNotes types, but for now we have a lot
|
2021-07-08 16:10:54 +02:00
|
|
|
of `assert request_notes.foo is not None` when accessing them.
|
|
|
|
"""
|
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
client: Client | None = None
|
|
|
|
client_name: str | None = None
|
|
|
|
client_version: str | None = None
|
|
|
|
log_data: MutableMapping[str, Any] | None = None
|
|
|
|
rate_limit: str | None = None
|
|
|
|
requester_for_logs: str | None = None
|
2021-07-08 16:10:54 +02:00
|
|
|
# We use realm_cached to indicate whether the realm is cached or not.
|
|
|
|
# Because the default value of realm is None, which can indicate "unset"
|
|
|
|
# and "nonexistence" at the same time.
|
2024-07-12 02:30:23 +02:00
|
|
|
realm: Realm | None = None
|
2021-07-08 16:10:54 +02:00
|
|
|
has_fetched_realm: bool = False
|
2024-07-12 02:30:23 +02:00
|
|
|
set_language: str | None = None
|
2024-07-12 02:30:17 +02:00
|
|
|
ratelimits_applied: list[rate_limiter.RateLimitResult] = field(default_factory=list)
|
2024-07-12 02:30:23 +02:00
|
|
|
query: str | None = None
|
|
|
|
error_format: str | None = None
|
|
|
|
saved_response: HttpResponse | None = None
|
|
|
|
tornado_handler_id: int | None = None
|
2024-07-12 02:30:17 +02:00
|
|
|
processed_parameters: set[str] = field(default_factory=set)
|
2022-06-12 21:33:20 +02:00
|
|
|
remote_server: Optional["RemoteZulipServer"] = None
|
2022-08-25 18:22:07 +02:00
|
|
|
is_webhook_view: bool = False
|
2021-07-08 16:10:54 +02:00
|
|
|
|
2021-08-21 19:24:20 +02:00
|
|
|
@classmethod
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-08-21 19:24:20 +02:00
|
|
|
def init_notes(cls) -> "RequestNotes":
|
|
|
|
return RequestNotes()
|
2016-05-29 16:52:55 +02:00
|
|
|
|
2018-11-09 19:45:25 +01:00
|
|
|
|
2023-01-02 20:50:23 +01:00
|
|
|
class RequestConfusingParamsError(JsonableError):
|
2018-11-09 19:45:25 +01:00
|
|
|
code = ErrorCode.REQUEST_CONFUSING_VAR
|
2021-02-12 08:20:45 +01:00
|
|
|
data_fields = ["var_name1", "var_name2"]
|
2018-11-09 19:45:25 +01:00
|
|
|
|
|
|
|
def __init__(self, var_name1: str, var_name2: str) -> None:
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
self.var_name1: str = var_name1
|
|
|
|
self.var_name2: str = var_name2
|
2018-11-09 19:45:25 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2018-11-09 19:45:25 +01:00
|
|
|
def msg_format() -> str:
|
|
|
|
return _("Can't decide between '{var_name1}' and '{var_name2}' arguments")
|
2017-11-05 11:15:10 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-05-29 16:52:55 +02:00
|
|
|
class RequestVariableMissingError(JsonableError):
|
2017-07-21 02:17:28 +02:00
|
|
|
code = ErrorCode.REQUEST_VARIABLE_MISSING
|
2021-02-12 08:20:45 +01:00
|
|
|
data_fields = ["var_name"]
|
2017-07-21 02:17:28 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def __init__(self, var_name: str) -> None:
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
self.var_name: str = var_name
|
2016-05-29 16:52:55 +02:00
|
|
|
|
2017-07-21 02:17:28 +02:00
|
|
|
@staticmethod
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-11-05 11:15:10 +01:00
|
|
|
def msg_format() -> str:
|
2017-07-21 02:17:28 +02:00
|
|
|
return _("Missing '{var_name}' argument")
|
2016-05-29 16:52:55 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-05-29 16:52:55 +02:00
|
|
|
class RequestVariableConversionError(JsonableError):
|
2017-07-21 02:17:28 +02:00
|
|
|
code = ErrorCode.REQUEST_VARIABLE_INVALID
|
2021-02-12 08:20:45 +01:00
|
|
|
data_fields = ["var_name", "bad_value"]
|
2017-07-21 02:17:28 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def __init__(self, var_name: str, bad_value: Any) -> None:
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
self.var_name: str = var_name
|
2016-05-29 16:52:55 +02:00
|
|
|
self.bad_value = bad_value
|
|
|
|
|
2017-07-21 02:17:28 +02:00
|
|
|
@staticmethod
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-11-05 11:15:10 +01:00
|
|
|
def msg_format() -> str:
|
2017-07-21 02:17:28 +02:00
|
|
|
return _("Bad value for '{var_name}': {bad_value}")
|
2016-05-29 16:52:55 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
arguments_map: dict[str, list[str]] = defaultdict(list)
|