Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
user_counts: Maintain count of workplace and non-workplace users.
Sahil Batra032d9e7eb489
diff --git a/zerver/lib/user_counts.py b/zerver/lib/user_counts.pyindex 7405da677ad..0615d82b01e 100644--- a/zerver/lib/user_counts.py+++ b/zerver/lib/user_counts.py@@ -2,13 +2,46 @@ from typing import Anyfrom django.db.models import Count+from zerver.lib.user_groups import get_recursive_group_membersfrom zerver.models import Realm, RealmAuditLog, UserProfile+from zerver.models.groups import SystemGroups, get_realm_system_groups_name_dictdef realm_user_count(realm: Realm) -> int:return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False).count()+def get_role_based_system_group_member_count(+ human_counts: dict[str, int],+ system_group_name: str,+) -> int:+ if system_group_name == SystemGroups.NOBODY:+ return 0++ if system_group_name == SystemGroups.OWNERS:+ roles = [UserProfile.ROLE_REALM_OWNER]+ elif system_group_name == SystemGroups.ADMINISTRATORS:+ roles = [UserProfile.ROLE_REALM_OWNER, UserProfile.ROLE_REALM_ADMINISTRATOR]+ elif system_group_name == SystemGroups.MODERATORS:+ roles = [+ UserProfile.ROLE_REALM_OWNER,+ UserProfile.ROLE_REALM_ADMINISTRATOR,+ UserProfile.ROLE_MODERATOR,+ ]+ elif system_group_name == SystemGroups.MEMBERS:+ roles = [+ UserProfile.ROLE_REALM_OWNER,+ UserProfile.ROLE_REALM_ADMINISTRATOR,+ UserProfile.ROLE_MODERATOR,+ UserProfile.ROLE_MEMBER,+ ]+ else:+ assert system_group_name in [SystemGroups.EVERYONE, SystemGroups.EVERYONE_ON_INTERNET]+ roles = UserProfile.ROLE_TYPES++ return sum(human_counts[str(role)] for role in roles)++def realm_user_count_by_role(realm: Realm) -> dict[str, Any]:human_counts = {str(UserProfile.ROLE_REALM_ADMINISTRATOR): 0,@@ -16,6 +49,8 @@ def realm_user_count_by_role(realm: Realm) -> dict[str, Any]:str(UserProfile.ROLE_MODERATOR): 0,str(UserProfile.ROLE_MEMBER): 0,str(UserProfile.ROLE_GUEST): 0,+ "workplace_users": 0,+ "non_workplace_users": 0,}for value_dict in (UserProfile.objects.filter(realm=realm, is_bot=False, is_active=True)@@ -24,6 +59,30 @@ def realm_user_count_by_role(realm: Realm) -> dict[str, Any]:):human_counts[str(value_dict["role"])] = value_dict["role__count"]bot_count = UserProfile.objects.filter(realm=realm, is_bot=True, is_active=True).count()++ total_human_count = sum(human_counts[str(role)] for role in UserProfile.ROLE_TYPES)++ system_groups_dict = get_realm_system_groups_name_dict(realm.id)+ if (+ realm.workplace_users_group_id in system_groups_dict+ and system_groups_dict[realm.workplace_users_group_id] != SystemGroups.FULL_MEMBERS+ ):+ # If workplace_users_group is set to a role based group except full members+ # group we can calculate the number of workplace users from already computed+ # role counts.+ system_group_name = system_groups_dict[realm.workplace_users_group_id]+ human_counts["workplace_users"] = get_role_based_system_group_member_count(+ human_counts, system_group_name+ )+ else:+ human_counts["workplace_users"] = (+ get_recursive_group_members(realm.workplace_users_group_id)+ .filter(is_bot=False, is_active=True)+ .count()+ )++ human_counts["non_workplace_users"] = total_human_count - human_counts["workplace_users"]+return {RealmAuditLog.ROLE_COUNT_HUMANS: human_counts,RealmAuditLog.ROLE_COUNT_BOTS: bot_count,diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.pyindex f16ec281bc1..3dcf883cfa8 100644--- a/zerver/tests/test_audit_log.py+++ b/zerver/tests/test_audit_log.py@@ -123,6 +123,12 @@ class TestRealmAuditLog(ZulipTestCase):# str(key) since json keys are always strings, and ujson.dumps will have converted# the UserProfile.role values into stringsself.assertTrue(isinstance(role_counts[RealmAuditLog.ROLE_COUNT_HUMANS][str(key)], int))+ self.assertTrue(+ isinstance(role_counts[RealmAuditLog.ROLE_COUNT_HUMANS]["workplace_users"], int)+ )+ self.assertTrue(+ isinstance(role_counts[RealmAuditLog.ROLE_COUNT_HUMANS]["non_workplace_users"], int)+ )self.assertTrue(isinstance(role_counts[RealmAuditLog.ROLE_COUNT_BOTS], int))def test_user_activation(self) -> None:diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex 23175110830..c4e2703132d 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -1071,7 +1071,7 @@ class LoginTest(ZulipTestCase):# the alert words for a realm, etc.with (self.assert_database_query_count(105),- self.assert_memcached_count(18),+ self.assert_memcached_count(19),self.captureOnCommitCallbacks(execute=True),):self.register(self.nonreg_email("test"), "test")diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 82969dc51fa..daf0c8dd33c 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -1168,7 +1168,7 @@ class QueryCountTest(ZulipTestCase):with (self.assert_database_query_count(104),- self.assert_memcached_count(23),+ self.assert_memcached_count(24),self.capture_send_event_calls(expected_num_events=11) as events,):fred = do_create_user(