diff --git a/rdmo/accounts/admin.py b/rdmo/accounts/admin.py index 1cfede8540..8d65fb1f57 100644 --- a/rdmo/accounts/admin.py +++ b/rdmo/accounts/admin.py @@ -30,15 +30,29 @@ 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( - 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()) ) + 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: + 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()]) @@ -47,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')