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.

(Backported from master; fixes #17067, fixes #17102).
This commit is contained in:
Clara Dantas
2020-07-20 21:25:28 -03:00
committed by Tim Abbott
parent deec501da4
commit a58f541b63
2 changed files with 11 additions and 8 deletions

View File

@@ -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'),

View File

@@ -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)