Skip to content

Commit

Permalink
✨ [#315] added extra version and uuid fields in admin
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Apr 18, 2024
1 parent b819418 commit dc1b716
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/objects/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
from django.contrib.gis import forms
from django.contrib.gis.db.models import GeometryField

from zds_client.client import ClientError

from .models import Object, ObjectRecord, ObjectType


@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):
Expand Down
11 changes: 11 additions & 0 deletions src/objects/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
47 changes: 45 additions & 2 deletions src/objects/token/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.contrib import admin
from django.contrib.admin.utils import unquote

from zds_client.client import ClientError

from objects.api.serializers import ObjectSerializer
from objects.core.models import ObjectType
from objects.utils.admin import EditInlineAdminMixin
Expand All @@ -14,7 +16,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()
Expand Down Expand Up @@ -104,7 +129,25 @@ def add_view(self, request, form_url="", extra_context=None):
class PermissionInline(EditInlineAdminMixin, admin.TabularInline):
model = Permission
fk_name = "token_auth"
fields = ("object_type", "mode", "use_fields", "fields")
fields = (
"object_type",
"mode",
"use_fields",
"fields",
"get_uuid",
"get_version",
)

def get_uuid(self, 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"


@admin.register(TokenAuth)
Expand Down

0 comments on commit dc1b716

Please sign in to comment.