Skip to content

Commit

Permalink
Add to admin
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jun 23, 2023
1 parent f10b4d2 commit 7a6962a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from django.apps import apps
from django.core import serializers
from django.utils import timezone
from django.utils.html import escape
from django.utils.safestring import mark_safe


DATE_FORMAT = "%Y-%m-%d"
Expand Down Expand Up @@ -176,3 +178,17 @@ def get_choice_key(choices, value):
if choices[key] == value:
return key
return None


def pretty_print_readonly_jsonfield(jsonfield_data):
"""
Display a pretty readonly version of a JSONField
https://stackoverflow.com/a/60219265
"""
result = ""

if jsonfield_data:
result = json.dumps(jsonfield_data, indent=4, ensure_ascii=False)
result = mark_safe(f"<pre>{escape(result)}</pre>")

return result
10 changes: 10 additions & 0 deletions users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.utils.translation import gettext_lazy as _

from core.admin import admin_site
from core.utils.utilities import pretty_print_readonly_jsonfield
from users import constants
from users.models import User, UserCard

Expand Down Expand Up @@ -71,6 +72,7 @@ class UserAdmin(UserAdmin):
"question_count",
"quiz_count",
"has_user_card",
"logs_display",
"created",
"updated",
]
Expand All @@ -97,6 +99,7 @@ class UserAdmin(UserAdmin):
)
},
),
("Autres", {"fields": ("logs_display",)}),
("Dates", {"fields": ("last_login", "created", "updated")}),
)
add_fieldsets = (
Expand Down Expand Up @@ -148,6 +151,13 @@ def has_user_card(self, user):
has_user_card.short_description = "User card"
has_user_card.boolean = True

def logs_display(self, tender=None):
if tender:
return pretty_print_readonly_jsonfield(tender.logs)
return "-"

logs_display.short_description = User._meta.get_field("logs").verbose_name


class UserCardAdmin(admin.ModelAdmin):
list_display = [
Expand Down

0 comments on commit 7a6962a

Please sign in to comment.