mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
string_validation: Factor out stream name validation.
Co-authored-by: Shlok Patel <shlokcpatel2001@gmail.com>
This commit is contained in:
@@ -148,7 +148,6 @@ from zerver.lib.streams import (
|
|||||||
access_stream_for_send_message,
|
access_stream_for_send_message,
|
||||||
can_access_stream_user_ids,
|
can_access_stream_user_ids,
|
||||||
check_stream_access_based_on_stream_post_policy,
|
check_stream_access_based_on_stream_post_policy,
|
||||||
check_stream_name,
|
|
||||||
create_stream_if_needed,
|
create_stream_if_needed,
|
||||||
get_default_value_for_history_public_to_subscribers,
|
get_default_value_for_history_public_to_subscribers,
|
||||||
get_web_public_streams_queryset,
|
get_web_public_streams_queryset,
|
||||||
@@ -156,6 +155,7 @@ from zerver.lib.streams import (
|
|||||||
send_stream_creation_event,
|
send_stream_creation_event,
|
||||||
subscribed_to_stream,
|
subscribed_to_stream,
|
||||||
)
|
)
|
||||||
|
from zerver.lib.string_validation import check_stream_name
|
||||||
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
|
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
|
||||||
from zerver.lib.timezone import canonicalize_timezone
|
from zerver.lib.timezone import canonicalize_timezone
|
||||||
from zerver.lib.topic import (
|
from zerver.lib.topic import (
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ from zerver.lib.exceptions import (
|
|||||||
)
|
)
|
||||||
from zerver.lib.markdown import markdown_convert
|
from zerver.lib.markdown import markdown_convert
|
||||||
from zerver.lib.stream_subscription import get_active_subscriptions_for_stream_id
|
from zerver.lib.stream_subscription import get_active_subscriptions_for_stream_id
|
||||||
|
from zerver.lib.string_validation import check_stream_name
|
||||||
from zerver.models import (
|
from zerver.models import (
|
||||||
DefaultStreamGroup,
|
DefaultStreamGroup,
|
||||||
Realm,
|
Realm,
|
||||||
@@ -175,20 +176,6 @@ def create_streams_if_needed(
|
|||||||
return added_streams, existing_streams
|
return added_streams, existing_streams
|
||||||
|
|
||||||
|
|
||||||
def check_stream_name(stream_name: str) -> None:
|
|
||||||
if stream_name.strip() == "":
|
|
||||||
raise JsonableError(_("Invalid stream name '{}'").format(stream_name))
|
|
||||||
if len(stream_name) > Stream.MAX_NAME_LENGTH:
|
|
||||||
raise JsonableError(
|
|
||||||
_("Stream name too long (limit: {} characters).").format(Stream.MAX_NAME_LENGTH)
|
|
||||||
)
|
|
||||||
for i in stream_name:
|
|
||||||
if ord(i) == 0:
|
|
||||||
raise JsonableError(
|
|
||||||
_("Stream name '{}' contains NULL (0x00) characters.").format(stream_name)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def subscribed_to_stream(user_profile: UserProfile, stream_id: int) -> bool:
|
def subscribed_to_stream(user_profile: UserProfile, stream_id: int) -> bool:
|
||||||
return Subscription.objects.filter(
|
return Subscription.objects.filter(
|
||||||
user_profile=user_profile,
|
user_profile=user_profile,
|
||||||
|
|||||||
20
zerver/lib/string_validation.py
Normal file
20
zerver/lib/string_validation.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
from zerver.lib.exceptions import JsonableError
|
||||||
|
from zerver.models import Stream
|
||||||
|
|
||||||
|
|
||||||
|
def check_stream_name(stream_name: str) -> None:
|
||||||
|
if stream_name.strip() == "":
|
||||||
|
raise JsonableError(_("Invalid stream name '{}'").format(stream_name))
|
||||||
|
if len(stream_name) > Stream.MAX_NAME_LENGTH:
|
||||||
|
raise JsonableError(
|
||||||
|
_("Stream name too long (limit: {} characters).").format(Stream.MAX_NAME_LENGTH)
|
||||||
|
)
|
||||||
|
for i in stream_name:
|
||||||
|
if ord(i) == 0:
|
||||||
|
raise JsonableError(
|
||||||
|
_("Stream name '{}' contains NULL (0x00) characters.").format(stream_name)
|
||||||
|
)
|
||||||
@@ -61,11 +61,11 @@ from zerver.lib.streams import (
|
|||||||
access_stream_by_name,
|
access_stream_by_name,
|
||||||
access_stream_for_delete_or_update,
|
access_stream_for_delete_or_update,
|
||||||
access_web_public_stream,
|
access_web_public_stream,
|
||||||
check_stream_name,
|
|
||||||
check_stream_name_available,
|
check_stream_name_available,
|
||||||
filter_stream_authorization,
|
filter_stream_authorization,
|
||||||
list_to_streams,
|
list_to_streams,
|
||||||
)
|
)
|
||||||
|
from zerver.lib.string_validation import check_stream_name
|
||||||
from zerver.lib.topic import (
|
from zerver.lib.topic import (
|
||||||
get_topic_history_for_public_stream,
|
get_topic_history_for_public_stream,
|
||||||
get_topic_history_for_stream,
|
get_topic_history_for_stream,
|
||||||
|
|||||||
Reference in New Issue
Block a user