From e6dd1bea63b4d267eb2370e14876b344a4c70d00 Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Mon, 3 Jun 2024 18:16:35 +0200 Subject: [PATCH 1/2] Remove add site/user from Role admin (#943) --- rdmo/accounts/admin.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rdmo/accounts/admin.py b/rdmo/accounts/admin.py index 1cfede8540..475e92be7b 100644 --- a/rdmo/accounts/admin.py +++ b/rdmo/accounts/admin.py @@ -30,6 +30,8 @@ class RoleAdmin(admin.ModelAdmin): list_display = ('user', 'email', 'members', 'managers', 'editors', 'reviewers') + readonly_fields = ('user', ) + def get_queryset(self, request): return Role.objects.prefetch_related( 'member', 'manager', 'editor', 'reviewer').annotate( @@ -37,6 +39,15 @@ def get_queryset(self, request): sites_count=Value(Site.objects.count()) ) + def get_form(self, request, obj=None, **kwargs): + form = super().get_form(request, obj, **kwargs) + form.base_fields['member'].widget.can_add_related = False + form.base_fields['manager'].widget.can_add_related = False + form.base_fields['editor'].widget.can_add_related = False + form.base_fields['reviewer'].widget.can_add_related = False + + return form + @staticmethod def render_all_sites_or_join(obj, field_name: str) -> str: if getattr(obj, f'{field_name}__count', 0) == obj.sites_count: From cc45bd7d3f9c65886a005175b5a538e414b006ff Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Tue, 4 Jun 2024 09:53:54 +0200 Subject: [PATCH 2/2] Add distinct to roles count and rename get_sites_for_role --- rdmo/accounts/admin.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/rdmo/accounts/admin.py b/rdmo/accounts/admin.py index 475e92be7b..8d65fb1f57 100644 --- a/rdmo/accounts/admin.py +++ b/rdmo/accounts/admin.py @@ -35,7 +35,10 @@ class RoleAdmin(admin.ModelAdmin): def get_queryset(self, request): return Role.objects.prefetch_related( 'member', 'manager', 'editor', 'reviewer').annotate( - Count('member'), Count('manager'), Count('editor'), Count('reviewer'), + Count('member', distinct=True), + Count('manager', distinct=True), + Count('editor', distinct=True), + Count('reviewer', distinct=True), sites_count=Value(Site.objects.count()) ) @@ -49,7 +52,7 @@ def get_form(self, request, obj=None, **kwargs): return form @staticmethod - def render_all_sites_or_join(obj, field_name: str) -> str: + def get_sites_for_role(obj, field_name: str) -> str: if getattr(obj, f'{field_name}__count', 0) == obj.sites_count: return 'all Sites' return ', '.join([site.domain for site in getattr(obj, field_name).all()]) @@ -58,13 +61,13 @@ def email(self, obj): return obj.user.email def members(self, obj): - return self.render_all_sites_or_join(obj, 'member') + return self.get_sites_for_role(obj, 'member') def managers(self, obj): - return self.render_all_sites_or_join(obj, 'manager') + return self.get_sites_for_role(obj, 'manager') def editors(self, obj): - return self.render_all_sites_or_join(obj, 'editor') + return self.get_sites_for_role(obj, 'editor') def reviewers(self, obj): - return self.render_all_sites_or_join(obj, 'reviewer') + return self.get_sites_for_role(obj, 'reviewer')