From a93f1b36bbae4bb0479169118d98de8265d915d2 Mon Sep 17 00:00:00 2001 From: bart-maykin Date: Thu, 18 Apr 2024 09:30:01 +0200 Subject: [PATCH] :sparkles: [#315] added extra version and uuid fields in admin --- src/objects/core/admin.py | 7 +++++- src/objects/core/models.py | 11 +++++++++ src/objects/token/admin.py | 48 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/objects/core/admin.py b/src/objects/core/admin.py index 223b9994..74d70823 100644 --- a/src/objects/core/admin.py +++ b/src/objects/core/admin.py @@ -7,7 +7,12 @@ @admin.register(ObjectType) class ObjectTypeAdmin(admin.ModelAdmin): - readonly_fields = ("_name",) + list_display = ("_name", "uuid", "get_version") + readonly_fields = ("_name", "get_version") + + def get_version(self, obj): + if obj: + return obj.version class ObjectRecordInline(admin.TabularInline): diff --git a/src/objects/core/models.py b/src/objects/core/models.py index 8cf1255d..93835da6 100644 --- a/src/objects/core/models.py +++ b/src/objects/core/models.py @@ -40,6 +40,17 @@ def url(self): # zds_client.get_operation_url() can be used here but it increases HTTP overhead return f"{self.service.api_root}objecttypes/{self.uuid}" + @property + def version(self): + client = self.service.build_client() + try: + object_type_data = client.retrieve("objecttype", url=self.url) + except (ClientError, ConnectionError, ValueError) as exc: + return None + + if recent_version := object_type_data["versions"][-1]: + return recent_version.split("/versions/")[-1] + def clean(self): client = self.service.build_client() try: diff --git a/src/objects/token/admin.py b/src/objects/token/admin.py index 1040a3cf..0780aa9f 100644 --- a/src/objects/token/admin.py +++ b/src/objects/token/admin.py @@ -14,7 +14,30 @@ @admin.register(Permission) class PermissionAdmin(admin.ModelAdmin): - list_display = ("token_auth", "object_type", "mode", "use_fields") + list_display = ( + "token_auth", + "object_type", + "mode", + "use_fields", + "get_uuid", + "get_version", + ) + readonly_fields = ( + "get_uuid", + "get_version", + ) + + def get_uuid(self, obj): + if obj: + return obj.object_type.uuid + + def get_version(self, obj): + if obj: + if object_type := obj.object_type: + return object_type.version + + get_version.short_description = "Version" + get_uuid.short_description = "UUID" def get_object_fields(self): object_serializer = ObjectSerializer() @@ -103,7 +126,28 @@ def add_view(self, request, form_url="", extra_context=None): class PermissionInline(EditInlineAdminMixin, admin.TabularInline): model = Permission - fields = ("object_type", "mode", "use_fields", "fields") + fk_name = "token_auth" + fields = ( + "object_type", + "mode", + "use_fields", + "fields", + "get_uuid", + "get_version", + ) + + def get_uuid(self, obj): + if obj: + if object_type := obj.object_type: + return object_type.uuid + + def get_version(self, obj): + if obj: + if object_type := obj.object_type: + return object_type.version + + get_version.short_description = "Version" + get_uuid.short_description = "UUID" @admin.register(TokenAuth)