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 4afd616
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/objects/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
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
45 changes: 43 additions & 2 deletions src/objects/token/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -104,7 +127,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 4afd616

Please sign in to comment.