diff --git a/zerver/actions/user_groups.py b/zerver/actions/user_groups.py index 283fdf4308..8f59c0f724 100644 --- a/zerver/actions/user_groups.py +++ b/zerver/actions/user_groups.py @@ -7,7 +7,7 @@ from django.utils.timezone import now as timezone_now from django.utils.translation import gettext as _ from zerver.lib.exceptions import JsonableError -from zerver.lib.user_groups import access_user_group_by_id, create_user_group +from zerver.lib.user_groups import access_user_group_by_id from zerver.models import ( GroupGroupMembership, Realm, @@ -25,6 +25,25 @@ class MemberGroupUserDict(TypedDict): date_joined: datetime.datetime +@transaction.atomic +def create_user_group( + name: str, + members: List[UserProfile], + realm: Realm, + *, + acting_user: Optional[UserProfile], + description: str = "", + is_system_group: bool = False, +) -> UserGroup: + user_group = UserGroup.objects.create( + name=name, realm=realm, description=description, is_system_group=is_system_group + ) + UserGroupMembership.objects.bulk_create( + UserGroupMembership(user_profile=member, user_group=user_group) for member in members + ) + return user_group + + @transaction.atomic(savepoint=False) def update_users_in_full_members_system_group( realm: Realm, affected_user_ids: Sequence[int] = [], *, acting_user: Optional[UserProfile] diff --git a/zerver/lib/user_groups.py b/zerver/lib/user_groups.py index 1d2810a361..e347e36ffe 100644 --- a/zerver/lib/user_groups.py +++ b/zerver/lib/user_groups.py @@ -1,6 +1,5 @@ -from typing import Dict, Iterable, List, Optional, Sequence, TypedDict +from typing import Dict, Iterable, List, Sequence, TypedDict -from django.db import transaction from django.db.models import QuerySet from django.utils.translation import gettext as _ from django_cte import With @@ -126,25 +125,6 @@ def get_direct_user_groups(user_profile: UserProfile) -> List[UserGroup]: return list(user_profile.direct_groups.all()) -def create_user_group( - name: str, - members: List[UserProfile], - realm: Realm, - *, - acting_user: Optional[UserProfile], - description: str = "", - is_system_group: bool = False, -) -> UserGroup: - with transaction.atomic(): - user_group = UserGroup.objects.create( - name=name, realm=realm, description=description, is_system_group=is_system_group - ) - UserGroupMembership.objects.bulk_create( - UserGroupMembership(user_profile=member, user_group=user_group) for member in members - ) - return user_group - - def get_user_group_direct_member_ids( user_group: UserGroup, ) -> ValuesQuerySet[UserGroupMembership, int]: diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.py index d845edebeb..88da90b05c 100644 --- a/zerver/tests/test_email_notifications.py +++ b/zerver/tests/test_email_notifications.py @@ -18,6 +18,7 @@ from django.utils.timezone import now as timezone_now from django_auth_ldap.config import LDAPSearch from zerver.actions.create_user import do_create_user +from zerver.actions.user_groups import create_user_group from zerver.actions.user_settings import do_change_user_setting from zerver.actions.users import do_change_user_role from zerver.lib.email_notifications import ( @@ -31,7 +32,6 @@ from zerver.lib.email_notifications import ( ) from zerver.lib.send_email import FromAddress, deliver_scheduled_emails, send_custom_email from zerver.lib.test_classes import ZulipTestCase -from zerver.lib.user_groups import create_user_group from zerver.models import ScheduledEmail, UserMessage, UserProfile, get_realm, get_stream diff --git a/zerver/tests/test_event_queue.py b/zerver/tests/test_event_queue.py index 89b69a7909..7bdb710526 100644 --- a/zerver/tests/test_event_queue.py +++ b/zerver/tests/test_event_queue.py @@ -8,12 +8,12 @@ from django.http import HttpRequest, HttpResponse from zerver.actions.message_send import internal_send_private_message from zerver.actions.muted_users import do_mute_user from zerver.actions.streams import do_change_subscription_property +from zerver.actions.user_groups import create_user_group from zerver.actions.user_settings import do_change_user_setting from zerver.actions.user_topics import do_set_user_topic_visibility_policy from zerver.lib.cache import cache_delete, get_muting_users_cache_key from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_helpers import HostRequestMock, dummy_handler, mock_queue_publish -from zerver.lib.user_groups import create_user_group from zerver.models import Recipient, Subscription, UserProfile, UserTopic, get_stream from zerver.tornado.event_queue import ( ClientDescriptor, diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 26a44f28c3..9d71e7b4bd 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -94,6 +94,7 @@ from zerver.actions.user_groups import ( bulk_add_members_to_user_group, check_add_user_group, check_delete_user_group, + create_user_group, do_update_user_group_description, do_update_user_group_name, remove_members_from_user_group, @@ -200,7 +201,6 @@ from zerver.lib.test_helpers import ( ) from zerver.lib.topic import TOPIC_NAME from zerver.lib.types import ProfileDataElementUpdateDict -from zerver.lib.user_groups import create_user_group from zerver.models import ( Attachment, CustomProfileField, diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py index 23637eeab0..ef46fa09b6 100644 --- a/zerver/tests/test_markdown.py +++ b/zerver/tests/test_markdown.py @@ -16,6 +16,7 @@ from zerver.actions.alert_words import do_add_alert_words from zerver.actions.create_realm import do_create_realm from zerver.actions.realm_emoji import do_remove_realm_emoji from zerver.actions.realm_settings import do_set_realm_property +from zerver.actions.user_groups import create_user_group from zerver.actions.user_settings import do_change_user_setting from zerver.actions.users import change_user_is_active from zerver.lib.alert_words import get_alert_word_automaton @@ -52,7 +53,6 @@ from zerver.lib.mention import ( from zerver.lib.message import render_markdown from zerver.lib.test_classes import ZulipTestCase from zerver.lib.tex import render_tex -from zerver.lib.user_groups import create_user_group from zerver.models import ( Message, RealmEmoji, diff --git a/zerver/tests/test_notification_data.py b/zerver/tests/test_notification_data.py index f529ab0d01..2760256189 100644 --- a/zerver/tests/test_notification_data.py +++ b/zerver/tests/test_notification_data.py @@ -1,7 +1,7 @@ +from zerver.actions.user_groups import create_user_group from zerver.lib.mention import MentionBackend, MentionData from zerver.lib.notification_data import UserMessageNotificationsData, get_user_group_mentions_data from zerver.lib.test_classes import ZulipTestCase -from zerver.lib.user_groups import create_user_group class TestNotificationData(ZulipTestCase): diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index bf1e8cb1ec..3d4b781c56 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -25,6 +25,7 @@ from analytics.lib.counts import CountStat, LoggingCountStat from analytics.models import InstallationCount, RealmCount from zerver.actions.message_delete import do_delete_messages from zerver.actions.message_flags import do_mark_stream_messages_as_read, do_update_message_flags +from zerver.actions.user_groups import create_user_group from zerver.actions.user_settings import do_regenerate_api_key from zerver.lib.avatar import absolute_avatar_url from zerver.lib.exceptions import JsonableError @@ -60,7 +61,6 @@ from zerver.lib.soft_deactivation import do_soft_deactivate_users from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_helpers import mock_queue_publish from zerver.lib.timestamp import datetime_to_timestamp -from zerver.lib.user_groups import create_user_group from zerver.models import ( Message, NotificationTriggers, diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py index 853cf40ecb..7ae5e14805 100644 --- a/zerver/tests/test_subs.py +++ b/zerver/tests/test_subs.py @@ -33,7 +33,7 @@ from zerver.actions.streams import ( do_change_stream_post_policy, do_deactivate_stream, ) -from zerver.actions.user_groups import add_subgroups_to_user_group +from zerver.actions.user_groups import add_subgroups_to_user_group, create_user_group from zerver.actions.users import do_change_user_role, do_deactivate_user from zerver.lib.exceptions import JsonableError from zerver.lib.message import UnreadStreamInfo, aggregate_unread_data, get_raw_unread_data @@ -82,7 +82,6 @@ from zerver.lib.types import ( NeverSubscribedStreamDict, SubscriptionInfo, ) -from zerver.lib.user_groups import create_user_group from zerver.models import ( Attachment, DefaultStream, diff --git a/zerver/tests/test_user_groups.py b/zerver/tests/test_user_groups.py index ae72234d0b..9dc00448fe 100644 --- a/zerver/tests/test_user_groups.py +++ b/zerver/tests/test_user_groups.py @@ -6,12 +6,11 @@ import orjson from django.utils.timezone import now as timezone_now from zerver.actions.realm_settings import do_set_realm_property -from zerver.actions.user_groups import promote_new_full_members +from zerver.actions.user_groups import create_user_group, promote_new_full_members from zerver.lib.streams import ensure_stream from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_helpers import most_recent_usermessage from zerver.lib.user_groups import ( - create_user_group, get_direct_memberships_of_users, get_direct_user_groups, get_recursive_group_members, diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 6b8c1d9269..b45c5417a5 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -27,6 +27,7 @@ from zerver.actions.custom_profile_fields import ( from zerver.actions.message_send import build_message_send_dict, do_send_messages from zerver.actions.realm_emoji import check_add_realm_emoji from zerver.actions.streams import bulk_add_subscriptions +from zerver.actions.user_groups import create_user_group from zerver.actions.users import do_change_user_role from zerver.lib.bulk_create import bulk_create_streams from zerver.lib.generate_test_data import create_test_data, generate_topics @@ -36,7 +37,6 @@ from zerver.lib.server_initialization import create_internal_realm, create_users from zerver.lib.storage import static_path from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS from zerver.lib.types import ProfileFieldData -from zerver.lib.user_groups import create_user_group from zerver.lib.users import add_service from zerver.lib.utils import generate_api_key from zerver.models import (