channel-folders: Add PATCH method to reorder channel folders.

The test cases are copied from ReorderCustomProfileFieldTest since we
are imitating the reordering mechanism from custom profile fields to
channel folders.
This commit is contained in:
Shubham Padia
2025-08-06 08:04:39 +00:00
committed by Tim Abbott
parent 40132e200b
commit 22b231ab6f
8 changed files with 174 additions and 2 deletions

View File

@@ -1,7 +1,11 @@
from collections.abc import Iterable
from django.db import transaction
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from zerver.lib.channel_folders import get_channel_folder_dict, render_channel_folder_description
from zerver.lib.exceptions import JsonableError
from zerver.models import ChannelFolder, Realm, RealmAuditLog, UserProfile
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.users import active_user_ids
@@ -22,6 +26,8 @@ def check_add_channel_folder(
rendered_description=rendered_description,
creator_id=acting_user.id,
)
channel_folder.order = channel_folder.id
channel_folder.save(update_fields=["order"])
creation_time = timezone_now()
RealmAuditLog.objects.create(
@@ -42,6 +48,18 @@ def check_add_channel_folder(
return channel_folder
@transaction.atomic(durable=True)
def try_reorder_realm_channel_folders(realm: Realm, order: Iterable[int]) -> None:
order_mapping = {_[1]: _[0] for _ in enumerate(order)}
channel_folders = ChannelFolder.objects.filter(realm=realm)
for channel_folder in channel_folders:
if channel_folder.id not in order_mapping:
raise JsonableError(_("Invalid order mapping."))
for channel_folder in channel_folders:
channel_folder.order = order_mapping[channel_folder.id]
channel_folder.save(update_fields=["order"])
def do_send_channel_folder_update_event(
channel_folder: ChannelFolder, data: dict[str, str | bool]
) -> None: