user_groups: Move create_user_group to zerver.actions.user_groups.

Since this function creates a new user group into the database,
it is more appropriate to have it not as a generic "lib" function
but as an "action".

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-12-11 22:17:49 -05:00 committed by Tim Abbott
parent d2db71fdf4
commit 0f5d6432a4
11 changed files with 30 additions and 33 deletions

View File

@ -7,7 +7,7 @@ from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from zerver.lib.exceptions import JsonableError 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 ( from zerver.models import (
GroupGroupMembership, GroupGroupMembership,
Realm, Realm,
@ -25,6 +25,25 @@ class MemberGroupUserDict(TypedDict):
date_joined: datetime.datetime 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) @transaction.atomic(savepoint=False)
def update_users_in_full_members_system_group( def update_users_in_full_members_system_group(
realm: Realm, affected_user_ids: Sequence[int] = [], *, acting_user: Optional[UserProfile] realm: Realm, affected_user_ids: Sequence[int] = [], *, acting_user: Optional[UserProfile]

View File

@ -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.db.models import QuerySet
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django_cte import With 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()) 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( def get_user_group_direct_member_ids(
user_group: UserGroup, user_group: UserGroup,
) -> ValuesQuerySet[UserGroupMembership, int]: ) -> ValuesQuerySet[UserGroupMembership, int]:

View File

@ -18,6 +18,7 @@ from django.utils.timezone import now as timezone_now
from django_auth_ldap.config import LDAPSearch from django_auth_ldap.config import LDAPSearch
from zerver.actions.create_user import do_create_user 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.user_settings import do_change_user_setting
from zerver.actions.users import do_change_user_role from zerver.actions.users import do_change_user_role
from zerver.lib.email_notifications import ( 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.send_email import FromAddress, deliver_scheduled_emails, send_custom_email
from zerver.lib.test_classes import ZulipTestCase 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 from zerver.models import ScheduledEmail, UserMessage, UserProfile, get_realm, get_stream

View File

@ -8,12 +8,12 @@ from django.http import HttpRequest, HttpResponse
from zerver.actions.message_send import internal_send_private_message from zerver.actions.message_send import internal_send_private_message
from zerver.actions.muted_users import do_mute_user from zerver.actions.muted_users import do_mute_user
from zerver.actions.streams import do_change_subscription_property 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_settings import do_change_user_setting
from zerver.actions.user_topics import do_set_user_topic_visibility_policy 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.cache import cache_delete, get_muting_users_cache_key
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import HostRequestMock, dummy_handler, mock_queue_publish 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.models import Recipient, Subscription, UserProfile, UserTopic, get_stream
from zerver.tornado.event_queue import ( from zerver.tornado.event_queue import (
ClientDescriptor, ClientDescriptor,

View File

@ -94,6 +94,7 @@ from zerver.actions.user_groups import (
bulk_add_members_to_user_group, bulk_add_members_to_user_group,
check_add_user_group, check_add_user_group,
check_delete_user_group, check_delete_user_group,
create_user_group,
do_update_user_group_description, do_update_user_group_description,
do_update_user_group_name, do_update_user_group_name,
remove_members_from_user_group, 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.topic import TOPIC_NAME
from zerver.lib.types import ProfileDataElementUpdateDict from zerver.lib.types import ProfileDataElementUpdateDict
from zerver.lib.user_groups import create_user_group
from zerver.models import ( from zerver.models import (
Attachment, Attachment,
CustomProfileField, CustomProfileField,

View File

@ -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.create_realm import do_create_realm
from zerver.actions.realm_emoji import do_remove_realm_emoji from zerver.actions.realm_emoji import do_remove_realm_emoji
from zerver.actions.realm_settings import do_set_realm_property 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.user_settings import do_change_user_setting
from zerver.actions.users import change_user_is_active from zerver.actions.users import change_user_is_active
from zerver.lib.alert_words import get_alert_word_automaton 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.message import render_markdown
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.tex import render_tex from zerver.lib.tex import render_tex
from zerver.lib.user_groups import create_user_group
from zerver.models import ( from zerver.models import (
Message, Message,
RealmEmoji, RealmEmoji,

View File

@ -1,7 +1,7 @@
from zerver.actions.user_groups import create_user_group
from zerver.lib.mention import MentionBackend, MentionData from zerver.lib.mention import MentionBackend, MentionData
from zerver.lib.notification_data import UserMessageNotificationsData, get_user_group_mentions_data from zerver.lib.notification_data import UserMessageNotificationsData, get_user_group_mentions_data
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.user_groups import create_user_group
class TestNotificationData(ZulipTestCase): class TestNotificationData(ZulipTestCase):

View File

@ -25,6 +25,7 @@ from analytics.lib.counts import CountStat, LoggingCountStat
from analytics.models import InstallationCount, RealmCount from analytics.models import InstallationCount, RealmCount
from zerver.actions.message_delete import do_delete_messages 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.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.actions.user_settings import do_regenerate_api_key
from zerver.lib.avatar import absolute_avatar_url from zerver.lib.avatar import absolute_avatar_url
from zerver.lib.exceptions import JsonableError 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_classes import ZulipTestCase
from zerver.lib.test_helpers import mock_queue_publish from zerver.lib.test_helpers import mock_queue_publish
from zerver.lib.timestamp import datetime_to_timestamp from zerver.lib.timestamp import datetime_to_timestamp
from zerver.lib.user_groups import create_user_group
from zerver.models import ( from zerver.models import (
Message, Message,
NotificationTriggers, NotificationTriggers,

View File

@ -33,7 +33,7 @@ from zerver.actions.streams import (
do_change_stream_post_policy, do_change_stream_post_policy,
do_deactivate_stream, 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.actions.users import do_change_user_role, do_deactivate_user
from zerver.lib.exceptions import JsonableError from zerver.lib.exceptions import JsonableError
from zerver.lib.message import UnreadStreamInfo, aggregate_unread_data, get_raw_unread_data from zerver.lib.message import UnreadStreamInfo, aggregate_unread_data, get_raw_unread_data
@ -82,7 +82,6 @@ from zerver.lib.types import (
NeverSubscribedStreamDict, NeverSubscribedStreamDict,
SubscriptionInfo, SubscriptionInfo,
) )
from zerver.lib.user_groups import create_user_group
from zerver.models import ( from zerver.models import (
Attachment, Attachment,
DefaultStream, DefaultStream,

View File

@ -6,12 +6,11 @@ import orjson
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from zerver.actions.realm_settings import do_set_realm_property 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.streams import ensure_stream
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import most_recent_usermessage from zerver.lib.test_helpers import most_recent_usermessage
from zerver.lib.user_groups import ( from zerver.lib.user_groups import (
create_user_group,
get_direct_memberships_of_users, get_direct_memberships_of_users,
get_direct_user_groups, get_direct_user_groups,
get_recursive_group_members, get_recursive_group_members,

View File

@ -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.message_send import build_message_send_dict, do_send_messages
from zerver.actions.realm_emoji import check_add_realm_emoji from zerver.actions.realm_emoji import check_add_realm_emoji
from zerver.actions.streams import bulk_add_subscriptions 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.actions.users import do_change_user_role
from zerver.lib.bulk_create import bulk_create_streams from zerver.lib.bulk_create import bulk_create_streams
from zerver.lib.generate_test_data import create_test_data, generate_topics 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.storage import static_path
from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS
from zerver.lib.types import ProfileFieldData 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.users import add_service
from zerver.lib.utils import generate_api_key from zerver.lib.utils import generate_api_key
from zerver.models import ( from zerver.models import (