models: Make get_admin_users_and_bots optionally not return owners.

This commit is contained in:
Vishnu KS 2021-03-20 00:36:41 +05:30 committed by Tim Abbott
parent 1a126f8bf2
commit 7a351edb60
2 changed files with 11 additions and 2 deletions

View File

@ -553,16 +553,23 @@ class Realm(models.Model):
def get_active_emoji(self) -> Dict[str, Dict[str, Iterable[str]]]: def get_active_emoji(self) -> Dict[str, Dict[str, Iterable[str]]]:
return get_active_realm_emoji_uncached(self) return get_active_realm_emoji_uncached(self)
def get_admin_users_and_bots(self) -> Sequence["UserProfile"]: def get_admin_users_and_bots(
self, include_realm_owners: bool = True
) -> Sequence["UserProfile"]:
"""Use this in contexts where we want administrative users as well as """Use this in contexts where we want administrative users as well as
bots with administrator privileges, like send_event calls for bots with administrator privileges, like send_event calls for
notifications to all administrator users. notifications to all administrator users.
""" """
if include_realm_owners:
roles = [UserProfile.ROLE_REALM_ADMINISTRATOR, UserProfile.ROLE_REALM_OWNER]
else:
roles = [UserProfile.ROLE_REALM_ADMINISTRATOR]
# TODO: Change return type to QuerySet[UserProfile] # TODO: Change return type to QuerySet[UserProfile]
return UserProfile.objects.filter( return UserProfile.objects.filter(
realm=self, realm=self,
is_active=True, is_active=True,
role__in=[UserProfile.ROLE_REALM_ADMINISTRATOR, UserProfile.ROLE_REALM_OWNER], role__in=roles,
) )
def get_human_admin_users(self) -> QuerySet: def get_human_admin_users(self) -> QuerySet:

View File

@ -128,6 +128,8 @@ class PermissionTest(ZulipTestCase):
self.assertTrue(user_profile in admin_users) self.assertTrue(user_profile in admin_users)
admin_users = user_profile.realm.get_admin_users_and_bots() admin_users = user_profile.realm.get_admin_users_and_bots()
self.assertTrue(user_profile in admin_users) self.assertTrue(user_profile in admin_users)
admin_users = user_profile.realm.get_admin_users_and_bots(include_realm_owners=False)
self.assertFalse(user_profile in admin_users)
def test_updating_non_existent_user(self) -> None: def test_updating_non_existent_user(self) -> None:
self.login("hamlet") self.login("hamlet")