From ca47dba49767d0b32bce3aba08d10c2853ea2bb0 Mon Sep 17 00:00:00 2001 From: witold Date: Mon, 1 Apr 2024 14:37:31 +0200 Subject: [PATCH] feat(admin): import institution admin --- lab/admin/institution.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/lab/admin/institution.py b/lab/admin/institution.py index 308c33f16..d43a0fa90 100644 --- a/lab/admin/institution.py +++ b/lab/admin/institution.py @@ -3,18 +3,49 @@ from django.contrib import admin from django.http.request import HttpRequest +from lab.models.participation import Participation + from ..models import Institution from .mixins import LabAdminAllowedMixin +class ParticipationInline(admin.TabularInline): + model = Participation + verbose_name = "Institution member" + verbose_name_plural = "Institution members" + extra = 0 + + fields = ("user",) + + def has_change_permission( + self, request: HttpRequest, obj: Institution | None = None + ) -> bool: + return False + + def has_delete_permission( + self, request: HttpRequest, obj: Institution | None = None + ) -> bool: + return False + + def has_add_permission( + self, request: HttpRequest, obj: Institution | None = None + ) -> bool: + return False + + # Allowance: ADMIN:lab admin, EDITOR:lab admin, CREATOR: any user, VIEWER:any user @admin.register(Institution) class InstitutionAdmin(LabAdminAllowedMixin, admin.ModelAdmin): - list_display = ("name", "country") - fields = ("name", "country") + list_display = ("name", "country", "ror_id", "members_num") + fields = ("name", "country", "ror_id") + inlines = [ParticipationInline] def has_add_permission( self, request: HttpRequest, obj: Optional[Institution] = None ) -> bool: # Any user can add an institution return True + + @admin.display + def members_num(self, obj: Participation) -> int: + return obj.participation_set.count()