Files
upstream-zulip/zerver/views/channel_folders.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

106 lines
3.6 KiB
Python

from dataclasses import asdict
from typing import Annotated
from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _
from pydantic import Json, StringConstraints
from zerver.actions.channel_folders import (
check_add_channel_folder,
do_archive_channel_folder,
do_change_channel_folder_description,
do_change_channel_folder_name,
do_unarchive_channel_folder,
try_reorder_realm_channel_folders,
)
from zerver.decorator import require_realm_admin
from zerver.lib.channel_folders import (
check_channel_folder_in_use,
check_channel_folder_name,
get_channel_folder_by_id,
get_channel_folders_in_realm,
)
from zerver.lib.exceptions import JsonableError
from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
from zerver.models.channel_folders import ChannelFolder
from zerver.models.users import UserProfile
@require_realm_admin
@typed_endpoint
def create_channel_folder(
request: HttpRequest,
user_profile: UserProfile,
*,
description: Annotated[str, StringConstraints(max_length=ChannelFolder.MAX_DESCRIPTION_LENGTH)],
name: Annotated[str, StringConstraints(max_length=ChannelFolder.MAX_NAME_LENGTH)],
) -> HttpResponse:
realm = user_profile.realm
check_channel_folder_name(name, realm)
channel_folder = check_add_channel_folder(realm, name, description, acting_user=user_profile)
return json_success(request, data={"channel_folder_id": channel_folder.id})
@typed_endpoint
def get_channel_folders(
request: HttpRequest,
user_profile: UserProfile,
*,
include_archived: Json[bool] = False,
) -> HttpResponse:
channel_folders = get_channel_folders_in_realm(user_profile.realm, include_archived)
return json_success(
request,
data={"channel_folders": [asdict(folder) for folder in channel_folders]},
)
@require_realm_admin
@typed_endpoint
def reorder_realm_channel_folders(
request: HttpRequest,
user_profile: UserProfile,
*,
order: Json[list[int]],
) -> HttpResponse:
try_reorder_realm_channel_folders(user_profile.realm, order)
return json_success(request)
@require_realm_admin
@typed_endpoint
def update_channel_folder(
request: HttpRequest,
user_profile: UserProfile,
*,
channel_folder_id: PathOnly[int],
description: Annotated[
str | None, StringConstraints(max_length=ChannelFolder.MAX_DESCRIPTION_LENGTH)
] = None,
is_archived: Json[bool] | None = None,
name: Annotated[str | None, StringConstraints(max_length=ChannelFolder.MAX_NAME_LENGTH)] = None,
) -> HttpResponse:
channel_folder = get_channel_folder_by_id(channel_folder_id, user_profile.realm)
if name is not None and channel_folder.name != name:
check_channel_folder_name(name, user_profile.realm)
do_change_channel_folder_name(channel_folder, name, acting_user=user_profile)
if description is not None and channel_folder.description != description:
do_change_channel_folder_description(channel_folder, description, acting_user=user_profile)
if is_archived is not None and channel_folder.is_archived != is_archived:
if is_archived:
if check_channel_folder_in_use(channel_folder):
raise JsonableError(
_("You need to remove all the channels from this folder to archive it.")
)
do_archive_channel_folder(channel_folder, acting_user=user_profile)
else:
do_unarchive_channel_folder(channel_folder, acting_user=user_profile)
return json_success(request)