mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits  will be squashed with this commit.
We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.
The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.
This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.
Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.
With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.
We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.
Fixes #23124.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
		
	
		
			
				
	
	
		
			283 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			283 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import datetime
 | 
						|
from dataclasses import dataclass
 | 
						|
from typing import Any, Callable, Dict, List, Optional, Tuple, TypedDict, TypeVar, Union
 | 
						|
 | 
						|
from django_stubs_ext import StrPromise
 | 
						|
from typing_extensions import NotRequired
 | 
						|
 | 
						|
# See zerver/lib/validator.py for more details of Validators,
 | 
						|
# including many examples
 | 
						|
ResultT = TypeVar("ResultT")
 | 
						|
Validator = Callable[[str, object], ResultT]
 | 
						|
ExtendedValidator = Callable[[str, str, object], str]
 | 
						|
RealmUserValidator = Callable[[int, object, bool], List[int]]
 | 
						|
 | 
						|
 | 
						|
ProfileDataElementValue = Union[str, List[int]]
 | 
						|
 | 
						|
 | 
						|
class ProfileDataElementBase(TypedDict, total=False):
 | 
						|
    id: int
 | 
						|
    name: str
 | 
						|
    type: int
 | 
						|
    hint: str
 | 
						|
    display_in_profile_summary: bool
 | 
						|
    field_data: str
 | 
						|
    order: int
 | 
						|
 | 
						|
 | 
						|
class ProfileDataElement(ProfileDataElementBase):
 | 
						|
    value: Optional[ProfileDataElementValue]
 | 
						|
    rendered_value: Optional[str]
 | 
						|
 | 
						|
 | 
						|
class ProfileDataElementUpdateDict(TypedDict):
 | 
						|
    id: int
 | 
						|
    value: ProfileDataElementValue
 | 
						|
 | 
						|
 | 
						|
ProfileData = List[ProfileDataElement]
 | 
						|
 | 
						|
FieldElement = Tuple[int, StrPromise, Validator[ProfileDataElementValue], Callable[[Any], Any], str]
 | 
						|
ExtendedFieldElement = Tuple[int, StrPromise, ExtendedValidator, Callable[[Any], Any], str]
 | 
						|
UserFieldElement = Tuple[int, StrPromise, RealmUserValidator, Callable[[Any], Any], str]
 | 
						|
 | 
						|
ProfileFieldData = Dict[str, Union[Dict[str, str], str]]
 | 
						|
 | 
						|
 | 
						|
class UserDisplayRecipient(TypedDict):
 | 
						|
    email: str
 | 
						|
    full_name: str
 | 
						|
    id: int
 | 
						|
    is_mirror_dummy: bool
 | 
						|
 | 
						|
 | 
						|
DisplayRecipientT = Union[str, List[UserDisplayRecipient]]
 | 
						|
 | 
						|
 | 
						|
class LinkifierDict(TypedDict):
 | 
						|
    pattern: str
 | 
						|
    url_template: str
 | 
						|
    id: int
 | 
						|
 | 
						|
 | 
						|
class UnspecifiedValue:
 | 
						|
    """In most API endpoints, we use a default value of `None"` to encode
 | 
						|
    parameters that the client did not pass, which is nicely Pythonic.
 | 
						|
 | 
						|
    However, that design does not work for those few endpoints where
 | 
						|
    we want to allow clients to pass an explicit `null` (which
 | 
						|
    JSON-decodes to `None`).
 | 
						|
 | 
						|
    We use this type as an explicit sentinel value for such endpoints.
 | 
						|
 | 
						|
    TODO: Can this be merged with the _NotSpecified class, which is
 | 
						|
    currently an internal implementation detail of the REQ class?
 | 
						|
    """
 | 
						|
 | 
						|
 | 
						|
class EditHistoryEvent(TypedDict, total=False):
 | 
						|
    """
 | 
						|
    Database format for edit history events.
 | 
						|
    """
 | 
						|
 | 
						|
    # user_id is null for precisely those edit history events
 | 
						|
    # predating March 2017, when we started tracking the person who
 | 
						|
    # made edits, which is still years after the introduction of topic
 | 
						|
    # editing support in Zulip.
 | 
						|
    user_id: Optional[int]
 | 
						|
    timestamp: int
 | 
						|
    prev_stream: int
 | 
						|
    stream: int
 | 
						|
    prev_topic: str
 | 
						|
    topic: str
 | 
						|
    prev_content: str
 | 
						|
    prev_rendered_content: Optional[str]
 | 
						|
    prev_rendered_content_version: Optional[int]
 | 
						|
 | 
						|
 | 
						|
class FormattedEditHistoryEvent(TypedDict, total=False):
 | 
						|
    """
 | 
						|
    Extended format used in the edit history endpoint.
 | 
						|
    """
 | 
						|
 | 
						|
    # See EditHistoryEvent for details on when this can be null.
 | 
						|
    user_id: Optional[int]
 | 
						|
    timestamp: int
 | 
						|
    prev_stream: int
 | 
						|
    stream: int
 | 
						|
    prev_topic: str
 | 
						|
    topic: str
 | 
						|
    prev_content: str
 | 
						|
    content: str
 | 
						|
    prev_rendered_content: Optional[str]
 | 
						|
    rendered_content: Optional[str]
 | 
						|
    content_html_diff: str
 | 
						|
 | 
						|
 | 
						|
class UserTopicDict(TypedDict, total=False):
 | 
						|
    """Dictionary containing fields fetched from the UserTopic model that
 | 
						|
    are needed to encode the UserTopic object for the API.
 | 
						|
    """
 | 
						|
 | 
						|
    stream_id: int
 | 
						|
    stream__name: str
 | 
						|
    topic_name: str
 | 
						|
    last_updated: int
 | 
						|
    visibility_policy: int
 | 
						|
 | 
						|
 | 
						|
# This next batch of types is for Stream/Subscription objects.
 | 
						|
class RawStreamDict(TypedDict):
 | 
						|
    """Dictionary containing fields fetched from the Stream model that
 | 
						|
    are needed to encode the stream for the API.
 | 
						|
    """
 | 
						|
 | 
						|
    can_remove_subscribers_group_id: int
 | 
						|
    date_created: datetime.datetime
 | 
						|
    description: str
 | 
						|
    email_token: str
 | 
						|
    first_message_id: Optional[int]
 | 
						|
    history_public_to_subscribers: bool
 | 
						|
    id: int
 | 
						|
    invite_only: bool
 | 
						|
    is_web_public: bool
 | 
						|
    message_retention_days: Optional[int]
 | 
						|
    name: str
 | 
						|
    rendered_description: str
 | 
						|
    stream_post_policy: int
 | 
						|
 | 
						|
 | 
						|
class RawSubscriptionDict(TypedDict):
 | 
						|
    """Dictionary containing fields fetched from the Subscription model
 | 
						|
    that are needed to encode the subscription for the API.
 | 
						|
    """
 | 
						|
 | 
						|
    active: bool
 | 
						|
    audible_notifications: Optional[bool]
 | 
						|
    color: str
 | 
						|
    desktop_notifications: Optional[bool]
 | 
						|
    email_notifications: Optional[bool]
 | 
						|
    is_muted: bool
 | 
						|
    pin_to_top: bool
 | 
						|
    push_notifications: Optional[bool]
 | 
						|
    recipient_id: int
 | 
						|
    wildcard_mentions_notify: Optional[bool]
 | 
						|
 | 
						|
 | 
						|
class SubscriptionStreamDict(TypedDict):
 | 
						|
    """Conceptually, the union of RawSubscriptionDict and RawStreamDict
 | 
						|
    (i.e. containing all the user's personal settings for the stream
 | 
						|
    as well as the stream's global settings), with a few additional
 | 
						|
    computed fields.
 | 
						|
    """
 | 
						|
 | 
						|
    audible_notifications: Optional[bool]
 | 
						|
    can_remove_subscribers_group_id: int
 | 
						|
    color: str
 | 
						|
    date_created: int
 | 
						|
    description: str
 | 
						|
    desktop_notifications: Optional[bool]
 | 
						|
    email_address: str
 | 
						|
    email_notifications: Optional[bool]
 | 
						|
    first_message_id: Optional[int]
 | 
						|
    history_public_to_subscribers: bool
 | 
						|
    in_home_view: bool
 | 
						|
    invite_only: bool
 | 
						|
    is_announcement_only: bool
 | 
						|
    is_muted: bool
 | 
						|
    is_web_public: bool
 | 
						|
    message_retention_days: Optional[int]
 | 
						|
    name: str
 | 
						|
    pin_to_top: bool
 | 
						|
    push_notifications: Optional[bool]
 | 
						|
    rendered_description: str
 | 
						|
    stream_id: int
 | 
						|
    stream_post_policy: int
 | 
						|
    stream_weekly_traffic: Optional[int]
 | 
						|
    subscribers: NotRequired[List[int]]
 | 
						|
    wildcard_mentions_notify: Optional[bool]
 | 
						|
 | 
						|
 | 
						|
class NeverSubscribedStreamDict(TypedDict):
 | 
						|
    can_remove_subscribers_group_id: int
 | 
						|
    date_created: int
 | 
						|
    description: str
 | 
						|
    first_message_id: Optional[int]
 | 
						|
    history_public_to_subscribers: bool
 | 
						|
    invite_only: bool
 | 
						|
    is_announcement_only: bool
 | 
						|
    is_web_public: bool
 | 
						|
    message_retention_days: Optional[int]
 | 
						|
    name: str
 | 
						|
    rendered_description: str
 | 
						|
    stream_id: int
 | 
						|
    stream_post_policy: int
 | 
						|
    stream_weekly_traffic: Optional[int]
 | 
						|
    subscribers: NotRequired[List[int]]
 | 
						|
 | 
						|
 | 
						|
class APIStreamDict(TypedDict):
 | 
						|
    """Stream information provided to Zulip clients as a dictionary via API.
 | 
						|
    It should contain all the fields specified in `zerver.models.Stream.API_FIELDS`
 | 
						|
    with few exceptions and possible additional fields.
 | 
						|
    """
 | 
						|
 | 
						|
    can_remove_subscribers_group_id: int
 | 
						|
    date_created: int
 | 
						|
    description: str
 | 
						|
    first_message_id: Optional[int]
 | 
						|
    history_public_to_subscribers: bool
 | 
						|
    invite_only: bool
 | 
						|
    is_web_public: bool
 | 
						|
    message_retention_days: Optional[int]
 | 
						|
    name: str
 | 
						|
    rendered_description: str
 | 
						|
    stream_id: int  # `stream_id`` represents `id` of the `Stream` object in `API_FIELDS`
 | 
						|
    stream_post_policy: int
 | 
						|
    # Computed fields not specified in `Stream.API_FIELDS`
 | 
						|
    is_announcement_only: bool
 | 
						|
    is_default: NotRequired[bool]
 | 
						|
 | 
						|
 | 
						|
class APISubscriptionDict(APIStreamDict):
 | 
						|
    """Similar to StreamClientDict, it should contain all the fields specified in
 | 
						|
    `zerver.models.Subscription.API_FIELDS` and several additional fields.
 | 
						|
    """
 | 
						|
 | 
						|
    audible_notifications: Optional[bool]
 | 
						|
    color: str
 | 
						|
    desktop_notifications: Optional[bool]
 | 
						|
    email_notifications: Optional[bool]
 | 
						|
    is_muted: bool
 | 
						|
    pin_to_top: bool
 | 
						|
    push_notifications: Optional[bool]
 | 
						|
    wildcard_mentions_notify: Optional[bool]
 | 
						|
    # Computed fields not specified in `Subscription.API_FIELDS`
 | 
						|
    email_address: str
 | 
						|
    in_home_view: bool
 | 
						|
    stream_weekly_traffic: Optional[int]
 | 
						|
    subscribers: List[int]
 | 
						|
 | 
						|
 | 
						|
@dataclass
 | 
						|
class SubscriptionInfo:
 | 
						|
    subscriptions: List[SubscriptionStreamDict]
 | 
						|
    unsubscribed: List[SubscriptionStreamDict]
 | 
						|
    never_subscribed: List[NeverSubscribedStreamDict]
 | 
						|
 | 
						|
 | 
						|
class RealmPlaygroundDict(TypedDict):
 | 
						|
    id: int
 | 
						|
    name: str
 | 
						|
    pygments_language: str
 | 
						|
    url_prefix: str
 | 
						|
 | 
						|
 | 
						|
@dataclass
 | 
						|
class GroupPermissionSetting:
 | 
						|
    require_system_group: bool
 | 
						|
    allow_internet_group: bool
 | 
						|
    allow_owners_group: bool
 | 
						|
    allow_nobody_group: bool
 |