From 8b30e03d4bc67ae750eba2176eeb78ee6c4b16cd Mon Sep 17 00:00:00 2001 From: Clara Dantas Date: Mon, 20 Jul 2020 21:25:28 -0300 Subject: [PATCH] models.py: Add get_role_name function in UserProfile class. This function returns the name of the user role that we can use to display in error report emails. --- corporate/views.py | 9 +-------- zerver/models.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/corporate/views.py b/corporate/views.py index 66c99890e9..42c7e6b53e 100644 --- a/corporate/views.py +++ b/corporate/views.py @@ -192,14 +192,7 @@ def sponsorship(request: HttpRequest, user: UserProfile, realm = user.realm requested_by = user.full_name - - role_id_to_name_map = { - UserProfile.ROLE_REALM_OWNER: "Realm owner", - UserProfile.ROLE_REALM_ADMINISTRATOR: "Realm adminstrator", - UserProfile.ROLE_MEMBER: "Member", - UserProfile.ROLE_GUEST: "Guest" - } - user_role = role_id_to_name_map[user.role] + user_role = user.get_role_name() support_realm_uri = get_realm(settings.STAFF_SUBDOMAIN).uri support_url = urljoin(support_realm_uri, urlunsplit(("", "", reverse('analytics.views.support'), diff --git a/zerver/models.py b/zerver/models.py index bc72efbe8b..dbac96581c 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -1124,6 +1124,16 @@ class UserProfile(AbstractBaseUser, PermissionsMixin): presence_enabled=bool, ) + ROLE_ID_TO_NAME_MAP = { + ROLE_REALM_OWNER: "Realm owner", + ROLE_REALM_ADMINISTRATOR: "Realm adminstrator", + ROLE_MEMBER: "Member", + ROLE_GUEST: "Guest" + } + + def get_role_name(self) -> str: + return self.ROLE_ID_TO_NAME_MAP[self.role] + @property def profile_data(self) -> ProfileData: values = CustomProfileFieldValue.objects.filter(user_profile=self)