From d3817ab549b2143f2c1ff0368756c04369e640b5 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 17 Aug 2022 10:09:12 +0200 Subject: [PATCH 7/8] manager: factorize role with OU display code (#67025) --- src/authentic2/manager/role_views.py | 6 ++---- src/authentic2/manager/utils.py | 7 +++++++ src/authentic2/manager/widgets.py | 5 +---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/authentic2/manager/role_views.py b/src/authentic2/manager/role_views.py index 8b1c63c5..d93bc5c7 100644 --- a/src/authentic2/manager/role_views.py +++ b/src/authentic2/manager/role_views.py @@ -43,7 +43,7 @@ from authentic2.utils.misc import redirect from . import forms, resources, tables, views from .journal_views import BaseJournalView -from .utils import get_ou_count, has_show_username, label_from_user +from .utils import has_show_username, label_from_role, label_from_user User = get_user_model() @@ -879,9 +879,7 @@ class UserOrRoleSelect2View(DetailView): def get_choice(self, obj): if isinstance(obj, Role): - text = str(obj) - if obj.ou and get_ou_count() > 1: - text = f'{obj.ou} - {obj}' + text = label_from_role(obj) key = 'role-%s' elif isinstance(obj, User): text = label_from_user(obj) diff --git a/src/authentic2/manager/utils.py b/src/authentic2/manager/utils.py index ff6fd8eb..31a7aa5e 100644 --- a/src/authentic2/manager/utils.py +++ b/src/authentic2/manager/utils.py @@ -46,6 +46,13 @@ def get_ou_count(): return OrganizationalUnit.objects.count() +def label_from_role(role): + label = str(role) + if role.ou and get_ou_count() > 1: + label = f'{role.ou} - {role}' + return label + + @GlobalCache(timeout=10) def has_show_username(): return not OrganizationalUnit.objects.filter(show_username=False).exists() diff --git a/src/authentic2/manager/widgets.py b/src/authentic2/manager/widgets.py index 0cf90e70..ce533a5b 100644 --- a/src/authentic2/manager/widgets.py +++ b/src/authentic2/manager/widgets.py @@ -130,10 +130,7 @@ class SearchRoleWidgetMixin(SplitTermMixin): ] def label_from_instance(self, obj): - label = str(obj) - if obj.ou and utils.get_ou_count() > 1: - label = f'{obj.ou} - {obj}' - return label + return utils.label_from_role(obj) class ChooseRoleWidget(SearchRoleWidgetMixin, SimpleModelSelect2Widget): -- 2.30.2